From 4b456cb68314bce1ddb5140e0c234510e489c9fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lena=20Miliz=C3=A9?= Date: Wed, 12 Apr 2023 11:18:50 +0200 Subject: [PATCH 1/2] compiler: improve captured metavariables diagnostic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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é --- compiler/rustc_expand/src/mbe/diagnostics.rs | 4 +++- tests/ui/macros/nonterminal-matching.stderr | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_expand/src/mbe/diagnostics.rs b/compiler/rustc_expand/src/mbe/diagnostics.rs index 6bc393c6534f3..00bb7b0f2a24d 100644 --- a/compiler/rustc_expand/src/mbe/diagnostics.rs +++ b/compiler/rustc_expand/src/mbe/diagnostics.rs @@ -66,7 +66,9 @@ pub(super) fn failed_to_match_macro<'cx>( && (matches!(expected_token.kind, TokenKind::Interpolated(_)) || matches!(token.kind, TokenKind::Interpolated(_))) { - err.note("captured metavariables except for `$tt`, `$ident` and `$lifetime` cannot be compared to other tokens"); + err.note("captured metavariables except for `:tt`, `:ident` and `:lifetime` cannot be compared to other tokens"); + err.note("see https://doc.rust-lang.org/nightly/reference/macros-by-example.html#forwarding-a-matched-fragment for more information"); + err.help("try using `:tt` instead in the macro definition"); } // Check whether there's a missing comma in this macro call, like `println!("{}" a);` diff --git a/tests/ui/macros/nonterminal-matching.stderr b/tests/ui/macros/nonterminal-matching.stderr index 762ecc3207fa5..c295eddbe47e5 100644 --- a/tests/ui/macros/nonterminal-matching.stderr +++ b/tests/ui/macros/nonterminal-matching.stderr @@ -18,7 +18,9 @@ LL | macro n(a $nt_item b) { ... LL | complex_nonterminal!(enum E {}); | ------------------------------- in this macro invocation - = note: captured metavariables except for `$tt`, `$ident` and `$lifetime` cannot be compared to other tokens + = note: captured metavariables except for `:tt`, `:ident` and `:lifetime` cannot be compared to other tokens + = note: see https://doc.rust-lang.org/nightly/reference/macros-by-example.html#forwarding-a-matched-fragment for more information + = help: try using `:tt` instead in the macro definition = note: this error originates in the macro `complex_nonterminal` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to previous error From 04f20d4ac8677b13c41e54fdde319c4e13b58330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lena=20Miliz=C3=A9?= Date: Wed, 12 Apr 2023 15:43:50 +0200 Subject: [PATCH 2/2] compiler: print the suggestion only for local macros MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit And wrap the link in the diagnostic in angle brackets. Signed-off-by: Lena Milizé --- compiler/rustc_expand/src/mbe/diagnostics.rs | 7 +++++-- tests/ui/macros/nonterminal-matching.stderr | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_expand/src/mbe/diagnostics.rs b/compiler/rustc_expand/src/mbe/diagnostics.rs index 00bb7b0f2a24d..35572292271c4 100644 --- a/compiler/rustc_expand/src/mbe/diagnostics.rs +++ b/compiler/rustc_expand/src/mbe/diagnostics.rs @@ -67,8 +67,11 @@ pub(super) fn failed_to_match_macro<'cx>( || matches!(token.kind, TokenKind::Interpolated(_))) { err.note("captured metavariables except for `:tt`, `:ident` and `:lifetime` cannot be compared to other tokens"); - err.note("see https://doc.rust-lang.org/nightly/reference/macros-by-example.html#forwarding-a-matched-fragment for more information"); - err.help("try using `:tt` instead in the macro definition"); + err.note("see for more information"); + + if !def_span.is_dummy() && !cx.source_map().is_imported(def_span) { + err.help("try using `:tt` instead in the macro definition"); + } } // Check whether there's a missing comma in this macro call, like `println!("{}" a);` diff --git a/tests/ui/macros/nonterminal-matching.stderr b/tests/ui/macros/nonterminal-matching.stderr index c295eddbe47e5..c2b047022ed5a 100644 --- a/tests/ui/macros/nonterminal-matching.stderr +++ b/tests/ui/macros/nonterminal-matching.stderr @@ -19,7 +19,7 @@ LL | macro n(a $nt_item b) { LL | complex_nonterminal!(enum E {}); | ------------------------------- in this macro invocation = note: captured metavariables except for `:tt`, `:ident` and `:lifetime` cannot be compared to other tokens - = note: see https://doc.rust-lang.org/nightly/reference/macros-by-example.html#forwarding-a-matched-fragment for more information + = note: see for more information = help: try using `:tt` instead in the macro definition = note: this error originates in the macro `complex_nonterminal` (in Nightly builds, run with -Z macro-backtrace for more info)