Tweak mismatched types error on break expressions

This commit is contained in:
Esteban Küber 2019-08-06 14:20:39 -07:00
parent 813a3a5d4b
commit c076392143
5 changed files with 57 additions and 22 deletions

View File

@ -548,7 +548,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
coerce.coerce(self, &cause, e, e_ty);
} else {
assert!(e_ty.is_unit());
coerce.coerce_forced_unit(self, &cause, &mut |_| (), true);
let ty = coerce.expected_ty();
coerce.coerce_forced_unit(self, &cause, &mut |err| {
let msg = "give it a value of the expected type";
let label = destination.label
.map(|l| format!(" {}", l.ident))
.unwrap_or_else(String::new);
let sugg = format!("break{} {}", label, match ty.sty {
ty::Bool => "true",
ty::Char => "'a'",
ty::Int(_) | ty::Uint(_) => "42",
ty::Float(_) => "3.14159",
_ => "value",
});
err.span_suggestion(expr.span, msg, sugg, Applicability::HasPlaceholders);
}, false);
}
} else {
// If `ctxt.coerce` is `None`, we can just ignore

View File

@ -12,10 +12,13 @@ error[E0308]: mismatched types
--> $DIR/issue-27042.rs:6:16
|
LL | loop { break };
| ^^^^^ expected (), found i32
| ^^^^^
| |
| expected i32, found ()
| help: give it a value of the expected type: `break 42`
|
= note: expected type `()`
found type `i32`
= note: expected type `i32`
found type `()`
error[E0308]: mismatched types
--> $DIR/issue-27042.rs:8:9

View File

@ -90,10 +90,13 @@ error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:4:31
|
LL | let val: ! = loop { break break; };
| ^^^^^ expected (), found !
| ^^^^^
| |
| expected !, found ()
| help: give it a value of the expected type: `break value`
|
= note: expected type `()`
found type `!`
= note: expected type `!`
found type `()`
error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:11:19
@ -153,10 +156,13 @@ error[E0308]: mismatched types
--> $DIR/loop-break-value.rs:90:9
|
LL | break;
| ^^^^^ expected (), found integer
| ^^^^^
| |
| expected integer, found ()
| help: give it a value of the expected type: `break value`
|
= note: expected type `()`
found type `{integer}`
= note: expected type `{integer}`
found type `()`
error: aborting due to 16 previous errors

View File

@ -2,28 +2,37 @@ error[E0308]: mismatched types
--> $DIR/loop-labeled-break-value.rs:3:29
|
LL | let _: i32 = loop { break };
| ^^^^^ expected (), found i32
| ^^^^^
| |
| expected i32, found ()
| help: give it a value of the expected type: `break 42`
|
= note: expected type `()`
found type `i32`
= note: expected type `i32`
found type `()`
error[E0308]: mismatched types
--> $DIR/loop-labeled-break-value.rs:6:37
|
LL | let _: i32 = 'inner: loop { break 'inner };
| ^^^^^^^^^^^^ expected (), found i32
| ^^^^^^^^^^^^
| |
| expected i32, found ()
| help: give it a value of the expected type: `break 'inner 42`
|
= note: expected type `()`
found type `i32`
= note: expected type `i32`
found type `()`
error[E0308]: mismatched types
--> $DIR/loop-labeled-break-value.rs:9:45
|
LL | let _: i32 = 'inner2: loop { loop { break 'inner2 } };
| ^^^^^^^^^^^^^ expected (), found i32
| ^^^^^^^^^^^^^
| |
| expected i32, found ()
| help: give it a value of the expected type: `break 'inner2 42`
|
= note: expected type `()`
found type `i32`
= note: expected type `i32`
found type `()`
error: aborting due to 3 previous errors

View File

@ -2,10 +2,13 @@ error[E0308]: mismatched types
--> $DIR/loop-properly-diverging-2.rs:2:23
|
LL | let x: i32 = loop { break };
| ^^^^^ expected (), found i32
| ^^^^^
| |
| expected i32, found ()
| help: give it a value of the expected type: `break 42`
|
= note: expected type `()`
found type `i32`
= note: expected type `i32`
found type `()`
error: aborting due to previous error