Skip to content

Commit 9cb664c

Browse files
committed
Tweak mismatched types error on break expressions
1 parent f01b9f8 commit 9cb664c

File tree

5 files changed

+57
-22
lines changed

5 files changed

+57
-22
lines changed

src/librustc_typeck/check/expr.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
548548
coerce.coerce(self, &cause, e, e_ty);
549549
} else {
550550
assert!(e_ty.is_unit());
551-
coerce.coerce_forced_unit(self, &cause, &mut |_| (), true);
551+
let ty = coerce.expected_ty();
552+
coerce.coerce_forced_unit(self, &cause, &mut |err| {
553+
let msg = "give it a value of the expected type";
554+
let label = destination.label
555+
.map(|l| format!(" {}", l.ident))
556+
.unwrap_or_else(String::new);
557+
let sugg = format!("break{} {}", label, match ty.sty {
558+
ty::Bool => "true",
559+
ty::Char => "'a'",
560+
ty::Int(_) | ty::Uint(_) => "42",
561+
ty::Float(_) => "3.14159",
562+
_ => "value",
563+
});
564+
err.span_suggestion(expr.span, msg, sugg, Applicability::HasPlaceholders);
565+
}, false);
552566
}
553567
} else {
554568
// If `ctxt.coerce` is `None`, we can just ignore

src/test/ui/issues/issue-27042.stderr

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ error[E0308]: mismatched types
1212
--> $DIR/issue-27042.rs:6:16
1313
|
1414
LL | loop { break };
15-
| ^^^^^ expected (), found i32
15+
| ^^^^^
16+
| |
17+
| expected i32, found ()
18+
| help: give it a value of the expected type: `break 42`
1619
|
17-
= note: expected type `()`
18-
found type `i32`
20+
= note: expected type `i32`
21+
found type `()`
1922

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

src/test/ui/loops/loop-break-value.stderr

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@ error[E0308]: mismatched types
9090
--> $DIR/loop-break-value.rs:4:31
9191
|
9292
LL | let val: ! = loop { break break; };
93-
| ^^^^^ expected (), found !
93+
| ^^^^^
94+
| |
95+
| expected !, found ()
96+
| help: give it a value of the expected type: `break value`
9497
|
95-
= note: expected type `()`
96-
found type `!`
98+
= note: expected type `!`
99+
found type `()`
97100

98101
error[E0308]: mismatched types
99102
--> $DIR/loop-break-value.rs:11:19
@@ -153,10 +156,13 @@ error[E0308]: mismatched types
153156
--> $DIR/loop-break-value.rs:90:9
154157
|
155158
LL | break;
156-
| ^^^^^ expected (), found integer
159+
| ^^^^^
160+
| |
161+
| expected integer, found ()
162+
| help: give it a value of the expected type: `break value`
157163
|
158-
= note: expected type `()`
159-
found type `{integer}`
164+
= note: expected type `{integer}`
165+
found type `()`
160166

161167
error: aborting due to 16 previous errors
162168

src/test/ui/loops/loop-labeled-break-value.stderr

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

1013
error[E0308]: mismatched types
1114
--> $DIR/loop-labeled-break-value.rs:6:37
1215
|
1316
LL | let _: i32 = 'inner: loop { break 'inner };
14-
| ^^^^^^^^^^^^ expected (), found i32
17+
| ^^^^^^^^^^^^
18+
| |
19+
| expected i32, found ()
20+
| help: give it a value of the expected type: `break 'inner 42`
1521
|
16-
= note: expected type `()`
17-
found type `i32`
22+
= note: expected type `i32`
23+
found type `()`
1824

1925
error[E0308]: mismatched types
2026
--> $DIR/loop-labeled-break-value.rs:9:45
2127
|
2228
LL | let _: i32 = 'inner2: loop { loop { break 'inner2 } };
23-
| ^^^^^^^^^^^^^ expected (), found i32
29+
| ^^^^^^^^^^^^^
30+
| |
31+
| expected i32, found ()
32+
| help: give it a value of the expected type: `break 'inner2 42`
2433
|
25-
= note: expected type `()`
26-
found type `i32`
34+
= note: expected type `i32`
35+
found type `()`
2736

2837
error: aborting due to 3 previous errors
2938

src/test/ui/loops/loop-properly-diverging-2.stderr

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

1013
error: aborting due to previous error
1114

0 commit comments

Comments
 (0)