avoid overlapping const suggestions

This commit is contained in:
im-lunex
2026-08-20 22:45:47 +06:00
parent f7d782a3be
commit 08ec41ea3d
3 changed files with 48 additions and 11 deletions

View File

@@ -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,
});
}
},
);
}

View File

@@ -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);

View File

@@ -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: <https://doc.rust-lang.org/book/ch19-00-patterns.html>
= 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