Skip to content

Commit ddb3b7e

Browse files
committed
Use verbose suggestion for break without value
1 parent 3747ef5 commit ddb3b7e

File tree

7 files changed

+76
-56
lines changed

7 files changed

+76
-56
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -670,14 +670,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
670670
let error = Some(Sorts(ExpectedFound { expected: ty, found: e_ty }));
671671
self.annotate_loop_expected_due_to_inference(&mut err, expr, error);
672672
if let Some(val) = ty_kind_suggestion(ty) {
673-
let label = destination
674-
.label
675-
.map(|l| format!(" {}", l.ident))
676-
.unwrap_or_else(String::new);
677-
err.span_suggestion(
678-
expr.span,
673+
err.span_suggestion_verbose(
674+
expr.span.shrink_to_hi(),
679675
"give it a value of the expected type",
680-
format!("break{label} {val}"),
676+
format!(" {val}"),
681677
Applicability::HasPlaceholders,
682678
);
683679
}
@@ -722,7 +718,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
722718
// ... except when we try to 'break rust;'.
723719
// ICE this expression in particular (see #43162).
724720
if let ExprKind::Path(QPath::Resolved(_, path)) = e.kind {
725-
if path.segments.len() == 1 && path.segments[0].ident.name == sym::rust {
721+
if let [segment] = path.segments && segment.ident.name == sym::rust {
726722
fatally_break_rust(self.tcx);
727723
}
728724
}

tests/ui/issues/issue-27042.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ error[E0308]: mismatched types
1212
--> $DIR/issue-27042.rs:6:16
1313
|
1414
LL | loop { break };
15-
| ^^^^^
16-
| |
17-
| expected `i32`, found `()`
18-
| help: give it a value of the expected type: `break 42`
15+
| ^^^^^ expected `i32`, found `()`
16+
|
17+
help: give it a value of the expected type
18+
|
19+
LL | loop { break 42 };
20+
| ++
1921

2022
error[E0308]: mismatched types
2123
--> $DIR/issue-27042.rs:8:9

tests/ui/loops/loop-break-value.stderr

+30-20
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,12 @@ error[E0308]: mismatched types
225225
LL | break 2;
226226
| ------- expected because of this `break`
227227
LL | break;
228-
| ^^^^^
229-
| |
230-
| expected integer, found `()`
231-
| help: give it a value of the expected type: `break value`
228+
| ^^^^^ expected integer, found `()`
229+
|
230+
help: give it a value of the expected type
231+
|
232+
LL | break value;
233+
| +++++
232234

233235
error[E0308]: mismatched types
234236
--> $DIR/loop-break-value.rs:108:9
@@ -237,10 +239,12 @@ LL | break 'a 1;
237239
| ---------- expected because of this `break`
238240
...
239241
LL | break;
240-
| ^^^^^
241-
| |
242-
| expected integer, found `()`
243-
| help: give it a value of the expected type: `break value`
242+
| ^^^^^ expected integer, found `()`
243+
|
244+
help: give it a value of the expected type
245+
|
246+
LL | break value;
247+
| +++++
244248

245249
error[E0308]: mismatched types
246250
--> $DIR/loop-break-value.rs:120:9
@@ -249,10 +253,12 @@ LL | break 'a 1;
249253
| ---------- expected because of this `break`
250254
...
251255
LL | break 'a;
252-
| ^^^^^^^^
253-
| |
254-
| expected integer, found `()`
255-
| help: give it a value of the expected type: `break 'a value`
256+
| ^^^^^^^^ expected integer, found `()`
257+
|
258+
help: give it a value of the expected type
259+
|
260+
LL | break 'a value;
261+
| +++++
256262

257263
error[E0308]: mismatched types
258264
--> $DIR/loop-break-value.rs:131:15
@@ -270,10 +276,12 @@ LL | break 2;
270276
| ------- expected because of this `break`
271277
LL | loop {
272278
LL | break 'a;
273-
| ^^^^^^^^
274-
| |
275-
| expected integer, found `()`
276-
| help: give it a value of the expected type: `break 'a value`
279+
| ^^^^^^^^ expected integer, found `()`
280+
|
281+
help: give it a value of the expected type
282+
|
283+
LL | break 'a value;
284+
| +++++
277285

278286
error[E0308]: mismatched types
279287
--> $DIR/loop-break-value.rs:143:15
@@ -291,10 +299,12 @@ LL | break 'a 2;
291299
| ---------- expected because of this `break`
292300
LL | loop {
293301
LL | break 'a;
294-
| ^^^^^^^^
295-
| |
296-
| expected integer, found `()`
297-
| help: give it a value of the expected type: `break 'a value`
302+
| ^^^^^^^^ expected integer, found `()`
303+
|
304+
help: give it a value of the expected type
305+
|
306+
LL | break 'a value;
307+
| +++++
298308

299309
error[E0308]: mismatched types
300310
--> $DIR/loop-break-value.rs:155:15

tests/ui/loops/loop-labeled-break-value.stderr

+18-12
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,34 @@ error[E0308]: mismatched types
22
--> $DIR/loop-labeled-break-value.rs:3:29
33
|
44
LL | let _: i32 = loop { break };
5-
| ^^^^^
6-
| |
7-
| expected `i32`, found `()`
8-
| help: give it a value of the expected type: `break 42`
5+
| ^^^^^ expected `i32`, found `()`
6+
|
7+
help: give it a value of the expected type
8+
|
9+
LL | let _: i32 = loop { break 42 };
10+
| ++
911

1012
error[E0308]: mismatched types
1113
--> $DIR/loop-labeled-break-value.rs:6:37
1214
|
1315
LL | let _: i32 = 'inner: loop { break 'inner };
14-
| ^^^^^^^^^^^^
15-
| |
16-
| expected `i32`, found `()`
17-
| help: give it a value of the expected type: `break 'inner 42`
16+
| ^^^^^^^^^^^^ expected `i32`, found `()`
17+
|
18+
help: give it a value of the expected type
19+
|
20+
LL | let _: i32 = 'inner: loop { break 'inner 42 };
21+
| ++
1822

1923
error[E0308]: mismatched types
2024
--> $DIR/loop-labeled-break-value.rs:9:45
2125
|
2226
LL | let _: i32 = 'inner2: loop { loop { break 'inner2 } };
23-
| ^^^^^^^^^^^^^
24-
| |
25-
| expected `i32`, found `()`
26-
| help: give it a value of the expected type: `break 'inner2 42`
27+
| ^^^^^^^^^^^^^ expected `i32`, found `()`
28+
|
29+
help: give it a value of the expected type
30+
|
31+
LL | let _: i32 = 'inner2: loop { loop { break 'inner2 42 } };
32+
| ++
2733

