diff options
| author | Trevor Gross <tmgross@umich.edu> | 2025-06-21 02:59:14 -0400 |
|---|---|---|
| committer | Trevor Gross <tmgross@umich.edu> | 2025-07-09 21:11:09 -0400 |
| commit | 87e981996fe47826a8f3a69e3d3cc73ec68dbf8d (patch) | |
| tree | dccb6b1b6b7222dc42fc2c42669b659ef1a41c2c | |
| parent | e7ef31d651be6aa8809933fabb793b98300b970f (diff) | |
| download | rust-87e981996fe47826a8f3a69e3d3cc73ec68dbf8d.tar.gz rust-87e981996fe47826a8f3a69e3d3cc73ec68dbf8d.zip | |
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.
| -rw-r--r-- | compiler/rustc_expand/messages.ftl | 5 | ||||
| -rw-r--r-- | compiler/rustc_expand/src/errors.rs | 10 | ||||
| -rw-r--r-- | compiler/rustc_expand/src/mbe/metavar_expr.rs | 14 | ||||
| -rw-r--r-- | tests/ui/macros/metavar-expressions/syntax-errors.rs | 2 | ||||
| -rw-r--r-- | tests/ui/macros/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 7b8c1e076a0..89d6e62834d 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 b56a2b043e7..3ac5d213053 100644 --- a/compiler/rustc_expand/src/errors.rs +++ b/compiler/rustc_expand/src/errors.rs @@ -531,6 +531,16 @@ mod metavar_exprs { } #[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 { #[primary_span] diff --git a/compiler/rustc_expand/src/mbe/metavar_expr.rs b/compiler/rustc_expand/src/mbe/metavar_expr.rs index 8f15b5794c7..d2b275ad20a 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 a6eebdfa674..585ea4d5979 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 f3c08f05e2a..bf1c7673a6c 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 |
