about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <dylan.dpc@gmail.com>2020-05-05 12:55:11 +0200
committerGitHub <noreply@github.com>2020-05-05 12:55:11 +0200
commit67a7b7a329ff6e3e242bc20edf00f0916f7bf78e (patch)
treec2a7571dc6c9862424cb4844284618d8f80829b8
parenta49d2b7a74a36db82be76a2ad04f647fc399811b (diff)
parentca72352e604408142c96a16e1ab9fed4916453de (diff)
downloadrust-67a7b7a329ff6e3e242bc20edf00f0916f7bf78e.tar.gz
rust-67a7b7a329ff6e3e242bc20edf00f0916f7bf78e.zip
Rollup merge of #71894 - mibac138:semicolon-not-always-helpful, r=estebank
Suggest removing semicolon in last expression only if it's type is known

Fixes #67971

Is there a syntax for explicitly checking if a note doesn't exist in test output? Something like `//~ !NOTE ...`

I believe r? @estebank deals with diagnostics.
-rw-r--r--src/librustc_typeck/check/mod.rs4
-rw-r--r--src/test/ui/issues/issue-43162.stderr3
-rw-r--r--src/test/ui/typeck/issue-67971.rs9
-rw-r--r--src/test/ui/typeck/issue-67971.stderr18
4 files changed, 30 insertions, 4 deletions
diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs
index 6274a11ebd5..bff1ca2433a 100644
--- a/src/librustc_typeck/check/mod.rs
+++ b/src/librustc_typeck/check/mod.rs
@@ -5387,7 +5387,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
             _ => return None,
         };
         let last_expr_ty = self.node_ty(last_expr.hir_id);
-        if self.can_sub(self.param_env, last_expr_ty, expected_ty).is_err() {
+        if matches!(last_expr_ty.kind, ty::Error)
+            || self.can_sub(self.param_env, last_expr_ty, expected_ty).is_err()
+        {
             return None;
         }
         let original_span = original_sp(last_stmt.span, blk.span);
diff --git a/src/test/ui/issues/issue-43162.stderr b/src/test/ui/issues/issue-43162.stderr
index 0ed3d27c65b..a443db40732 100644
--- a/src/test/ui/issues/issue-43162.stderr
+++ b/src/test/ui/issues/issue-43162.stderr
@@ -17,9 +17,6 @@ LL | fn foo() -> bool {
    |    ---      ^^^^ expected `bool`, found `()`
    |    |
    |    implicitly returns `()` as its body has no tail or `return` expression
-LL |
-LL |     break true;
-   |               - help: consider removing this semicolon
 
 error: aborting due to 3 previous errors
 
diff --git a/src/test/ui/typeck/issue-67971.rs b/src/test/ui/typeck/issue-67971.rs
new file mode 100644
index 00000000000..8bf725cb5ee
--- /dev/null
+++ b/src/test/ui/typeck/issue-67971.rs
@@ -0,0 +1,9 @@
+struct S {}
+
+fn foo(ctx: &mut S) -> String { //~ ERROR mismatched types
+    // Don't suggest to remove semicolon as it won't fix anything
+    ctx.sleep = 0;
+    //~^ ERROR no field `sleep` on type `&mut S`
+}
+
+fn main() {}
diff --git a/src/test/ui/typeck/issue-67971.stderr b/src/test/ui/typeck/issue-67971.stderr
new file mode 100644
index 00000000000..36ad3fcb342
--- /dev/null
+++ b/src/test/ui/typeck/issue-67971.stderr
@@ -0,0 +1,18 @@
+error[E0609]: no field `sleep` on type `&mut S`
+  --> $DIR/issue-67971.rs:5:9
+   |
+LL |     ctx.sleep = 0;
+   |         ^^^^^ unknown field
+
+error[E0308]: mismatched types
+  --> $DIR/issue-67971.rs:3:24
+   |
+LL | fn foo(ctx: &mut S) -> String {
+   |    ---                 ^^^^^^ expected struct `std::string::String`, found `()`
+   |    |
+   |    implicitly returns `()` as its body has no tail or `return` expression
+
+error: aborting due to 2 previous errors
+
+Some errors have detailed explanations: E0308, E0609.
+For more information about an error, try `rustc --explain E0308`.