about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2018-12-30 13:55:00 -0800
committerEsteban Küber <esteban@kuber.com.ar>2018-12-30 13:55:00 -0800
commit1f65dc07705e9463ffed0395894f1e7d63cec515 (patch)
tree380dcfaead68cc3d9bf0fa89ebc41b7aa2716a5c
parent59183180f718fc2212828e180f2f856f0db1bb9c (diff)
downloadrust-1f65dc07705e9463ffed0395894f1e7d63cec515.tar.gz
rust-1f65dc07705e9463ffed0395894f1e7d63cec515.zip
Point at the return type span on type mismatch due to missing return
Do not point at the entire block span on fn return type mismatches
caused by missing return.
-rw-r--r--src/librustc/hir/map/mod.rs3
-rw-r--r--src/librustc_typeck/check/mod.rs12
-rw-r--r--src/test/ui/block-result/consider-removing-last-semi.stderr28
-rw-r--r--src/test/ui/block-result/issue-11714.stderr15
-rw-r--r--src/test/ui/block-result/issue-13428.stderr31
-rw-r--r--src/test/ui/break-while-condition.stderr9
-rw-r--r--src/test/ui/coercion/coercion-missing-tail-expected-type.stderr24
-rw-r--r--src/test/ui/issues/issue-10536.stderr6
-rw-r--r--src/test/ui/issues/issue-32323.stderr4
-rw-r--r--src/test/ui/issues/issue-43162.stderr14
-rw-r--r--src/test/ui/issues/issue-44023.stderr8
-rw-r--r--src/test/ui/issues/issue-6458-4.stderr12
-rw-r--r--src/test/ui/liveness/liveness-forgot-ret.stderr4
-rw-r--r--src/test/ui/liveness/liveness-missing-ret2.stderr11
-rw-r--r--src/test/ui/liveness/liveness-return-last-stmt-semi.stderr34
-rw-r--r--src/test/ui/missing/missing-return.stderr4
16 files changed, 96 insertions, 123 deletions
diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs
index 188d487d644..5bcbeeefa50 100644
--- a/src/librustc/hir/map/mod.rs
+++ b/src/librustc/hir/map/mod.rs
@@ -667,6 +667,7 @@ impl<'hir> Map<'hir> {
                 Node::Item(_) |
                 Node::ForeignItem(_) |
                 Node::TraitItem(_) |
+                Node::Expr(Expr { node: ExprKind::Closure(..), ..}) |
                 Node::ImplItem(_) => true,
                 _ => false,
             }