2834
error: aborting due to 3 previous errors
2935

tests/ui/loops/loop-properly-diverging-2.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ error[E0308]: mismatched types
22
--> $DIR/loop-properly-diverging-2.rs:2:23
33
|
44
LL | let x: i32 = loop { break };
5-
| ^^^^^
6-
| |
7-
| expected `i32`, found `()`
8-
| help: give it a value of the expected type: `break 42`
5+
| ^^^^^ expected `i32`, found `()`
6+
|
7+
help: give it a value of the expected type
8+
|
9+
LL | let x: i32 = loop { break 42 };
10+
| ++
911

1012
error: aborting due to previous error
1113

tests/ui/never_type/issue-52443.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ error[E0308]: mismatched types
3333
--> $DIR/issue-52443.rs:4:17
3434
|
3535
LL | [(); loop { break }];
36-
| ^^^^^
37-
| |
38-
| expected `usize`, found `()`
39-
| help: give it a value of the expected type: `break 42`
36+
| ^^^^^ expected `usize`, found `()`
37+
|
38+
help: give it a value of the expected type
39+
|
40+
LL | [(); loop { break 42 }];
41+
| ++
4042

4143
error[E0015]: cannot convert `RangeFrom<usize>` into an iterator in constants
4244
--> $DIR/issue-52443.rs:9:21

tests/ui/type/type-error-break-tail.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ LL | fn loop_ending() -> i32 {
66
LL | loop {
77
| ---- this loop is expected to be of type `i32`
88
LL | if false { break; }
9-
| ^^^^^
10-
| |
11-
| expected `i32`, found `()`
12-
| help: give it a value of the expected type: `break 42`
9+
| ^^^^^ expected `i32`, found `()`
10+
|
11+
help: give it a value of the expected type
12+
|
13+
LL | if false { break 42; }
14+
| ++
1315

1416
error: aborting due to previous error
1517

0 commit comments

Comments
 (0)