From 08ec41ea3dfd2d30a5747a084d0dbc79fba227d6 Mon Sep 17 00:00:00 2001 From: im-lunex Date: Thu, 20 Aug 2026 22:45:47 +0600 Subject: [PATCH] avoid overlapping const suggestions --- compiler/rustc_parse/src/parser/pat.rs | 24 ++++++++++--------- .../recover-pat-range-macro-span-overlap.rs | 15 ++++++++++++ ...ecover-pat-range-macro-span-overlap.stderr | 20 ++++++++++++++++ 3 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 tests/ui/parser/recover/recover-pat-range-macro-span-overlap.rs create mode 100644 tests/ui/parser/recover/recover-pat-range-macro-span-overlap.stderr diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index 266a2d13419..f40421a3197 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -628,17 +628,19 @@ impl<'a> Parser<'a> { } // help: extract the expr into a `const VAL: _ = expr` - let ident = match self.field { - Some(field) => field.ident.as_str().to_uppercase(), - None => "VAL".to_owned(), - }; - err.subdiagnostic(UnexpectedExpressionInPatternSugg::Const { - stmt_lo: line_lo, - ident_span: expr_span, - expr, - ident, - indentation, - }); + if !line_lo.overlaps(expr_span) { + let ident = match self.field { + Some(field) => field.ident.as_str().to_uppercase(), + None => "VAL".to_owned(), + }; + err.subdiagnostic(UnexpectedExpressionInPatternSugg::Const { + stmt_lo: line_lo, + ident_span: expr_span, + expr, + ident, + indentation, + }); + } }, ); } diff --git a/tests/ui/parser/recover/recover-pat-range-macro-span-overlap.rs b/tests/ui/parser/recover/recover-pat-range-macro-span-overlap.rs new file mode 100644 index 00000000000..6b237e94d8f --- /dev/null +++ b/tests/ui/parser/recover/recover-pat-range-macro-span-overlap.rs @@ -0,0 +1,15 @@ +// test for issue - https://github.com/rust-lang/rust/issues/161213 +macro_rules! m { + ($value:expr) => { + enum Foo { + Bar = $value, + //~^ error: expected a pattern range bound, found an expression + } + fn main() { + match 0 { + 0..Foo::Bar + $value | _ => (), + } + } + }; +} +m!(1); diff --git a/tests/ui/parser/recover/recover-pat-range-macro-span-overlap.stderr b/tests/ui/parser/recover/recover-pat-range-macro-span-overlap.stderr new file mode 100644 index 00000000000..901a327980e --- /dev/null +++ b/tests/ui/parser/recover/recover-pat-range-macro-span-overlap.stderr @@ -0,0 +1,20 @@ +error: expected a pattern range bound, found an expression + --> $DIR/recover-pat-range-macro-span-overlap.rs:5:19 + | +LL | Bar = $value, + | ___________________^ +LL | | +LL | | } +LL | | fn main() { +LL | | match 0 { +LL | | 0..Foo::Bar + $value | _ => (), + | |_____________________________^ not a pattern +... +LL | m!(1); + | ----- in this macro invocation + | + = note: arbitrary expressions are not allowed in patterns: + = note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 1 previous error +