Skip to content

Commit 4b456cb

Browse files
committed
compiler: improve captured metavariables diagnostic
Adds a link to the relevant part of The Rust Reference in the eror message, and suggests a possible fix (replacing the fragment specifier with :tt in the macro definition). Fixes typos in the original message. Signed-off-by: Lena Milizé <[email protected]>
1 parent 87a2408 commit 4b456cb

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

compiler/rustc_expand/src/mbe/diagnostics.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ pub(super) fn failed_to_match_macro<'cx>(
6666
&& (matches!(expected_token.kind, TokenKind::Interpolated(_))
6767
|| matches!(token.kind, TokenKind::Interpolated(_)))
6868
{
69-
err.note("captured metavariables except for `$tt`, `$ident` and `$lifetime` cannot be compared to other tokens");
69+
err.note("captured metavariables except for `:tt`, `:ident` and `:lifetime` cannot be compared to other tokens");
70+
err.note("see https://doc.rust-lang.org/nightly/reference/macros-by-example.html#forwarding-a-matched-fragment for more information");
71+
err.help("try using `:tt` instead in the macro definition");
7072
}
7173

7274
// Check whether there's a missing comma in this macro call, like `println!("{}" a);`

tests/ui/macros/nonterminal-matching.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ LL | macro n(a $nt_item b) {
1818
...
1919
LL | complex_nonterminal!(enum E {});
2020
| ------------------------------- in this macro invocation
21-
= note: captured metavariables except for `$tt`, `$ident` and `$lifetime` cannot be compared to other tokens
21+
= note: captured metavariables except for `:tt`, `:ident` and `:lifetime` cannot be compared to other tokens
22+
= note: see https://doc.rust-lang.org/nightly/reference/macros-by-example.html#forwarding-a-matched-fragment for more information
23+
= help: try using `:tt` instead in the macro definition
2224
= note: this error originates in the macro `complex_nonterminal` (in Nightly builds, run with -Z macro-backtrace for more info)
2325

2426
error: aborting due to previous error

0 commit comments

Comments
 (0)