From 700cb1940820e1a9568c322d112448d7b5fd5cd8 Mon Sep 17 00:00:00 2001 From: Urgau Date: Tue, 24 Feb 2026 19:56:05 +0100 Subject: [PATCH 1/3] Revert "Also duplicate `#[expect]` attribute in `#[derive]`-ed code" --- .../src/deriving/generic/mod.rs | 1 - .../derive-expect-issue-150553-3.rs | 4 +++- .../derive-expect-issue-150553-3.stderr | 12 ++---------- .../derive-expect-issue-150553.rs | 5 ++++- .../derive-expect-issue-150553.stderr | 15 +++++++++++++++ 5 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553.stderr diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index b2fff77e3f162..b392a9623d050 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -540,7 +540,6 @@ impl<'a> TraitDef<'a> { .filter(|a| { a.has_any_name(&[ sym::allow, - sym::expect, sym::warn, sym::deny, sym::forbid, diff --git a/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553-3.rs b/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553-3.rs index 904366e6532e5..b5f06912a97fc 100644 --- a/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553-3.rs +++ b/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553-3.rs @@ -1,3 +1,5 @@ +// FIXME: Bring back duplication of the `#[expect]` attribute when deriving. +// // Make sure we produce the unfulfilled expectation lint if neither the struct or the // derived code fulfilled it. @@ -5,7 +7,7 @@ #[expect(unexpected_cfgs)] //~^ WARN this lint expectation is unfulfilled -//~^^ WARN this lint expectation is unfulfilled +//FIXME ~^^ WARN this lint expectation is unfulfilled #[derive(Debug)] pub struct MyStruct { pub t_ref: i64, diff --git a/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553-3.stderr b/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553-3.stderr index 6478ec435db07..e75483df3f78a 100644 --- a/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553-3.stderr +++ b/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553-3.stderr @@ -1,18 +1,10 @@ warning: this lint expectation is unfulfilled - --> $DIR/derive-expect-issue-150553-3.rs:6:10 + --> $DIR/derive-expect-issue-150553-3.rs:8:10 | LL | #[expect(unexpected_cfgs)] | ^^^^^^^^^^^^^^^ | = note: `#[warn(unfulfilled_lint_expectations)]` on by default -warning: this lint expectation is unfulfilled - --> $DIR/derive-expect-issue-150553-3.rs:6:10 - | -LL | #[expect(unexpected_cfgs)] - | ^^^^^^^^^^^^^^^ - | - = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` - -warning: 2 warnings emitted +warning: 1 warning emitted diff --git a/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553.rs b/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553.rs index 1752835c8bb81..06ec71bb6c84d 100644 --- a/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553.rs +++ b/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553.rs @@ -1,9 +1,11 @@ +// FIXME: Bring back duplication of the `#[expect]` attribute when deriving. +// // Make sure we properly copy the `#[expect]` attr to the derived code and that no // unfulfilled expectations are trigerred. // // See for rational. -//@ check-pass +//@ check-fail #![deny(redundant_lifetimes)] @@ -12,6 +14,7 @@ use std::fmt::Debug; #[derive(Debug)] #[expect(redundant_lifetimes)] pub struct RefWrapper<'a, T> +//~^ ERROR redundant_lifetimes where 'a: 'static, T: Debug, diff --git a/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553.stderr b/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553.stderr new file mode 100644 index 0000000000000..c4047329d5c1f --- /dev/null +++ b/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553.stderr @@ -0,0 +1,15 @@ +error: unnecessary lifetime parameter `'a` + --> $DIR/derive-expect-issue-150553.rs:16:23 + | +LL | pub struct RefWrapper<'a, T> + | ^^ + | + = note: you can use the `'static` lifetime directly, in place of `'a` +note: the lint level is defined here + --> $DIR/derive-expect-issue-150553.rs:10:9 + | +LL | #![deny(redundant_lifetimes)] + | ^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 1 previous error + From 8fefa653a920cd7a723927466acb5c626efd0e3c Mon Sep 17 00:00:00 2001 From: Urgau Date: Tue, 24 Feb 2026 20:00:58 +0100 Subject: [PATCH 2/3] Add regression test for lint expectations with expansion and derives --- .../derive-expect-issue-150553-4.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553-4.rs diff --git a/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553-4.rs b/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553-4.rs new file mode 100644 index 0000000000000..fb0d508186f8b --- /dev/null +++ b/tests/ui/lint/rfc-2383-lint-reason/derive-expect-issue-150553-4.rs @@ -0,0 +1,15 @@ +// This test makes sure that expended items with derives don't interfear with lint expectations. +// +// See for some context. + +//@ check-pass + +#[derive(Clone, Debug)] +#[expect(unused)] +pub struct LoggingArgs { + #[cfg(false)] + x: i32, + y: i32, +} + +fn main() {} From fe3b3a424685105d01011ddb0bdcb5560fa4f8d1 Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Wed, 25 Feb 2026 17:20:18 +0100 Subject: [PATCH 3/3] Revert "rustc_expand: improve diagnostics for non-repeatable metavars" This reverts commit 93b997356aa3a006475c169db42d9279d745bf36. --- compiler/rustc_expand/src/base.rs | 11 +--- compiler/rustc_expand/src/mbe/diagnostics.rs | 52 +------------------ compiler/rustc_expand/src/mbe/macro_rules.rs | 46 ++-------------- tests/ui/macros/issue-6596-1.stderr | 6 +-- tests/ui/macros/typo-in-norepeat-expr-2.rs | 42 --------------- .../ui/macros/typo-in-norepeat-expr-2.stderr | 46 ---------------- tests/ui/macros/typo-in-norepeat-expr.fixed | 12 ----- tests/ui/macros/typo-in-norepeat-expr.rs | 12 ----- tests/ui/macros/typo-in-norepeat-expr.stderr | 18 ------- 9 files changed, 6 insertions(+), 239 deletions(-) delete mode 100644 tests/ui/macros/typo-in-norepeat-expr-2.rs delete mode 100644 tests/ui/macros/typo-in-norepeat-expr-2.stderr delete mode 100644 tests/ui/macros/typo-in-norepeat-expr.fixed delete mode 100644 tests/ui/macros/typo-in-norepeat-expr.rs delete mode 100644 tests/ui/macros/typo-in-norepeat-expr.stderr diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index 6315670b499ef..476be94efd9e4 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -277,16 +277,7 @@ impl<'cx> MacroExpanderResult<'cx> { // Emit the SEMICOLON_IN_EXPRESSIONS_FROM_MACROS deprecation lint. let is_local = true; - let parser = ParserAnyMacro::from_tts( - cx, - tts, - site_span, - arm_span, - is_local, - macro_ident, - vec![], - vec![], - ); + let parser = ParserAnyMacro::from_tts(cx, tts, site_span, arm_span, is_local, macro_ident); ExpandResult::Ready(Box::new(parser)) } } diff --git a/compiler/rustc_expand/src/mbe/diagnostics.rs b/compiler/rustc_expand/src/mbe/diagnostics.rs index a689ae16ab224..df6903dc4937f 100644 --- a/compiler/rustc_expand/src/mbe/diagnostics.rs +++ b/compiler/rustc_expand/src/mbe/diagnostics.rs @@ -222,13 +222,11 @@ impl<'dcx> CollectTrackerAndEmitter<'dcx, '_> { pub(super) fn emit_frag_parse_err( mut e: Diag<'_>, - parser: &mut Parser<'_>, + parser: &Parser<'_>, orig_parser: &mut Parser<'_>, site_span: Span, arm_span: Span, kind: AstFragmentKind, - bindings: Vec, - matched_rule_bindings: Vec, ) -> ErrorGuaranteed { // FIXME(davidtwco): avoid depending on the error message text if parser.token == token::Eof @@ -287,54 +285,6 @@ pub(super) fn emit_frag_parse_err( }, _ => annotate_err_with_kind(&mut e, kind, site_span), }; - - let matched_rule_bindings_names: Vec<_> = - matched_rule_bindings.iter().map(|bind| bind.name).collect(); - let bindings_name: Vec<_> = bindings.iter().map(|bind| bind.name).collect(); - if parser.token.kind == token::Dollar { - parser.bump(); - if let token::Ident(name, _) = parser.token.kind { - if let Some(matched_name) = rustc_span::edit_distance::find_best_match_for_name( - &matched_rule_bindings_names[..], - name, - None, - ) { - e.span_suggestion_verbose( - parser.token.span, - "there is a macro metavariable with similar name", - format!("{matched_name}"), - Applicability::MaybeIncorrect, - ); - } else if bindings_name.contains(&name) { - e.span_label( - parser.token.span, - format!( - "there is an macro metavariable with this name in another macro matcher" - ), - ); - } else if let Some(matched_name) = - rustc_span::edit_distance::find_best_match_for_name(&bindings_name[..], name, None) - { - e.span_suggestion_verbose( - parser.token.span, - "there is a macro metavariable with a similar name in another macro matcher", - format!("{matched_name}"), - Applicability::MaybeIncorrect, - ); - } else { - let msg = matched_rule_bindings_names - .iter() - .map(|sym| format!("${}", sym)) - .collect::>() - .join(", "); - - e.span_label(parser.token.span, format!("macro metavariable not found")); - if !matched_rule_bindings_names.is_empty() { - e.note(format!("available metavariable names are: {msg}")); - } - } - } - } e.emit() } diff --git a/compiler/rustc_expand/src/mbe/macro_rules.rs b/compiler/rustc_expand/src/mbe/macro_rules.rs index 07ba34626ac02..7cd96211de508 100644 --- a/compiler/rustc_expand/src/mbe/macro_rules.rs +++ b/compiler/rustc_expand/src/mbe/macro_rules.rs @@ -56,8 +56,6 @@ pub(crate) struct ParserAnyMacro<'a> { arm_span: Span, /// Whether or not this macro is defined in the current crate is_local: bool, - bindings: Vec, - matched_rule_bindings: Vec, } impl<'a> ParserAnyMacro<'a> { @@ -70,22 +68,13 @@ impl<'a> ParserAnyMacro<'a> { arm_span, is_trailing_mac, is_local, - bindings, - matched_rule_bindings, } = *self; let snapshot = &mut parser.create_snapshot_for_diagnostic(); let fragment = match parse_ast_fragment(parser, kind) { Ok(f) => f, Err(err) => { let guar = diagnostics::emit_frag_parse_err( - err, - parser, - snapshot, - site_span, - arm_span, - kind, - bindings, - matched_rule_bindings, + err, parser, snapshot, site_span, arm_span, kind, ); return kind.dummy(site_span, guar); } @@ -120,9 +109,6 @@ impl<'a> ParserAnyMacro<'a> { arm_span: Span, is_local: bool, macro_ident: Ident, - // bindings and lhs is for diagnostics - bindings: Vec, - matched_rule_bindings: Vec, ) -> Self { Self { parser: Parser::new(&cx.sess.psess, tts, None), @@ -136,8 +122,6 @@ impl<'a> ParserAnyMacro<'a> { is_trailing_mac: cx.current_expansion.is_trailing_mac, arm_span, is_local, - bindings, - matched_rule_bindings, } } } @@ -376,7 +360,7 @@ fn expand_macro<'cx>( match try_success_result { Ok((rule_index, rule, named_matches)) => { - let MacroRule::Func { lhs, rhs, .. } = rule else { + let MacroRule::Func { rhs, .. } = rule else { panic!("try_match_macro returned non-func rule"); }; let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else { @@ -404,32 +388,8 @@ fn expand_macro<'cx>( cx.resolver.record_macro_rule_usage(node_id, rule_index); } - let mut bindings = vec![]; - for rule in rules { - let MacroRule::Func { lhs, .. } = rule else { continue }; - for param in lhs { - let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue }; - bindings.push(*bind); - } - } - - let mut matched_rule_bindings = vec![]; - for param in lhs { - let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue }; - matched_rule_bindings.push(*bind); - } - // Let the context choose how to interpret the result. Weird, but useful for X-macros. - Box::new(ParserAnyMacro::from_tts( - cx, - tts, - sp, - arm_span, - is_local, - name, - bindings, - matched_rule_bindings, - )) + Box::new(ParserAnyMacro::from_tts(cx, tts, sp, arm_span, is_local, name)) } Err(CanRetry::No(guar)) => { debug!("Will not retry matching as an error was emitted already"); diff --git a/tests/ui/macros/issue-6596-1.stderr b/tests/ui/macros/issue-6596-1.stderr index cb66dcc6ea890..f20d67329dbe7 100644 --- a/tests/ui/macros/issue-6596-1.stderr +++ b/tests/ui/macros/issue-6596-1.stderr @@ -2,15 +2,11 @@ error: expected expression, found `$` --> $DIR/issue-6596-1.rs:3:9 | LL | $nonexistent - | ^----------- - | || - | |macro metavariable not found - | expected expression + | ^^^^^^^^^^^^ expected expression ... LL | e!(foo); | ------- in this macro invocation | - = note: available metavariable names are: $inp = note: this error originates in the macro `e` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 1 previous error diff --git a/tests/ui/macros/typo-in-norepeat-expr-2.rs b/tests/ui/macros/typo-in-norepeat-expr-2.rs deleted file mode 100644 index 48b64c248d637..0000000000000 --- a/tests/ui/macros/typo-in-norepeat-expr-2.rs +++ /dev/null @@ -1,42 +0,0 @@ -macro_rules! err { - (begin $follow:ident end $arg:expr) => { - [$arg] - }; - (begin1 $arg1:ident end $agr2:expr) => { - [$follow] //~ ERROR: expected expression, found `$` - //~^ NOTE: there is an macro metavariable with this name in another macro matcher - //~| NOTE: expected expression - }; -} - -macro_rules! err1 { - (begin $follow:ident end $arg:expr) => { - [$arg] - }; - (begin1 $arg1:ident end) => { - [$follo] //~ ERROR: expected expression, found `$` - //~| NOTE: expected expression - //~| HELP: there is a macro metavariable with a similar name in another macro matcher - }; -} - -macro_rules! err2 { - (begin $follow:ident end $arg:expr) => { - [$arg] - }; - (begin1 $arg1:ident end) => { - [$xyz] //~ ERROR: expected expression, found `$` - //~^ NOTE: expected expression - //~| NOTE available metavariable names are: $arg1 - //~| NOTE: macro metavariable not found - }; -} - -fn main () { - let _ = err![begin1 x end ig]; //~ NOTE: in this expansion of err! - let _ = err1![begin1 x end]; //~ NOTE: in this expansion of err1! - //~| NOTE: in this expansion of err1! - - let _ = err2![begin1 x end]; //~ NOTE: in this expansion of err2! - //~| NOTE in this expansion of err2! -} diff --git a/tests/ui/macros/typo-in-norepeat-expr-2.stderr b/tests/ui/macros/typo-in-norepeat-expr-2.stderr deleted file mode 100644 index 20390cccc9a74..0000000000000 --- a/tests/ui/macros/typo-in-norepeat-expr-2.stderr +++ /dev/null @@ -1,46 +0,0 @@ -error: expected expression, found `$` - --> $DIR/typo-in-norepeat-expr-2.rs:6:10 - | -LL | [$follow] - | ^------ - | || - | |there is an macro metavariable with this name in another macro matcher - | expected expression -... -LL | let _ = err![begin1 x end ig]; - | ---------------------- in this macro invocation - | - = note: this error originates in the macro `err` (in Nightly builds, run with -Z macro-backtrace for more info) - -error: expected expression, found `$` - --> $DIR/typo-in-norepeat-expr-2.rs:17:10 - | -LL | [$follo] - | ^^^^^^ expected expression -... -LL | let _ = err1![begin1 x end]; - | -------------------- in this macro invocation - | - = note: this error originates in the macro `err1` (in Nightly builds, run with -Z macro-backtrace for more info) -help: there is a macro metavariable with a similar name in another macro matcher - | -LL | [$follow] - | + - -error: expected expression, found `$` - --> $DIR/typo-in-norepeat-expr-2.rs:28:10 - | -LL | [$xyz] - | ^--- - | || - | |macro metavariable not found - | expected expression -... -LL | let _ = err2![begin1 x end]; - | -------------------- in this macro invocation - | - = note: available metavariable names are: $arg1 - = note: this error originates in the macro `err2` (in Nightly builds, run with -Z macro-backtrace for more info) - -error: aborting due to 3 previous errors - diff --git a/tests/ui/macros/typo-in-norepeat-expr.fixed b/tests/ui/macros/typo-in-norepeat-expr.fixed deleted file mode 100644 index a59f461e63120..0000000000000 --- a/tests/ui/macros/typo-in-norepeat-expr.fixed +++ /dev/null @@ -1,12 +0,0 @@ -//@ run-rustfix -macro_rules! m { - (begin $ard:ident end) => { - [$ard] //~ ERROR: expected expression, found `$` - //~^ HELP: there is a macro metavariable with similar name - }; -} - -fn main() { - let x = 1; - let _ = m![begin x end]; -} diff --git a/tests/ui/macros/typo-in-norepeat-expr.rs b/tests/ui/macros/typo-in-norepeat-expr.rs deleted file mode 100644 index fe554f07e7552..0000000000000 --- a/tests/ui/macros/typo-in-norepeat-expr.rs +++ /dev/null @@ -1,12 +0,0 @@ -//@ run-rustfix -macro_rules! m { - (begin $ard:ident end) => { - [$arg] //~ ERROR: expected expression, found `$` - //~^ HELP: there is a macro metavariable with similar name - }; -} - -fn main() { - let x = 1; - let _ = m![begin x end]; -} diff --git a/tests/ui/macros/typo-in-norepeat-expr.stderr b/tests/ui/macros/typo-in-norepeat-expr.stderr deleted file mode 100644 index 8f37957ea98e0..0000000000000 --- a/tests/ui/macros/typo-in-norepeat-expr.stderr +++ /dev/null @@ -1,18 +0,0 @@ -error: expected expression, found `$` - --> $DIR/typo-in-norepeat-expr.rs:4:10 - | -LL | [$arg] - | ^^^^ expected expression -... -LL | let _ = m![begin x end]; - | --------------- in this macro invocation - | - = note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info) -help: there is a macro metavariable with similar name - | -LL - [$arg] -LL + [$ard] - | - -error: aborting due to 1 previous error -