From 87e981996fe47826a8f3a69e3d3cc73ec68dbf8d Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Sat, 21 Jun 2025 02:59:14 -0400 Subject: [PATCH] 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. --- compiler/rustc_expand/messages.ftl | 5 +++++ compiler/rustc_expand/src/errors.rs | 10 ++++++++++ compiler/rustc_expand/src/mbe/metavar_expr.rs | 14 +++++--------- .../ui/macros/metavar-expressions/syntax-errors.rs | 2 +- .../metavar-expressions/syntax-errors.stderr | 6 ++++-- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_expand/messages.ftl b/compiler/rustc_expand/messages.ftl index 7b8c1e076a09..89d6e62834d6 100644 --- a/compiler/rustc_expand/messages.ftl +++ b/compiler/rustc_expand/messages.ftl @@ -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 diff --git a/compiler/rustc_expand/src/errors.rs b/compiler/rustc_expand/src/errors.rs index b56a2b043e75..3ac5d2130535 100644 --- a/compiler/rustc_expand/src/errors.rs +++ b/compiler/rustc_expand/src/errors.rs @@ -530,6 +530,16 @@ mod metavar_exprs { pub insert_span: Option, } + #[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 { diff --git a/compiler/rustc_expand/src/mbe/metavar_expr.rs b/compiler/rustc_expand/src/mbe/metavar_expr.rs index 8f15b5794c7f..d2b275ad20a9 100644 --- a/compiler/rustc_expand/src/mbe/metavar_expr.rs +++ b/compiler/rustc_expand/src/mbe/metavar_expr.rs @@ -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)?; diff --git a/tests/ui/macros/metavar-expressions/syntax-errors.rs b/tests/ui/macros/metavar-expressions/syntax-errors.rs index a6eebdfa6744..585ea4d5979c 100644 --- a/tests/ui/macros/metavar-expressions/syntax-errors.rs +++ b/tests/ui/macros/metavar-expressions/syntax-errors.rs @@ -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() {} diff --git a/tests/ui/macros/metavar-expressions/syntax-errors.stderr b/tests/ui/macros/metavar-expressions/syntax-errors.stderr index f3c08f05e2a4..bf1c7673a6ce 100644 --- a/tests/ui/macros/metavar-expressions/syntax-errors.stderr +++ b/tests/ui/macros/metavar-expressions/syntax-errors.stderr @@ -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