Skip to content

Commit 32e745f

Browse files
authored
Rollup merge of rust-lang#66101 - estebank:break-tail-e0308, r=Centril
Tweak type mismatch caused by break on tail expr When `break;` has a type mismatch because the `Destination` points at a tail expression with an obligation flowing from a return type, point at the return type. Fix rust-lang#39968.
2 parents 409b2bf + b0f258b commit 32e745f

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

src/librustc/traits/error_reporting.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
12311231
fn suggest_fn_call(
12321232
&self,
12331233
obligation: &PredicateObligation<'tcx>,
1234-
err: &mut DiagnosticBuilder<'tcx>,
1234+
err: &mut DiagnosticBuilder<'_>,
12351235
trait_ref: &ty::Binder<ty::TraitRef<'tcx>>,
12361236
points_at_arg: bool,
12371237
) {

src/librustc_typeck/check/demand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
553553

554554
pub fn check_for_cast(
555555
&self,
556-
err: &mut DiagnosticBuilder<'tcx>,
556+
err: &mut DiagnosticBuilder<'_>,
557557
expr: &hir::Expr,
558558
checked_ty: Ty<'tcx>,
559559
expected_ty: Ty<'tcx>,

src/librustc_typeck/check/expr.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
583583
} else {
584584
assert!(e_ty.is_unit());
585585
let ty = coerce.expected_ty();
586-
coerce.coerce_forced_unit(self, &cause, &mut |err| {
586+
coerce.coerce_forced_unit(self, &cause, &mut |mut err| {
587+
self.suggest_mismatched_types_on_tail(
588+
&mut err,
589+
expr,
590+
ty,
591+
e_ty,
592+
cause.span,
593+
target_id,
594+
);
587595
let val = match ty.kind {
588596
ty::Bool => "true",
589597
ty::Char => "'a'",

src/librustc_typeck/check/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -4245,7 +4245,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
42454245
/// - Possible missing return type if the return type is the default, and not `fn main()`.
42464246
pub fn suggest_mismatched_types_on_tail(
42474247
&self,
4248-
err: &mut DiagnosticBuilder<'tcx>,
4248+
err: &mut DiagnosticBuilder<'_>,
42494249
expr: &'tcx hir::Expr,
42504250
expected: Ty<'tcx>,
42514251
found: Ty<'tcx>,
@@ -4272,7 +4272,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
42724272
/// ```
42734273
fn suggest_fn_call(
42744274
&self,
4275-
err: &mut DiagnosticBuilder<'tcx>,
4275+
err: &mut DiagnosticBuilder<'_>,
42764276
expr: &hir::Expr,
42774277
expected: Ty<'tcx>,
42784278
found: Ty<'tcx>,
@@ -4385,7 +4385,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
43854385

43864386
pub fn suggest_ref_or_into(
43874387
&self,
4388-
err: &mut DiagnosticBuilder<'tcx>,
4388+
err: &mut DiagnosticBuilder<'_>,
43894389
expr: &hir::Expr,
43904390
expected: Ty<'tcx>,
43914391
found: Ty<'tcx>,
@@ -4453,7 +4453,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
44534453
/// in the heap by calling `Box::new()`.
44544454
fn suggest_boxing_when_appropriate(
44554455
&self,
4456-
err: &mut DiagnosticBuilder<'tcx>,
4456+
err: &mut DiagnosticBuilder<'_>,
44574457
expr: &hir::Expr,
44584458
expected: Ty<'tcx>,
44594459
found: Ty<'tcx>,
@@ -4497,7 +4497,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
44974497
/// it suggests adding a semicolon.
44984498
fn suggest_missing_semicolon(
44994499
&self,
4500-
err: &mut DiagnosticBuilder<'tcx>,
4500+
err: &mut DiagnosticBuilder<'_>,
45014501
expression: &'tcx hir::Expr,
45024502
expected: Ty<'tcx>,
45034503
cause_span: Span,
@@ -4536,7 +4536,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
45364536
/// type.
45374537
fn suggest_missing_return_type(
45384538
&self,
4539-
err: &mut DiagnosticBuilder<'tcx>,
4539+
err: &mut DiagnosticBuilder<'_>,
45404540
fn_decl: &hir::FnDecl,
45414541
expected: Ty<'tcx>,
45424542
found: Ty<'tcx>,
@@ -4602,7 +4602,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
46024602
/// `.await` to the tail of the expression.
46034603
fn suggest_missing_await(
46044604
&self,
4605-
err: &mut DiagnosticBuilder<'tcx>,
4605+
err: &mut DiagnosticBuilder<'_>,
46064606
expr: &hir::Expr,
46074607
expected: Ty<'tcx>,
46084608
found: Ty<'tcx>,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn loop_ending() -> i32 {
2+
loop {
3+
if false { break; } //~ ERROR mismatched types
4+
return 42;
5+
}
6+
}
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/type-error-break-tail.rs:3:20
3+
|
4+
LL | fn loop_ending() -> i32 {
5+
| --- expected `i32` because of return type
6+
LL | loop {
7+
LL | if false { break; }
8+
| ^^^^^
9+
| |
10+
| expected i32, found ()
11+
| help: give it a value of the expected type: `break 42`
12+
|
13+
= note: expected type `i32`
14+
found type `()`
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)