Use verbose suggestion for break without value

This commit is contained in:
Esteban Küber 2023-09-25 22:10:08 +00:00
parent 3747ef5d6f
commit ddb3b7e70a
7 changed files with 76 additions and 56 deletions

View file

@ -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);
}
}

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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<usize>` into an iterator in constants
--> $DIR/issue-52443.rs:9:21

View file

@ -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