From 74760ffec4463ce10ac0dd521e5eff16d5a00d24 Mon Sep 17 00:00:00 2001 From: xizheyin Date: Sun, 24 May 2026 20:29:16 +0800 Subject: [PATCH 1/2] Add suggest-compatible-variants-macro-issue-142359 --- ...-compatible-variants-macro-issue-142359.rs | 6 +++++ ...-compatible-variants-macro-issue-142359.rs | 16 ++++++++++++ ...patible-variants-macro-issue-142359.stderr | 25 +++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 tests/ui/typeck/suggestions/auxiliary/suggest-compatible-variants-macro-issue-142359.rs create mode 100644 tests/ui/typeck/suggestions/suggest-compatible-variants-macro-issue-142359.rs create mode 100644 tests/ui/typeck/suggestions/suggest-compatible-variants-macro-issue-142359.stderr diff --git a/tests/ui/typeck/suggestions/auxiliary/suggest-compatible-variants-macro-issue-142359.rs b/tests/ui/typeck/suggestions/auxiliary/suggest-compatible-variants-macro-issue-142359.rs new file mode 100644 index 0000000000000..a94f6244b17e7 --- /dev/null +++ b/tests/ui/typeck/suggestions/auxiliary/suggest-compatible-variants-macro-issue-142359.rs @@ -0,0 +1,6 @@ +#[macro_export] +macro_rules! unit { + () => {{ + () + }}; +} diff --git a/tests/ui/typeck/suggestions/suggest-compatible-variants-macro-issue-142359.rs b/tests/ui/typeck/suggestions/suggest-compatible-variants-macro-issue-142359.rs new file mode 100644 index 0000000000000..f84ebd6f9ea50 --- /dev/null +++ b/tests/ui/typeck/suggestions/suggest-compatible-variants-macro-issue-142359.rs @@ -0,0 +1,16 @@ +// Make sure we don't suggest compatible variants cross macro context. (issue #142359) +//@ aux-crate:ext=suggest-compatible-variants-macro-issue-142359.rs + +extern crate ext; + +use std::ops::ControlFlow; + +fn main() { + let x: Result = Err(1); + + let _ = match x { + Err(r) => ControlFlow::Break(r), + Ok(r) => { ext::unit!() } //~ ERROR `match` arms have incompatible types [E0308] + + }; +} diff --git a/tests/ui/typeck/suggestions/suggest-compatible-variants-macro-issue-142359.stderr b/tests/ui/typeck/suggestions/suggest-compatible-variants-macro-issue-142359.stderr new file mode 100644 index 0000000000000..63c599d1431d0 --- /dev/null +++ b/tests/ui/typeck/suggestions/suggest-compatible-variants-macro-issue-142359.stderr @@ -0,0 +1,25 @@ +error[E0308]: `match` arms have incompatible types + --> $DIR/suggest-compatible-variants-macro-issue-142359.rs:13:19 + | +LL | let _ = match x { + | _____________- +LL | | Err(r) => ControlFlow::Break(r), + | | --------------------- this is found to be of type `ControlFlow` +LL | | Ok(r) => {ext::unit!()} + | | ^^^^^^^^^^^^ expected `ControlFlow`, found `()` +LL | | +LL | | }; + | |_____- `match` arms have incompatible types + | + = note: expected enum `ControlFlow` + found unit type `()` + = note: this error originates in the macro `ext::unit` (in Nightly builds, run with -Z macro-backtrace for more info) +help: try wrapping the expression in `std::ops::ControlFlow::Continue` + --> $DIR/auxiliary/suggest-compatible-variants-macro-issue-142359.rs:4:9 + | +LL | std::ops::ControlFlow::Continue(()) + | ++++++++++++++++++++++++++++++++ + + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0308`. From bbd3554304e61fb2af02c5a06f7e58a7d2c61771 Mon Sep 17 00:00:00 2001 From: xizheyin Date: Sun, 24 May 2026 20:37:20 +0800 Subject: [PATCH 2/2] Do not suggest compatible variants crossing macro contexts --- compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs | 7 ++++--- ...gest-compatible-variants-macro-issue-142359.stderr | 11 +++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 323f0fb040d6b..6a9a2065f4d31 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -2707,12 +2707,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Some(CtorKind::Const) => unreachable!("unit variants don't have fields"), }; - // Suggest constructor as deep into the block tree as possible. - // This fixes https://github.com/rust-lang/rust/issues/101065, - // and also just helps make the most minimal suggestions. + // Suggest constructor as deep into the block tree as possible, + // but don't cross macro contexts. This fixes #101065 while + // keeping suggestions out of macro definitions (#142359). let mut expr = expr; while let hir::ExprKind::Block(block, _) = &expr.kind && let Some(expr_) = &block.expr + && expr_.span.eq_ctxt(expr.span) { expr = expr_ } diff --git a/tests/ui/typeck/suggestions/suggest-compatible-variants-macro-issue-142359.stderr b/tests/ui/typeck/suggestions/suggest-compatible-variants-macro-issue-142359.stderr index 63c599d1431d0..7344060a91e4e 100644 --- a/tests/ui/typeck/suggestions/suggest-compatible-variants-macro-issue-142359.stderr +++ b/tests/ui/typeck/suggestions/suggest-compatible-variants-macro-issue-142359.stderr @@ -1,12 +1,12 @@ error[E0308]: `match` arms have incompatible types - --> $DIR/suggest-compatible-variants-macro-issue-142359.rs:13:19 + --> $DIR/suggest-compatible-variants-macro-issue-142359.rs:13:20 | LL | let _ = match x { | _____________- LL | | Err(r) => ControlFlow::Break(r), | | --------------------- this is found to be of type `ControlFlow` -LL | | Ok(r) => {ext::unit!()} - | | ^^^^^^^^^^^^ expected `ControlFlow`, found `()` +LL | | Ok(r) => { ext::unit!() } + | | ^^^^^^^^^^^^ expected `ControlFlow`, found `()` LL | | LL | | }; | |_____- `match` arms have incompatible types @@ -15,10 +15,9 @@ LL | | }; found unit type `()` = note: this error originates in the macro `ext::unit` (in Nightly builds, run with -Z macro-backtrace for more info) help: try wrapping the expression in `std::ops::ControlFlow::Continue` - --> $DIR/auxiliary/suggest-compatible-variants-macro-issue-142359.rs:4:9 | -LL | std::ops::ControlFlow::Continue(()) - | ++++++++++++++++++++++++++++++++ + +LL | Ok(r) => std::ops::ControlFlow::Continue({ ext::unit!() }) + | ++++++++++++++++++++++++++++++++ + error: aborting due to 1 previous error