Skip to content

Commit 7e5760b

Browse files
committed
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.
1 parent 41a449d commit 7e5760b

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
@@ -154,6 +154,11 @@ expand_mve_missing_paren =
154154
.note = metavariable expressions use function-like parentheses syntax
155155
.suggestion = try adding parentheses
156156
157+
expand_mve_unrecognized_expr =
158+
unrecognized metavariable expression
159+
.label = not a valid metavariable expression
160+
.note = valid metavariable expressions are {$valid_expr_list}
161+
157162
expand_mve_unrecognized_var =
158163
variable `{$key}` is not recognized in meta-variable expression
159164

compiler/rustc_expand/src/errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,16 @@ mod metavar_exprs {
530530
pub insert_span: Option<Span>,
531531
}
532532

533+
#[derive(Diagnostic)]
534+
#[note]
535+
#[diag(expand_mve_unrecognized_expr)]
536+
pub(crate) struct MveUnrecognizedExpr {
537+
#[primary_span]
538+
#[label]
539+
pub span: Span,
540+
pub valid_expr_list: &'static str,
541+
}
542+
533543
#[derive(Diagnostic)]
534544
#[diag(expand_mve_unrecognized_var)]
535545
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
@@ -32,6 +32,9 @@ const EXPR_NAME_ARG_MAP: &[(&str, ArgSpec)] = &[
3232
("len", ArgSpec::Between(0, 1)),
3333
];
3434

35+
/// List of the above for diagnostics
36+
const VALID_METAVAR_EXPR_NAMES: &str = "`count`, `ignore`, `index`, `len`, and `concat`";
37+
3538
/// A meta-variable expression, for expansions based on properties of meta-variables.
3639
#[derive(Debug, PartialEq, Encodable, Decodable)]
3740
pub(crate) enum MetaVarExpr {
@@ -99,15 +102,11 @@ impl MetaVarExpr {
99102
"index" => MetaVarExpr::Index(parse_depth(&mut iter, psess, ident.span)?),
100103
"len" => MetaVarExpr::Len(parse_depth(&mut iter, psess, ident.span)?),
101104
_ => {
102-
let err_msg = "unrecognized meta-variable expression";
103-
let mut err = psess.dcx().struct_span_err(ident.span, err_msg);
104-
err.span_suggestion(
105-
ident.span,
106-
"supported expressions are count, ignore, index and len",
107-
"",
108-
Applicability::MachineApplicable,
109-
);
110-
return Err(err);
105+
let err = errors::MveUnrecognizedExpr {
106+
span: ident.span,
107+
valid_expr_list: VALID_METAVAR_EXPR_NAMES,
108+
};
109+
return Err(psess.dcx().create_err(err));
111110
}
112111
};
113112
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
@@ -118,7 +118,7 @@ macro_rules! unknown_ignore_ident {
118118

119119
macro_rules! unknown_metavar {
120120
( $( $i:ident ),* ) => { ${ aaaaaaaaaaaaaa(i) } };
121-
//~^ ERROR unrecognized meta-variable expression
121+
//~^ ERROR unrecognized metavariable expression
122122
}
123123

124124
fn main() {}

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

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

195-
error: unrecognized meta-variable expression
195+
error: unrecognized metavariable expression
196196
--> $DIR/syntax-errors.rs:120:33
197197
|
198198
LL | ( $( $i:ident ),* ) => { ${ aaaaaaaaaaaaaa(i) } };
199-
| ^^^^^^^^^^^^^^ help: supported expressions are count, ignore, index and len
199+
| ^^^^^^^^^^^^^^ not a valid metavariable expression
200+
|
201+
= note: valid metavariable expressions are `count`, `ignore`, `index`, `len`, and `concat`
200202

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

0 commit comments

Comments
 (0)