diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 5fc8b1f9fbe..eead4da5e3e 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -670,14 +670,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let error = Some(Sorts(ExpectedFound { expected: ty, found: e_ty })); self.annotate_loop_expected_due_to_inference(&mut err, expr, error); if let Some(val) = ty_kind_suggestion(ty) { - let label = destination - .label - .map(|l| format!(" {}", l.ident)) - .unwrap_or_else(String::new); - err.span_suggestion( - expr.span, + err.span_suggestion_verbose( + expr.span.shrink_to_hi(), "give it a value of the expected type", - format!("break{label} {val}"), + format!(" {val}"), Applicability::HasPlaceholders, ); } @@ -722,7 +718,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // ... except when we try to 'break rust;'. // ICE this expression in particular (see #43162). if let ExprKind::Path(QPath::Resolved(_, path)) = e.kind { - if path.segments.len() == 1 && path.segments[0].ident.name == sym::rust { + if let [segment] = path.segments && segment.ident.name == sym::rust { fatally_break_rust(self.tcx); } } diff --git a/tests/ui/issues/issue-27042.stderr b/tests/ui/issues/issue-27042.stderr index 59ef28481d0..69eedeb8025 100644 --- a/tests/ui/issues/issue-27042.stderr +++ b/tests/ui/issues/issue-27042.stderr @@ -12,10 +12,12 @@ error[E0308]: mismatched types --> $DIR/issue-27042.rs:6:16 | LL | loop { break }; - | ^^^^^ - | | - | expected `i32`, found `()` - | help: give it a value of the expected type: `break 42` + | ^^^^^ expected `i32`, found `()` + | +help: give it a value of the expected type + | +LL | loop { break 42 }; + | ++ error[E0308]: mismatched types --> $DIR/issue-27042.rs:8:9 diff --git a/tests/ui/loops/loop-break-value.stderr b/tests/ui/loops/loop-break-value.stderr index 4c312c2fe38..6c83bc7575c 100644 --- a/tests/ui/loops/loop-break-value.stderr +++ b/tests/ui/loops/loop-break-value.stderr @@ -225,10 +225,12 @@ error[E0308]: mismatched types LL | break 2; | ------- expected because of this `break` LL | break; - | ^^^^^ - | | - | expected integer, found `()` - | help: give it a value of the expected type: `break value` + | ^^^^^ expected integer, found `()` + | +help: give it a value of the expected type + | +LL | break value; + | +++++ error[E0308]: mismatched types --> $DIR/loop-break-value.rs:108:9 @@ -237,10 +239,12 @@ LL | break 'a 1; | ---------- expected because of this `break` ... LL | break; - | ^^^^^ - | | - | expected integer, found `()` - | help: give it a value of the expected type: `break value` + | ^^^^^ expected integer, found `()` + | +help: give it a value of the expected type + | +LL | break value; + | +++++ error[E0308]: mismatched types --> $DIR/loop-break-value.rs:120:9 @@ -249,10 +253,12 @@ LL | break 'a 1; | ---------- expected because of this `break` ... LL | break 'a; - | ^^^^^^^^ - | | - | expected integer, found `()` - | help: give it a value of the expected type: `break 'a value` + | ^^^^^^^^ expected integer, found `()` + | +help: give it a value of the expected type + | +LL | break 'a value; + | +++++ error[E0308]: mismatched types --> $DIR/loop-break-value.rs:131:15 @@ -270,10 +276,12 @@ LL | break 2; | ------- expected because of this `break` LL | loop { LL | break 'a; - | ^^^^^^^^ - | | - | expected integer, found `()` - | help: give it a value of the expected type: `break 'a value` + | ^^^^^^^^ expected integer, found `()` + | +help: give it a value of the expected type + | +LL | break 'a value; + | +++++ error[E0308]: mismatched types --> $DIR/loop-break-value.rs:143:15 @@ -291,10 +299,12 @@ LL | break 'a 2; | ---------- expected because of this `break` LL | loop { LL | break 'a; - | ^^^^^^^^ - | | - | expected integer, found `()` - | help: give it a value of the expected type: `break 'a value` + | ^^^^^^^^ expected integer, found `()` + | +help: give it a value of the expected type + | +LL | break 'a value; + | +++++ error[E0308]: mismatched types --> $DIR/loop-break-value.rs:155:15 diff --git a/tests/ui/loops/loop-labeled-break-value.stderr b/tests/ui/loops/loop-labeled-break-value.stderr index aa04d330f25..f233670e8c4 100644 --- a/tests/ui/loops/loop-labeled-break-value.stderr +++ b/tests/ui/loops/loop-labeled-break-value.stderr @@ -2,28 +2,34 @@ error[E0308]: mismatched types --> $DIR/loop-labeled-break-value.rs:3:29 | LL | let _: i32 = loop { break }; - | ^^^^^ - | | - | expected `i32`, found `()` - | help: give it a value of the expected type: `break 42` + | ^^^^^ expected `i32`, found `()` + | +help: give it a value of the expected type + | +LL | let _: i32 = loop { break 42 }; + | ++ error[E0308]: mismatched types --> $DIR/loop-labeled-break-value.rs:6:37 | LL | let _: i32 = 'inner: loop { break 'inner }; - | ^^^^^^^^^^^^ - | | - | expected `i32`, found `()` - | help: give it a value of the expected type: `break 'inner 42` + | ^^^^^^^^^^^^ expected `i32`, found `()` + | +help: give it a value of the expected type + | +LL | let _: i32 = 'inner: loop { break 'inner 42 }; + | ++ error[E0308]: mismatched types --> $DIR/loop-labeled-break-value.rs:9:45 | LL | let _: i32 = 'inner2: loop { loop { break 'inner2 } }; - | ^^^^^^^^^^^^^ - | | - | expected `i32`, found `()` - | help: give it a value of the expected type: `break 'inner2 42` + | ^^^^^^^^^^^^^ expected `i32`, found `()` + | +help: give it a value of the expected type + | +LL | let _: i32 = 'inner2: loop { loop { break 'inner2 42 } }; + | ++ error: aborting due to 3 previous errors diff --git a/tests/ui/loops/loop-properly-diverging-2.stderr b/tests/ui/loops/loop-properly-diverging-2.stderr index 5030a2935b9..dc60657ee14 100644 --- a/tests/ui/loops/loop-properly-diverging-2.stderr +++ b/tests/ui/loops/loop-properly-diverging-2.stderr @@ -2,10 +2,12 @@ error[E0308]: mismatched types --> $DIR/loop-properly-diverging-2.rs:2:23 | LL | let x: i32 = loop { break }; - | ^^^^^ - | | - | expected `i32`, found `()` - | help: give it a value of the expected type: `break 42` + | ^^^^^ expected `i32`, found `()` + | +help: give it a value of the expected type + | +LL | let x: i32 = loop { break 42 }; + | ++ error: aborting due to previous error diff --git a/tests/ui/never_type/issue-52443.stderr b/tests/ui/never_type/issue-52443.stderr index 99dfce86903..59292ed68a3 100644 --- a/tests/ui/never_type/issue-52443.stderr +++ b/tests/ui/never_type/issue-52443.stderr @@ -33,10 +33,12 @@ error[E0308]: mismatched types --> $DIR/issue-52443.rs:4:17 | LL | [(); loop { break }]; - | ^^^^^ - | | - | expected `usize`, found `()` - | help: give it a value of the expected type: `break 42` + | ^^^^^ expected `usize`, found `()` + | +help: give it a value of the expected type + | +LL | [(); loop { break 42 }]; + | ++ error[E0015]: cannot convert `RangeFrom` into an iterator in constants --> $DIR/issue-52443.rs:9:21 diff --git a/tests/ui/type/type-error-break-tail.stderr b/tests/ui/type/type-error-break-tail.stderr index 4bae046de86..9a02bc28752 100644 --- a/tests/ui/type/type-error-break-tail.stderr +++ b/tests/ui/type/type-error-break-tail.stderr @@ -6,10 +6,12 @@ LL | fn loop_ending() -> i32 { LL | loop { | ---- this loop is expected to be of type `i32` LL | if false { break; } - | ^^^^^ - | | - | expected `i32`, found `()` - | help: give it a value of the expected type: `break 42` + | ^^^^^ expected `i32`, found `()` + | +help: give it a value of the expected type + | +LL | if false { break 42; } + | ++ error: aborting due to previous error