@@ -675,7 +676,7 @@ impl<'hir> Map<'hir> {
             match *node {
                 Node::Expr(ref expr) => {
                     match expr.node {
-                        ExprKind::While(..) | ExprKind::Loop(..) => true,
+                        ExprKind::While(..) | ExprKind::Loop(..) | ExprKind::Ret(..) => true,
                         _ => false,
                     }
                 }
diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs
index 39beb283285..aa13087b484 100644
--- a/src/librustc_typeck/check/mod.rs
+++ b/src/librustc_typeck/check/mod.rs
@@ -4791,7 +4791,17 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
                 // #41425 -- label the implicit `()` as being the
                 // "found type" here, rather than the "expected type".
                 if !self.diverges.get().always() {
-                    coerce.coerce_forced_unit(self, &self.misc(blk.span), &mut |err| {
+                    // #50009 -- Do not point at the entire fn block span, point at the return type
+                    // span, as it is the cause of the requirement, and
+                    // `consider_hint_about_removing_semicolon` will point at the last expression
+                    // if it were a relevant part of the error. This improves usability in editors
+                    // that highlight errors inline.
+                    let sp = if let Some((decl, _)) = self.get_fn_decl(blk.id) {
+                        decl.output.span()
+                    } else {
+                        blk.span
+                    };
+                    coerce.coerce_forced_unit(self, &self.misc(sp), &mut |err| {
                         if let Some(expected_ty) = expected.only_has_type(self) {
                             self.consider_hint_about_removing_semicolon(blk,
                                                                         expected_ty,
diff --git a/src/test/ui/block-result/consider-removing-last-semi.stderr b/src/test/ui/block-result/consider-removing-last-semi.stderr
index 870da00680f..ce8c624cdad 100644
--- a/src/test/ui/block-result/consider-removing-last-semi.stderr
+++ b/src/test/ui/block-result/consider-removing-last-semi.stderr
@@ -1,27 +1,23 @@
 error[E0308]: mismatched types
-  --> $DIR/consider-removing-last-semi.rs:1:18
+  --> $DIR/consider-removing-last-semi.rs:1:11
    |
-LL |   fn f() -> String {  //~ ERROR mismatched types
-   |  __________________^
-LL | |     0u8;
-LL | |     "bla".to_string();
-   | |                      - help: consider removing this semicolon
-LL | | }
-   | |_^ expected struct `std::string::String`, found ()
+LL | fn f() -> String {  //~ ERROR mismatched types
+   |           ^^^^^^ expected struct `std::string::String`, found ()
+LL |     0u8;
+LL |     "bla".to_string();
+   |                      - help: consider removing this semicolon
    |
    = note: expected type `std::string::String`
               found type `()`
 
 error[E0308]: mismatched types
-  --> $DIR/consider-removing-last-semi.rs:6:18
+  --> $DIR/consider-removing-last-semi.rs:6:11
    |
-LL |   fn g() -> String {  //~ ERROR mismatched types
-   |  __________________^
-LL | |     "this won't work".to_string();
-LL | |     "removeme".to_string();
-   | |                           - help: consider removing this semicolon
-LL | | }
-   | |_^ expected struct `std::string::String`, found ()
+LL | fn g() -> String {  //~ ERROR mismatched types
+   |           ^^^^^^ expected struct `std::string::String`, found ()
+LL |     "this won't work".to_string();
+LL |     "removeme".to_string();
+   |                           - help: consider removing this semicolon
    |
    = note: expected type `std::string::String`
               found type `()`
diff --git a/src/test/ui/block-result/issue-11714.stderr b/src/test/ui/block-result/issue-11714.stderr
index 8bb37395c5c..cc97eaf8e1a 100644
--- a/src/test/ui/block-result/issue-11714.stderr
+++ b/src/test/ui/block-result/issue-11714.stderr
@@ -1,14 +1,11 @@
 error[E0308]: mismatched types
-  --> $DIR/issue-11714.rs:1:18
+  --> $DIR/issue-11714.rs:1:14
    |
-LL |   fn blah() -> i32 { //~ ERROR mismatched types
-   |  __________________^
-LL | |     1
-LL | |
-LL | |     ;
-   | |     - help: consider removing this semicolon
-LL | | }
-   | |_^ expected i32, found ()
+LL | fn blah() -> i32 { //~ ERROR mismatched types
+   |              ^^^ expected i32, found ()
+...
+LL |     ;
+   |     - help: consider removing this semicolon
    |
    = note: expected type `i32`
               found type `()`
diff --git a/src/test/ui/block-result/issue-13428.stderr b/src/test/ui/block-result/issue-13428.stderr
index 516baa8e5f1..d3a2b513137 100644
--- a/src/test/ui/block-result/issue-13428.stderr
+++ b/src/test/ui/block-result/issue-13428.stderr
@@ -1,30 +1,23 @@
 error[E0308]: mismatched types
-  --> $DIR/issue-13428.rs:3:20
+  --> $DIR/issue-13428.rs:3:13
    |
-LL |   fn foo() -> String {  //~ ERROR mismatched types
-   |  ____________________^
-LL | |     format!("Hello {}",
-LL | |             "world")
-LL | |     // Put the trailing semicolon on its own line to test that the
-LL | |     // note message gets the offending semicolon exactly
-LL | |     ;
-   | |     - help: consider removing this semicolon
-LL | | }
-   | |_^ expected struct `std::string::String`, found ()
+LL | fn foo() -> String {  //~ ERROR mismatched types
+   |             ^^^^^^ expected struct `std::string::String`, found ()
+...
+LL |     ;
+   |     - help: consider removing this semicolon
    |
    = note: expected type `std::string::String`
               found type `()`
 
 error[E0308]: mismatched types
-  --> $DIR/issue-13428.rs:11:20
+  --> $DIR/issue-13428.rs:11:13
    |
-LL |   fn bar() -> String {  //~ ERROR mismatched types
-   |  ____________________^
-LL | |     "foobar".to_string()
-LL | |     ;
-   | |     - help: consider removing this semicolon
-LL | | }
-   | |_^ expected struct `std::string::String`, found ()
+LL | fn bar() -> String {  //~ ERROR mismatched types
+   |             ^^^^^^ expected struct `std::string::String`, found ()
+LL |     "foobar".to_string()
+LL |     ;
+   |     - help: consider removing this semicolon
    |
    = note: expected type `std::string::String`
               found type `()`
diff --git a/src/test/ui/break-while-condition.stderr b/src/test/ui/break-while-condition.stderr
index 3e81753a410..1c48ad630d8 100644
--- a/src/test/ui/break-while-condition.stderr
+++ b/src/test/ui/break-while-condition.stderr
@@ -1,11 +1,8 @@
 error[E0308]: mismatched types
-  --> $DIR/break-while-condition.rs:9:20
+  --> $DIR/break-while-condition.rs:3:11
    |
-LL |           let _: ! = { //~ ERROR mismatched types
-   |  ____________________^
-LL | |             'a: while break 'a {};
-LL | |         };
-   | |_________^ expected !, found ()
+LL | fn main() {
+   |           ^ expected !, found ()
    |
    = note: expected type `!`
               found type `()`
diff --git a/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr b/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr
index 18adf9a46cc..27f0540e36e 100644
--- a/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr
+++ b/src/test/ui/coercion/coercion-missing-tail-expected-type.stderr
@@ -1,25 +1,21 @@
 error[E0308]: mismatched types
-  --> $DIR/coercion-missing-tail-expected-type.rs:3:28
+  --> $DIR/coercion-missing-tail-expected-type.rs:3:24
    |
-LL |   fn plus_one(x: i32) -> i32 { //~ ERROR mismatched types
-   |  ____________________________^
-LL | |     x + 1;
-   | |          - help: consider removing this semicolon
-LL | | }
-   | |_^ expected i32, found ()
+LL | fn plus_one(x: i32) -> i32 { //~ ERROR mismatched types
+   |                        ^^^ expected i32, found ()
+LL |     x + 1;
+   |          - help: consider removing this semicolon
    |
    = note: expected type `i32`
               found type `()`
 
 error[E0308]: mismatched types
-  --> $DIR/coercion-missing-tail-expected-type.rs:7:29
+  --> $DIR/coercion-missing-tail-expected-type.rs:7:13
    |
-LL |   fn foo() -> Result<u8, u64> { //~ ERROR mismatched types
-   |  _____________________________^
-LL | |     Ok(1);
-   | |          - help: consider removing this semicolon
-LL | | }
-   | |_^ expected enum `std::result::Result`, found ()
+LL | fn foo() -> Result<u8, u64> { //~ ERROR mismatched types
+   |             ^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found ()
+LL |     Ok(1);
+   |          - help: consider removing this semicolon
    |
    = note: expected type `std::result::Result<u8, u64>`
               found type `()`
diff --git a/src/test/ui/issues/issue-10536.stderr b/src/test/ui/issues/issue-10536.stderr
index d5caf777cd4..71f9a83316c 100644
--- a/src/test/ui/issues/issue-10536.stderr
+++ b/src/test/ui/issues/issue-10536.stderr
@@ -17,10 +17,10 @@ LL |     assert!({one! two()});
    |              ^^^
 
 error[E0308]: mismatched types
-  --> $DIR/issue-10536.rs:14:13
+  --> $DIR/issue-10536.rs:11:15
    |
-LL |     assert!({one! two()});
-   |             ^^^^^^^^^^^^ expected bool, found ()
+LL | pub fn main() {
+   |               ^ expected bool, found ()
    |
    = note: expected type `bool`
               found type `()`
diff --git a/src/test/ui/issues/issue-32323.stderr b/src/test/ui/issues/issue-32323.stderr
index 00ef2ca1809..6729fdf726a 100644
--- a/src/test/ui/issues/issue-32323.stderr
+++ b/src/test/ui/issues/issue-32323.stderr
@@ -1,8 +1,8 @@
 error[E0308]: mismatched types
-  --> $DIR/issue-32323.rs:5:49
+  --> $DIR/issue-32323.rs:5:30
    |
 LL | pub fn f<'a, T: Tr<'a>>() -> <T as Tr<'a>>::Out {}
-   |                                                 ^^ expected associated type, found ()
+   |                              ^^^^^^^^^^^^^^^^^^ expected associated type, found ()
    |
    = note: expected type `<T as Tr<'a>>::Out`
               found type `()`
diff --git a/src/test/ui/issues/issue-43162.stderr b/src/test/ui/issues/issue-43162.stderr
index 8b02476841d..d679fbb7262 100644
--- a/src/test/ui/issues/issue-43162.stderr
+++ b/src/test/ui/issues/issue-43162.stderr
@@ -11,15 +11,13 @@ LL |     break {}; //~ ERROR E0268
    |     ^^^^^^^^ cannot break outside of a loop
 
 error[E0308]: mismatched types
-  --> $DIR/issue-43162.rs:1:18
+  --> $DIR/issue-43162.rs:1:13
    |
-LL |   fn foo() -> bool {
-   |  __________________^
-LL | |     //~^ ERROR E0308
-LL | |     break true; //~ ERROR E0268
-   | |               - help: consider removing this semicolon
-LL | | }
-   | |_^ expected bool, found ()
+LL | fn foo() -> bool {
+   |             ^^^^ expected bool, found ()
+LL |     //~^ ERROR E0308
+LL |     break true; //~ ERROR E0268
+   |               - help: consider removing this semicolon
    |
    = note: expected type `bool`
               found type `()`
diff --git a/src/test/ui/issues/issue-44023.stderr b/src/test/ui/issues/issue-44023.stderr
index 21cfe07e3e6..55757c1eb93 100644
--- a/src/test/ui/issues/issue-44023.stderr
+++ b/src/test/ui/issues/issue-44023.stderr
@@ -1,10 +1,8 @@
 error[E0308]: mismatched types
-  --> $DIR/issue-44023.rs:5:42
+  --> $DIR/issue-44023.rs:5:36
    |
-LL |   fn საჭმელად_გემრიელი_სადილი ( ) -> isize { //~ ERROR mismatched types
-   |  __________________________________________^
-LL | | }
-   | |_^ expected isize, found ()
+LL | fn საჭმელად_გემრიელი_სადილი ( ) -> isize { //~ ERROR mismatched types
+   |                                    ^^^^^ expected isize, found ()
    |
    = note: expected type `isize`
               found type `()`
diff --git a/src/test/ui/issues/issue-6458-4.stderr b/src/test/ui/issues/issue-6458-4.stderr
index 3e621c17cdc..e487e15c16c 100644
--- a/src/test/ui/issues/issue-6458-4.stderr
+++ b/src/test/ui/issues/issue-6458-4.stderr
@@ -1,12 +1,10 @@
 error[E0308]: mismatched types
-  --> $DIR/issue-6458-4.rs:1:40
+  --> $DIR/issue-6458-4.rs:1:20
    |
-LL |   fn foo(b: bool) -> Result<bool,String> { //~ ERROR mismatched types
-   |  ________________________________________^
-LL | |     Err("bar".to_string());
-   | |                           - help: consider removing this semicolon
-LL | | }
-   | |_^ expected enum `std::result::Result`, found ()
+LL | fn foo(b: bool) -> Result<bool,String> { //~ ERROR mismatched types
+   |                    ^^^^^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found ()
+LL |     Err("bar".to_string());
+   |                           - help: consider removing this semicolon
    |
    = note: expected type `std::result::Result<bool, std::string::String>`
               found type `()`
diff --git a/src/test/ui/liveness/liveness-forgot-ret.stderr b/src/test/ui/liveness/liveness-forgot-ret.stderr
index b8911225900..8d0eeb35fc3 100644
--- a/src/test/ui/liveness/liveness-forgot-ret.stderr
+++ b/src/test/ui/liveness/liveness-forgot-ret.stderr
@@ -1,8 +1,8 @@
 error[E0308]: mismatched types
-  --> $DIR/liveness-forgot-ret.rs:3:25
+  --> $DIR/liveness-forgot-ret.rs:3:19
    |
 LL | fn f(a: isize) -> isize { if god_exists(a) { return 5; }; }
-   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected isize, found ()
+   |                   ^^^^^ expected isize, found ()    - expected because of this statement
    |
    = note: expected type `isize`
               found type `()`
diff --git a/src/test/ui/liveness/liveness-missing-ret2.stderr b/src/test/ui/liveness/liveness-missing-ret2.stderr
index e5f74786bc6..956b71fcfe8 100644
--- a/src/test/ui/liveness/liveness-missing-ret2.stderr
+++ b/src/test/ui/liveness/liveness-missing-ret2.stderr
@@ -1,13 +1,8 @@
 error[E0308]: mismatched types
-  --> $DIR/liveness-missing-ret2.rs:1:17
+  --> $DIR/liveness-missing-ret2.rs:1:11
    |
-LL |   fn f() -> isize { //~ ERROR mismatched types
-   |  _________________^
-LL | |     // Make sure typestate doesn't interpret this match expression as
-LL | |     // the function result
-LL | |    match true { true => { } _ => {} };
-LL | | }
-   | |_^ expected isize, found ()
+LL | fn f() -> isize { //~ ERROR mismatched types
+   |           ^^^^^ expected isize, found ()
    |
    = note: expected type `isize`
               found type `()`
diff --git a/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr b/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr
index 3aa81a405a6..bb4ca303aa7 100644
--- a/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr
+++ b/src/test/ui/liveness/liveness-return-last-stmt-semi.stderr
@@ -1,11 +1,10 @@
 error[E0308]: mismatched types
-  --> $DIR/liveness-return-last-stmt-semi.rs:4:45
+  --> $DIR/liveness-return-last-stmt-semi.rs:4:41
    |
 LL | macro_rules! test { () => { fn foo() -> i32 { 1; } } }
-   |                                             ^^^-^^
-   |                                             |  |
-   |                                             |  help: consider removing this semicolon
-   |                                             expected i32, found ()
+   |                                         ^^^    - help: consider removing this semicolon
+   |                                         |
+   |                                         expected i32, found ()
 ...
 LL |     test!();
    |     -------- in this macro invocation
@@ -14,35 +13,30 @@ LL |     test!();
               found type `()`
 
 error[E0308]: mismatched types
-  --> $DIR/liveness-return-last-stmt-semi.rs:7:23
+  --> $DIR/liveness-return-last-stmt-semi.rs:7:19
    |
 LL | fn no_return() -> i32 {} //~ ERROR mismatched types
-   |                       ^^ expected i32, found ()
+   |                   ^^^ expected i32, found ()
    |
    = note: expected type `i32`
               found type `()`
 
 error[E0308]: mismatched types
-  --> $DIR/liveness-return-last-stmt-semi.rs:9:23
+  --> $DIR/liveness-return-last-stmt-semi.rs:9:19
    |
-LL |   fn bar(x: u32) -> u32 { //~ ERROR mismatched types
-   |  _______________________^
-LL | |     x * 2;
-   | |          - help: consider removing this semicolon
-LL | | }
-   | |_^ expected u32, found ()
+LL | fn bar(x: u32) -> u32 { //~ ERROR mismatched types
+   |                   ^^^ expected u32, found ()
+LL |     x * 2;
+   |          - help: consider removing this semicolon
    |
    = note: expected type `u32`
               found type `()`
 
 error[E0308]: mismatched types
-  --> $DIR/liveness-return-last-stmt-semi.rs:13:23
+  --> $DIR/liveness-return-last-stmt-semi.rs:13:19
    |
-LL |   fn baz(x: u64) -> u32 { //~ ERROR mismatched types
-   |  _______________________^
-LL | |     x * 2;
-LL | | }
-   | |_^ expected u32, found ()
+LL | fn baz(x: u64) -> u32 { //~ ERROR mismatched types
+   |                   ^^^ expected u32, found ()
    |
    = note: expected type `u32`
               found type `()`
diff --git a/src/test/ui/missing/missing-return.stderr b/src/test/ui/missing/missing-return.stderr
index 6d42dafd6f1..be86325de00 100644
--- a/src/test/ui/missing/missing-return.stderr
+++ b/src/test/ui/missing/missing-return.stderr
@@ -1,8 +1,8 @@
 error[E0308]: mismatched types
-  --> $DIR/missing-return.rs:3:17
+  --> $DIR/missing-return.rs:3:11
    |
 LL | fn f() -> isize { }
-   |                 ^^^ expected isize, found ()
+   |           ^^^^^ expected isize, found ()
    |
    = note: expected type `isize`
               found type `()`