about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2019-08-06 14:20:39 -0700
committerEsteban Küber <esteban@kuber.com.ar>2019-08-09 07:18:05 -0700
commitc076392143e483f60429d148ac58522e423eea9e (patch)
tree70625460727d33c3d7f3e0a42687e61f3af94238
parent813a3a5d4b2be4e2101faa73a067da02a704a598 (diff)
downloadrust-c076392143e483f60429d148ac58522e423eea9e.tar.gz
rust-c076392143e483f60429d148ac58522e423eea9e.zip
Tweak mismatched types error on break expressions
-rw-r--r--src/librustc_typeck/check/expr.rs16
-rw-r--r--src/test/ui/issues/issue-27042.stderr9
-rw-r--r--src/test/ui/loops/loop-break-value.stderr18
-rw-r--r--src/test/ui/loops/loop-labeled-break-value.stderr27
-rw-r--r--src/test/ui/loops/loop-properly-diverging-2.stderr9
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