diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 7e5f1d97a8bf4..50bc4d97437c8 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -2422,10 +2422,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { { expr = expr_ } + let expr_span = expr.span.find_oldest_ancestor_in_same_ctxt(); vec![ - (expr.span.shrink_to_lo(), format!("{prefix}{variant}{open}")), - (expr.span.shrink_to_hi(), close.to_owned()), + (expr_span.shrink_to_lo(), format!("{prefix}{variant}{open}")), + (expr_span.shrink_to_hi(), close.to_owned()), ] }; 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..36d0215686477 --- /dev/null +++ b/tests/ui/typeck/suggestions/suggest-compatible-variants-macro-issue-142359.rs @@ -0,0 +1,11 @@ +// Make sure we don't suggest compatible variants inside macro. (issue #142359) +use std::ops::ControlFlow; + +fn main(){ + let x: Result = Err(1); + + let v= match x { + Err(r) => ControlFlow::Break(r), + Ok(r) => { println!("A")} //~ 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..87725ab57cb5f --- /dev/null +++ b/tests/ui/typeck/suggestions/suggest-compatible-variants-macro-issue-142359.stderr @@ -0,0 +1,23 @@ +error[E0308]: `match` arms have incompatible types + --> $DIR/suggest-compatible-variants-macro-issue-142359.rs:9:20 + | +LL | let v= match x { + | ____________- +LL | | Err(r) => ControlFlow::Break(r), + | | --------------------- this is found to be of type `ControlFlow` +LL | | Ok(r) => { println!("A")} + | | ^^^^^^^^^^^^^ expected `ControlFlow`, found `()` +LL | | }; + | |_____- `match` arms have incompatible types + | + = note: expected enum `ControlFlow` + found unit type `()` + = note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) +help: try wrapping the expression in `std::ops::ControlFlow::Continue` + | +LL | Ok(r) => { std::ops::ControlFlow::Continue(println!("A"))} + | ++++++++++++++++++++++++++++++++ + + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0308`.