Skip to content

Commit 13a7e49

Browse files
committed
mbe: Refactor the diagnostic for unrecognized metavariable expressions
1 parent 7eb3204 commit 13a7e49

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

compiler/rustc_expand/messages.ftl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ expand_mve_missing_paren =
149149
.note = metavariable expressions use function-like parentheses syntax
150150
.suggestion = try adding parentheses
151151
152+
expand_mve_unrecognized_expr =
153+
unrecognized metavariable expression
154+
.label = not a valid metavariable expression
155+
.note = valid metavariable expressions are {$valid_expr_list}
156+
152157
expand_mve_unrecognized_var =
153158
variable `{$key}` is not recognized in meta-variable expression
154159

compiler/rustc_expand/src/errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,16 @@ mod metavar_exprs {
527527
pub insert_span: Option<Span>,
528528
}
529529

530+
#[derive(Diagnostic)]
531+
#[note]
532+
#[diag(expand_mve_unrecognized_expr)]
533+
pub(crate) struct MveUnrecognizedExpr {
534+
#[primary_span]
535+
#[label]
536+
pub span: Span,
537+
pub valid_expr_list: &'static str,
538+
}
539+
530540
#[derive(Diagnostic)]
531541
#[diag(expand_mve_unrecognized_var)]
532542
pub(crate) struct MveUnrecognizedVar {

compiler/rustc_expand/src/mbe/metavar_expr.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ const EXPR_NAME_ARG_MAP: &[(&str, Option<usize>)] = &[
2121
("len", Some(2)),
2222
];
2323

24+
/// List of the above for diagnostics
25+
const VALID_METAVAR_EXPR_NAMES: &str = "`count`, `ignore`, `index`, `len`, and `concat`";
26+
2427
/// A meta-variable expression, for expansions based on properties of meta-variables.
2528
#[derive(Debug, PartialEq, Encodable, Decodable)]
2629
pub(crate) enum MetaVarExpr {
@@ -89,15 +92,11 @@ impl MetaVarExpr {
8992
"index" => MetaVarExpr::Index(parse_depth(&mut iter, psess, ident.span)?),
9093
"len" => MetaVarExpr::Len(parse_depth(&mut iter, psess, ident.span)?),
9194
_ => {
92-
let err_msg = "unrecognized meta-variable expression";
93-
let mut err = psess.dcx().struct_span_err(ident.span, err_msg);
94-
err.span_suggestion(
95-
ident.span,
96-
"supported expressions are count, ignore, index and len",
97-
"",
98-
Applicability::MachineApplicable,
99-
);
100-
return Err(err);
95+
let err = errors::MveUnrecognizedExpr {
96+
span: ident.span,
97+
valid_expr_list: VALID_METAVAR_EXPR_NAMES,
98+
};
99+
return Err(psess.dcx().create_err(err));
101100
}
102101
};
103102
check_trailing_tokens(&mut iter, psess, ident)?;

tests/ui/macros/metavar-expressions/syntax-errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ macro_rules! unknown_ignore_ident {
116116

117117
macro_rules! unknown_metavar {
118118
( $( $i:ident ),* ) => { ${ aaaaaaaaaaaaaa(i) } };
119-
//~^ ERROR unrecognized meta-variable expression
119+
//~^ ERROR unrecognized metavariable expression
120120
}
121121

122122
fn main() {}

tests/ui/macros/metavar-expressions/syntax-errors.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,13 @@ error: meta-variables within meta-variable expressions must be referenced using
182182
LL | ${ignore(bar)}
183183
| ^^^^^^
184184

185-
error: unrecognized meta-variable expression
185+
error: unrecognized metavariable expression
186186
--> $DIR/syntax-errors.rs:118:33
187187
|
188188
LL | ( $( $i:ident ),* ) => { ${ aaaaaaaaaaaaaa(i) } };
189-
| ^^^^^^^^^^^^^^ help: supported expressions are count, ignore, index and len
189+
| ^^^^^^^^^^^^^^ not a valid metavariable expression
190+
|
191+
= note: valid metavariable expressions are `count`, `ignore`, `index`, `len`, and `concat`
190192

191193
error: expected identifier or string literal
192194
--> $DIR/syntax-errors.rs:38:14

0 commit comments

Comments
 (0)