]> git.lizzy.rs Git - rust.git/commitdiff
Fix incorrect type mismatch label pointing at return type
authorEsteban Küber <esteban@kuber.com.ar>
Thu, 14 Dec 2017 01:34:47 +0000 (17:34 -0800)
committerEsteban Küber <esteban@kuber.com.ar>
Fri, 29 Jun 2018 17:36:32 +0000 (10:36 -0700)
src/librustc_typeck/check/mod.rs
src/test/ui/break-while-condition.stderr
src/test/ui/issue-50585.stderr
src/test/ui/suggestions/issue-46302.rs [new file with mode: 0644]
src/test/ui/suggestions/issue-46302.stderr [new file with mode: 0644]
src/test/ui/suggestions/str-array-assignment.stderr

index 0185d00518699b0b53cc09681b665ce8dba83034..fa78b38dbb7a84da4b2a36953ac26db3ec91b33d 100644 (file)
@@ -4625,21 +4625,23 @@ fn suggest_missing_return_type(&self,
                                    can_suggest: bool) {
         // Only suggest changing the return type for methods that
         // haven't set a return type at all (and aren't `fn main()` or an impl).
-        match (&fn_decl.output, found.is_suggestable(), can_suggest) {
-            (&hir::FunctionRetTy::DefaultReturn(span), true, true) => {
+        match (&fn_decl.output, found.is_suggestable(), can_suggest, expected.is_nil()) {
+            (&hir::FunctionRetTy::DefaultReturn(span), true, true, true) => {
                 err.span_suggestion(span,
                                     "try adding a return type",
                                     format!("-> {} ",
                                             self.resolve_type_vars_with_obligations(found)));
             }
-            (&hir::FunctionRetTy::DefaultReturn(span), false, true) => {
+            (&hir::FunctionRetTy::DefaultReturn(span), false, true, true) => {
                 err.span_label(span, "possibly return type missing here?");
             }
-            (&hir::FunctionRetTy::DefaultReturn(span), _, _) => {
+            (&hir::FunctionRetTy::DefaultReturn(span), _, false, true) => {
                 // `fn main()` must return `()`, do not suggest changing return type
                 err.span_label(span, "expected `()` because of default return type");
             }
-            (&hir::FunctionRetTy::Return(ref ty), _, _) => {
+            // expectation was caused by something else, not the default return
+            (&hir::FunctionRetTy::DefaultReturn(_), _, _, false) => {}
+            (&hir::FunctionRetTy::Return(ref ty), _, _, _) => {
                 // Only point to return type if the expected type is the return type, as if they
                 // are not, the expectation must have been caused by something else.
                 debug!("suggest_missing_return_type: return type {:?} node {:?}", ty, ty.node);
index c8f06db960392b5d8a72837130d912dc1489dfab..9d22dcce4540cb4b29b1575ebebb41210e1268e6 100644 (file)
@@ -13,9 +13,6 @@ LL | |         };
 error[E0308]: mismatched types
   --> $DIR/break-while-condition.rs:26:13
    |
-LL |   fn main() {
-   |             - expected `()` because of default return type
-...
 LL | /             while false { //~ ERROR mismatched types
 LL | |                 break
 LL | |             }
@@ -27,9 +24,6 @@ LL | |             }
 error[E0308]: mismatched types
   --> $DIR/break-while-condition.rs:34:13
    |
-LL |   fn main() {
-   |             - expected `()` because of default return type
-...
 LL | /             while false { //~ ERROR mismatched types
 LL | |                 return
 LL | |             }
index 3f9328de93fd516dd640347464edfde1f0f00ce1..e4edc18c3f8e4e4e2386152b4c09f879a762e2e0 100644 (file)
@@ -1,8 +1,6 @@
 error[E0308]: mismatched types
   --> $DIR/issue-50585.rs:12:18
    |
-LL | fn main() {
-   |           - expected `()` because of default return type
 LL |     |y: Vec<[(); for x in 0..2 {}]>| {};
    |                  ^^^^^^^^^^^^^^^^ expected usize, found ()
    |
diff --git a/src/test/ui/suggestions/issue-46302.rs b/src/test/ui/suggestions/issue-46302.rs
new file mode 100644 (file)
index 0000000..6ae6b54
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn foo() {
+  let s = "abc";
+  let u: &str = if true { s[..2] } else { s };
+  //~^ ERROR mismatched types
+}
+
+fn main() {
+    foo();
+}
diff --git a/src/test/ui/suggestions/issue-46302.stderr b/src/test/ui/suggestions/issue-46302.stderr
new file mode 100644 (file)
index 0000000..8e39913
--- /dev/null
@@ -0,0 +1,15 @@
+error[E0308]: mismatched types
+  --> $DIR/issue-46302.rs:13:27
+   |
+LL |   let u: &str = if true { s[..2] } else { s };
+   |                           ^^^^^^
+   |                           |
+   |                           expected &str, found str
+   |                           help: consider borrowing here: `&s[..2]`
+   |
+   = note: expected type `&str`
+              found type `str`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.
index 041bae4a421080644c088473bf386227040f847c..5af936eec5bb6881e1d58a1b8ff8ef758a2d7a9e 100644 (file)
@@ -10,9 +10,6 @@ LL |   let t = if true { s[..2] } else { s };
 error[E0308]: mismatched types
   --> $DIR/str-array-assignment.rs:15:27
    |
-LL | fn main() {
-   |           - expected `()` because of default return type
-...
 LL |   let u: &str = if true { s[..2] } else { s };
    |                           ^^^^^^
    |                           |