diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index dc204eb47aefa670a633712ddebc6e1e03eeafda..51aebfef8ace962380a5f79a67b76ebf04ab3563 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -829,7 +829,13 @@ fn non_exhaustive_match<'p, 'tcx>( } else { " ".to_string() }; - let comma = if matches!(only.body.kind, hir::ExprKind::Block(..)) { "" } else { "," }; + let comma = if matches!(only.body.kind, hir::ExprKind::Block(..)) + && only.span.ctxt() == only.body.span.ctxt() + { + "" + } else { + "," + }; suggestion = Some(( only.span.shrink_to_hi(), format!("{}{}{} => todo!()", comma, pre_indentation, pattern), @@ -837,8 +843,13 @@ fn non_exhaustive_match<'p, 'tcx>( } [.., prev, last] if prev.span.ctxt() == last.span.ctxt() => { if let Ok(snippet) = sm.span_to_snippet(prev.span.between(last.span)) { - let comma = - if matches!(last.body.kind, hir::ExprKind::Block(..)) { "" } else { "," }; + let comma = if matches!(last.body.kind, hir::ExprKind::Block(..)) + && last.span.ctxt() == last.body.span.ctxt() + { + "" + } else { + "," + }; suggestion = Some(( last.span.shrink_to_hi(), format!( diff --git a/src/test/ui/issue-94866.rs b/src/test/ui/issue-94866.rs new file mode 100644 index 0000000000000000000000000000000000000000..c42034879369232c258265e28c7ceee85c7a8d57 --- /dev/null +++ b/src/test/ui/issue-94866.rs @@ -0,0 +1,14 @@ +macro_rules! m { + () => { + {} + }; +} + +enum Enum { A, B } + +fn main() { + match Enum::A { + //~^ ERROR non-exhaustive patterns + Enum::A => m!() + } +} diff --git a/src/test/ui/issue-94866.stderr b/src/test/ui/issue-94866.stderr new file mode 100644 index 0000000000000000000000000000000000000000..5477d83f449209ee59375ff18db17253552a9e98 --- /dev/null +++ b/src/test/ui/issue-94866.stderr @@ -0,0 +1,21 @@ +error[E0004]: non-exhaustive patterns: `B` not covered + --> $DIR/issue-94866.rs:10:11 + | +LL | match Enum::A { + | ^^^^^^^ pattern `B` not covered + | +note: `Enum` defined here + --> $DIR/issue-94866.rs:7:16 + | +LL | enum Enum { A, B } + | ---- ^ not covered + = note: the matched value is of type `Enum` +help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown + | +LL ~ Enum::A => m!(), +LL + B => todo!() + | + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0004`. diff --git a/src/test/ui/pattern/usefulness/tuple-struct-nonexhaustive.stderr b/src/test/ui/pattern/usefulness/tuple-struct-nonexhaustive.stderr index fc0430d06fa1c03354a6875b6040d6793548f9b2..e2a65ff8524040062aefcaad59b284c07e11921a 100644 --- a/src/test/ui/pattern/usefulness/tuple-struct-nonexhaustive.stderr +++ b/src/test/ui/pattern/usefulness/tuple-struct-nonexhaustive.stderr @@ -12,7 +12,7 @@ LL | struct Foo(isize, isize); = note: the matched value is of type `Foo` help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | -LL ~ Foo(2, b) => println!("{}", b) +LL ~ Foo(2, b) => println!("{}", b), LL + Foo(_, _) => todo!() |