Add metavariables to TokenDescription.

Pasted metavariables are wrapped in invisible delimiters, which
pretty-print as empty strings, and changing that can break some proc
macros. But error messages saying "expected identifer, found ``" are
bad. So this commit adds support for metavariables in `TokenDescription`
so they print as "metavariable" in error messages, instead of "``".

It's not used meaningfully yet, but will be needed to get rid of
interpolated tokens.
This commit is contained in:
Nicholas Nethercote 2024-04-18 08:58:06 +10:00
parent afe238f66f
commit cfafa9380b
3 changed files with 58 additions and 16 deletions

View file

@ -1086,6 +1086,8 @@ pub(crate) enum ExpectedIdentifierFound {
ReservedKeyword(#[primary_span] Span),
#[label(parse_expected_identifier_found_doc_comment)]
DocComment(#[primary_span] Span),
#[label(parse_expected_identifier_found_metavar)]
MetaVar(#[primary_span] Span),
#[label(parse_expected_identifier)]
Other(#[primary_span] Span),
}
@ -1099,6 +1101,7 @@ impl ExpectedIdentifierFound {
Some(TokenDescription::Keyword) => ExpectedIdentifierFound::Keyword,
Some(TokenDescription::ReservedKeyword) => ExpectedIdentifierFound::ReservedKeyword,
Some(TokenDescription::DocComment) => ExpectedIdentifierFound::DocComment,
Some(TokenDescription::MetaVar(_)) => ExpectedIdentifierFound::MetaVar,
None => ExpectedIdentifierFound::Other,
})(span)
}
@ -1117,6 +1120,7 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for ExpectedIdentifier {
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
let token_descr = TokenDescription::from_token(&self.token);
let mut add_token = true;
let mut diag = Diag::new(dcx, level, match token_descr {
Some(TokenDescription::ReservedIdentifier) => {
fluent::parse_expected_identifier_found_reserved_identifier_str
@ -1128,10 +1132,16 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for ExpectedIdentifier {
Some(TokenDescription::DocComment) => {
fluent::parse_expected_identifier_found_doc_comment_str
}
Some(TokenDescription::MetaVar(_)) => {
add_token = false;
fluent::parse_expected_identifier_found_metavar_str
}
None => fluent::parse_expected_identifier_found_str,
});
diag.span(self.span);
diag.arg("token", self.token);
if add_token {
diag.arg("token", self.token);
}
if let Some(sugg) = self.suggest_raw {
sugg.add_to_diag(&mut diag);
@ -1171,6 +1181,7 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for ExpectedSemi {
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, G> {
let token_descr = TokenDescription::from_token(&self.token);
let mut add_token = true;
let mut diag = Diag::new(dcx, level, match token_descr {
Some(TokenDescription::ReservedIdentifier) => {
fluent::parse_expected_semi_found_reserved_identifier_str
@ -1180,10 +1191,16 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for ExpectedSemi {
fluent::parse_expected_semi_found_reserved_keyword_str
}
Some(TokenDescription::DocComment) => fluent::parse_expected_semi_found_doc_comment_str,
Some(TokenDescription::MetaVar(_)) => {
add_token = false;
fluent::parse_expected_semi_found_metavar_str
}
None => fluent::parse_expected_semi_found_str,
});
diag.span(self.span);
diag.arg("token", self.token);
if add_token {
diag.arg("token", self.token);
}
if let Some(unexpected_token_label) = self.unexpected_token_label {
diag.span_label(unexpected_token_label, fluent::parse_label_unexpected_token);
@ -1925,6 +1942,12 @@ pub(crate) enum UnexpectedTokenAfterStructName {
span: Span,
token: Token,
},
#[diag(parse_unexpected_token_after_struct_name_found_metavar)]
MetaVar {
#[primary_span]
#[label(parse_unexpected_token_after_struct_name)]
span: Span,
},
#[diag(parse_unexpected_token_after_struct_name_found_other)]
Other {
#[primary_span]
@ -1941,6 +1964,7 @@ impl UnexpectedTokenAfterStructName {
Some(TokenDescription::Keyword) => Self::Keyword { span, token },
Some(TokenDescription::ReservedKeyword) => Self::ReservedKeyword { span, token },
Some(TokenDescription::DocComment) => Self::DocComment { span, token },
Some(TokenDescription::MetaVar(_)) => Self::MetaVar { span },
None => Self::Other { span, token },
}
}