diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index fcf21c61d8fd9..f7e4ace8fc5fc 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -688,12 +688,26 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { let msg = "`match` arms have incompatible types"; err.span_label(outer_error_span, msg); if let Some((sp, boxed)) = semi_span { - if matches!(boxed, StatementAsExpression::NeedsBoxing) { - err.span_suggestion_verbose( + if let (StatementAsExpression::NeedsBoxing, [.., prior_arm]) = + (boxed, &prior_arms[..]) + { + err.multipart_suggestion( + "consider removing this semicolon and boxing the expressions", + vec![ + (prior_arm.shrink_to_lo(), "Box::new(".to_string()), + (prior_arm.shrink_to_hi(), ")".to_string()), + (arm_span.shrink_to_lo(), "Box::new(".to_string()), + (arm_span.shrink_to_hi(), ")".to_string()), + (sp, String::new()), + ], + Applicability::HasPlaceholders, + ); + } else if matches!(boxed, StatementAsExpression::NeedsBoxing) { + err.span_suggestion_short( sp, - "consider removing this semicolon and boxing the expression", + "consider removing this semicolon and boxing the expressions", String::new(), - Applicability::HasPlaceholders, + Applicability::MachineApplicable, ); } else { err.span_suggestion_short( @@ -727,11 +741,16 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { } if let Some((sp, boxed)) = semicolon { if matches!(boxed, StatementAsExpression::NeedsBoxing) { - err.span_suggestion_verbose( - sp, + err.multipart_suggestion( "consider removing this semicolon and boxing the expression", - String::new(), - Applicability::HasPlaceholders, + vec![ + (then.shrink_to_lo(), "Box::new(".to_string()), + (then.shrink_to_hi(), ")".to_string()), + (else_sp.shrink_to_lo(), "Box::new(".to_string()), + (else_sp.shrink_to_hi(), ")".to_string()), + (sp, String::new()), + ], + Applicability::MachineApplicable, ); } else { err.span_suggestion_short( diff --git a/src/test/ui/suggestions/match-prev-arm-needing-semi.stderr b/src/test/ui/suggestions/match-prev-arm-needing-semi.stderr index 7a4f74a1994ce..e9803a78f94b3 100644 --- a/src/test/ui/suggestions/match-prev-arm-needing-semi.stderr +++ b/src/test/ui/suggestions/match-prev-arm-needing-semi.stderr @@ -24,10 +24,13 @@ help: consider `await`ing on the `Future` | LL | false => async_dummy().await, | ^^^^^^ -help: consider removing this semicolon and boxing the expression +help: consider removing this semicolon and boxing the expressions + | +LL | Box::new(async_dummy()) +LL | +LL | } +LL | false => Box::new(async_dummy()), | -LL | async_dummy() - | -- error[E0308]: `match` arms have incompatible types --> $DIR/match-prev-arm-needing-semi.rs:39:18 @@ -55,10 +58,13 @@ help: consider `await`ing on the `Future` | LL | false => async_dummy2().await, | ^^^^^^ -help: consider removing this semicolon and boxing the expression +help: consider removing this semicolon and boxing the expressions + | +LL | Box::new(async_dummy()) +LL | +LL | } +LL | false => Box::new(async_dummy2()), | -LL | async_dummy() - | -- error[E0308]: `match` arms have incompatible types --> $DIR/match-prev-arm-needing-semi.rs:50:18 @@ -70,10 +76,10 @@ LL | let _ = match true { | _____________- LL | | true => async_dummy(), | | ------------- this is found to be of type `impl Future` +LL | | LL | | false => async_dummy2(), | | ^^^^^^^^^^^^^^ expected opaque type, found a different opaque type -LL | | -LL | | +... | LL | | LL | | }; | |_____- `match` arms have incompatible types @@ -84,6 +90,7 @@ LL | | }; help: consider `await`ing on both `Future`s | LL | true => async_dummy().await, +LL | LL | false => async_dummy2().await, |