From c076392143e483f60429d148ac58522e423eea9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 6 Aug 2019 14:20:39 -0700 Subject: [PATCH] Tweak mismatched types error on break expressions --- src/librustc_typeck/check/expr.rs | 16 ++++++++++- src/test/ui/issues/issue-27042.stderr | 9 ++++--- src/test/ui/loops/loop-break-value.stderr | 18 ++++++++----- .../ui/loops/loop-labeled-break-value.stderr | 27 ++++++++++++------- .../ui/loops/loop-properly-diverging-2.stderr | 9 ++++--- 5 files changed, 57 insertions(+), 22 deletions(-) diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index d825358beaa..c567741be39 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -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 diff --git a/src/test/ui/issues/issue-27042.stderr b/src/test/ui/issues/issue-27042.stderr index 4beb752854b..7678984a518 100644 --- a/src/test/ui/issues/issue-27042.stderr +++ b/src/test/ui/issues/issue-27042.stderr @@ -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 diff --git a/src/test/ui/loops/loop-break-value.stderr b/src/test/ui/loops/loop-break-value.stderr index 1e167905ec8..7310790b880 100644 --- a/src/test/ui/loops/loop-break-value.stderr +++ b/src/test/ui/loops/loop-break-value.stderr @@ -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 diff --git a/src/test/ui/loops/loop-labeled-break-value.stderr b/src/test/ui/loops/loop-labeled-break-value.stderr index ad7cb4b0c2e..8b9468cacc1 100644 --- a/src/test/ui/loops/loop-labeled-break-value.stderr +++ b/src/test/ui/loops/loop-labeled-break-value.stderr @@ -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 diff --git a/src/test/ui/loops/loop-properly-diverging-2.stderr b/src/test/ui/loops/loop-properly-diverging-2.stderr index 6293fdb058a..3758bbf9f6f 100644 --- a/src/test/ui/loops/loop-properly-diverging-2.stderr +++ b/src/test/ui/loops/loop-properly-diverging-2.stderr @@ -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