mbe: Refactor the diagnostic for unrecognized metavariable expressions

Change to a structural diagnostic, update the valid list, and move the
valid list to a note.
This commit is contained in:
Trevor Gross 2025-06-21 02:59:14 -04:00
parent e7ef31d651
commit 87e981996f
5 changed files with 25 additions and 12 deletions

View file

@ -154,6 +154,11 @@ expand_mve_missing_paren =
.note = metavariable expressions use function-like parentheses syntax
.suggestion = try adding parentheses
expand_mve_unrecognized_expr =
unrecognized metavariable expression
.label = not a valid metavariable expression
.note = valid metavariable expressions are {$valid_expr_list}
expand_mve_unrecognized_var =
variable `{$key}` is not recognized in meta-variable expression

View file

@ -530,6 +530,16 @@ mod metavar_exprs {
pub insert_span: Option<Span>,
}
#[derive(Diagnostic)]
#[note]
#[diag(expand_mve_unrecognized_expr)]
pub(crate) struct MveUnrecognizedExpr {
#[primary_span]
#[label]
pub span: Span,
pub valid_expr_list: &'static str,
}
#[derive(Diagnostic)]
#[diag(expand_mve_unrecognized_var)]
pub(crate) struct MveUnrecognizedVar {

View file

@ -79,15 +79,11 @@ impl MetaVarExpr {
"index" => MetaVarExpr::Index(parse_depth(&mut iter, psess, ident.span)?),
"len" => MetaVarExpr::Len(parse_depth(&mut iter, psess, ident.span)?),
_ => {
let err_msg = "unrecognized meta-variable expression";
let mut err = psess.dcx().struct_span_err(ident.span, err_msg);
err.span_suggestion(
ident.span,
"supported expressions are count, ignore, index and len",
"",
Applicability::MachineApplicable,
);
return Err(err);
let err = errors::MveUnrecognizedExpr {
span: ident.span,
valid_expr_list: "`count`, `ignore`, `index`, `len`, and `concat`",
};
return Err(psess.dcx().create_err(err));
}
};
check_trailing_tokens(&mut iter, psess, ident)?;

View file

@ -118,7 +118,7 @@ macro_rules! unknown_ignore_ident {
macro_rules! unknown_metavar {
( $( $i:ident ),* ) => { ${ aaaaaaaaaaaaaa(i) } };
//~^ ERROR unrecognized meta-variable expression
//~^ ERROR unrecognized metavariable expression
}
fn main() {}

View file

@ -192,11 +192,13 @@ error: meta-variables within meta-variable expressions must be referenced using
LL | ${ignore(bar)}
| ^^^^^^
error: unrecognized meta-variable expression
error: unrecognized metavariable expression
--> $DIR/syntax-errors.rs:120:33
|
LL | ( $( $i:ident ),* ) => { ${ aaaaaaaaaaaaaa(i) } };
| ^^^^^^^^^^^^^^ help: supported expressions are count, ignore, index and len
| ^^^^^^^^^^^^^^ not a valid metavariable expression
|
= note: valid metavariable expressions are `count`, `ignore`, `index`, `len`, and `concat`
error: expected identifier or string literal
--> $DIR/syntax-errors.rs:38:14