Skip to content

Commit 1f65dc0

Browse files
committed
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.
1 parent 5918318 commit 1f65dc0

16 files changed

+96
-123
lines changed

src/librustc/hir/map/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ impl<'hir> Map<'hir> {
667667
Node::Item(_) |
668668
Node::ForeignItem(_) |
669669
Node::TraitItem(_) |
670+
Node::Expr(Expr { node: ExprKind::Closure(..), ..}) |
670671
Node::ImplItem(_) => true,
671672
_ => false,
672673
}
@@ -675,7 +676,7 @@ impl<'hir> Map<'hir> {
675676
match *node {
676677
Node::Expr(ref expr) => {
677678
match expr.node {
678-
ExprKind::While(..) | ExprKind::Loop(..) => true,
679+
ExprKind::While(..) | ExprKind::Loop(..) | ExprKind::Ret(..) => true,
679680
_ => false,
680681
}
681682
}

src/librustc_typeck/check/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4791,7 +4791,17 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
47914791
// #41425 -- label the implicit `()` as being the
47924792
// "found type" here, rather than the "expected type".
47934793
if !self.diverges.get().always() {
4794-
coerce.coerce_forced_unit(self, &self.misc(blk.span), &mut |err| {
4794+
// #50009 -- Do not point at the entire fn block span, point at the return type
4795+
// span, as it is the cause of the requirement, and
4796+
// `consider_hint_about_removing_semicolon` will point at the last expression
4797+
// if it were a relevant part of the error. This improves usability in editors
4798+
// that highlight errors inline.
4799+
let sp = if let Some((decl, _)) = self.get_fn_decl(blk.id) {
4800+
decl.output.span()
4801+
} else {
4802+
blk.span
4803+
};
4804+
coerce.coerce_forced_unit(self, &self.misc(sp), &mut |err| {
47954805
if let Some(expected_ty) = expected.only_has_type(self) {
47964806
self.consider_hint_about_removing_semicolon(blk,
47974807
expected_ty,

src/test/ui/block-result/consider-removing-last-semi.stderr

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
error[E0308]: mismatched types
2-
--> $DIR/consider-removing-last-semi.rs:1:18
2+
--> $DIR/consider-removing-last-semi.rs:1:11
33
|
4-
LL | fn f() -> String { //~ ERROR mismatched types
5-
| __________________^
6-
LL | | 0u8;
7-
LL | | "bla".to_string();
8-
| | - help: consider removing this semicolon
9-
LL | | }
10-
| |_^ expected struct `std::string::String`, found ()
4+
LL | fn f() -> String { //~ ERROR mismatched types
5+
| ^^^^^^ expected struct `std::string::String`, found ()
6+
LL | 0u8;
7+
LL | "bla".to_string();
8+
| - help: consider removing this semicolon
119
|
1210
= note: expected type `std::string::String`
1311
found type `()`
1412

1513
error[E0308]: mismatched types
16-
--> $DIR/consider-removing-last-semi.rs:6:18
14+
--> $DIR/consider-removing-last-semi.rs:6:11
1715
|
18-
LL | fn g() -> String { //~ ERROR mismatched types
19-
| __________________^
20-
LL | | "this won't work".to_string();
21-
LL | | "removeme".to_string();
22-
| | - help: consider removing this semicolon
23-
LL | | }
24-
| |_^ expected struct `std::string::String`, found ()
16+
LL | fn g() -> String { //~ ERROR mismatched types
17+
| ^^^^^^ expected struct `std::string::String`, found ()
18+
LL | "this won't work".to_string();
19+
LL | "removeme".to_string();
20+
| - help: consider removing this semicolon
2521
|
2622
= note: expected type `std::string::String`
2723
found type `()`

src/test/ui/block-result/issue-11714.stderr

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
error[E0308]: mismatched types
2-
--> $DIR/issue-11714.rs:1:18
2+
--> $DIR/issue-11714.rs:1:14
33
|
4-
LL | fn blah() -> i32 { //~ ERROR mismatched types
5-
| __________________^
6-
LL | | 1
7-
LL | |
8-
LL | | ;
9-
| | - help: consider removing this semicolon
10-
LL | | }
11-
| |_^ expected i32, found ()
4+
LL | fn blah() -> i32 { //~ ERROR mismatched types
5+
| ^^^ expected i32, found ()
6+
...
7+
LL | ;
8+
| - help: consider removing this semicolon
129
|
1310
= note: expected type `i32`
1411
found type `()`

src/test/ui/block-result/issue-13428.stderr

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
11
error[E0308]: mismatched types
2-
--> $DIR/issue-13428.rs:3:20
2+
--> $DIR/issue-13428.rs:3:13
33
|
4-
LL | fn foo() -> String { //~ ERROR mismatched types
5-
| ____________________^
6-
LL | | format!("Hello {}",
7-
LL | | "world")
8-
LL | | // Put the trailing semicolon on its own line to test that the
9-
LL | | // note message gets the offending semicolon exactly
10-
LL | | ;
11-
| | - help: consider removing this semicolon
12-
LL | | }
13-
| |_^ expected struct `std::string::String`, found ()
4+
LL | fn foo() -> String { //~ ERROR mismatched types
5+
| ^^^^^^ expected struct `std::string::String`, found ()
6+
...
7+
LL | ;
8+
| - help: consider removing this semicolon
149
|
1510
= note: expected type `std::string::String`
1611
found type `()`
1712

1813
error[E0308]: mismatched types
19-
--> $DIR/issue-13428.rs:11:20
14+
--> $DIR/issue-13428.rs:11:13
2015
|
21-
LL | fn bar() -> String { //~ ERROR mismatched types
22-
| ____________________^
23-
LL | | "foobar".to_string()
24-
LL | | ;
25-
| | - help: consider removing this semicolon
26-
LL | | }
27-
| |_^ expected struct `std::string::String`, found ()
16+
LL | fn bar() -> String { //~ ERROR mismatched types
17+
| ^^^^^^ expected struct `std::string::String`, found ()
18+
LL | "foobar".to_string()
19+
LL | ;
20+
| - help: consider removing this semicolon
2821
|
2922
= note: expected type `std::string::String`
3023
found type `()`

src/test/ui/break-while-condition.stderr

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
error[E0308]: mismatched types
2-
--> $DIR/break-while-condition.rs:9:20
2+
--> $DIR/break-while-condition.rs:3:11
33
|
4-
LL | let _: ! = { //~ ERROR mismatched types
5-
| ____________________^
6-
LL | | 'a: while break 'a {};
7-
LL | | };
8-
| |_________^ expected !, found ()
4+
LL | fn main() {
5+
| ^ expected !, found ()
96
|
107
= note: expected type `!`
118
found type `()`

src/test/ui/coercion/coercion-missing-tail-expected-type.stderr

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
error[E0308]: mismatched types
2-
--> $DIR/coercion-missing-tail-expected-type.rs:3:28
2+
--> $DIR/coercion-missing-tail-expected-type.rs:3:24
33
|
4-
LL | fn plus_one(x: i32) -> i32 { //~ ERROR mismatched types
5-
| ____________________________^
6-
LL | | x + 1;
7-
| | - help: consider removing this semicolon
8-
LL | | }
9-
| |_^ expected i32, found ()
4+
LL | fn plus_one(x: i32) -> i32 { //~ ERROR mismatched types
5+
| ^^^ expected i32, found ()
6+
LL | x + 1;
7+
| - help: consider removing this semicolon
108
|
119
= note: expected type `i32`
1210
found type `()`
1311

1412
error[E0308]: mismatched types
15-
--> $DIR/coercion-missing-tail-expected-type.rs:7:29
13+
--> $DIR/coercion-missing-tail-expected-type.rs:7:13
1614
|
17-
LL | fn foo() -> Result<u8, u64> { //~ ERROR mismatched types
18-
| _____________________________^
19-
LL | | Ok(1);
20-
| | - help: consider removing this semicolon
21-
LL | | }
22-
| |_^ expected enum `std::result::Result`, found ()
15+
LL | fn foo() -> Result<u8, u64> { //~ ERROR mismatched types
16+
| ^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found ()
17+
LL | Ok(1);
18+
| - help: consider removing this semicolon
2319
|
2420
= note: expected type `std::result::Result<u8, u64>`
2521
found type `()`

src/test/ui/issues/issue-10536.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ LL | assert!({one! two()});
1717
| ^^^
1818

1919
error[E0308]: mismatched types
20-
--> $DIR/issue-10536.rs:14:13
20+
--> $DIR/issue-10536.rs:11:15
2121
|
22-
LL | assert!({one! two()});
23-
| ^^^^^^^^^^^^ expected bool, found ()
22+
LL | pub fn main() {
23+
| ^ expected bool, found ()
2424
|
2525
= note: expected type `bool`
2626
found type `()`

src/test/ui/issues/issue-32323.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0308]: mismatched types
2-
--> $DIR/issue-32323.rs:5:49
2+
--> $DIR/issue-32323.rs:5:30
33
|
44
LL | pub fn f<'a, T: Tr<'a>>() -> <T as Tr<'a>>::Out {}
5-
| ^^ expected associated type, found ()
5+
| ^^^^^^^^^^^^^^^^^^ expected associated type, found ()
66
|
77
= note: expected type `<T as Tr<'a>>::Out`
88
found type `()`

src/test/ui/issues/issue-43162.stderr

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ LL | break {}; //~ ERROR E0268
1111
| ^^^^^^^^ cannot break outside of a loop
1212

1313
error[E0308]: mismatched types
14-
--> $DIR/issue-43162.rs:1:18
14+
--> $DIR/issue-43162.rs:1:13
1515
|
16-
LL | fn foo() -> bool {
17-
| __________________^
18-
LL | | //~^ ERROR E0308
19-
LL | | break true; //~ ERROR E0268
20-
| | - help: consider removing this semicolon
21-
LL | | }
22-
| |_^ expected bool, found ()
16+
LL | fn foo() -> bool {
17+
| ^^^^ expected bool, found ()
18+
LL | //~^ ERROR E0308
19+
LL | break true; //~ ERROR E0268
20+
| - help: consider removing this semicolon
2321
|
2422
= note: expected type `bool`
2523
found type `()`

src/test/ui/issues/issue-44023.stderr

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
error[E0308]: mismatched types
2-
--> $DIR/issue-44023.rs:5:42
2+
--> $DIR/issue-44023.rs:5:36
33
|
4-
LL | fn საჭმელად_გემრიელი_სადილი ( ) -> isize { //~ ERROR mismatched types
5-
| __________________________________________^
6-
LL | | }
7-
| |_^ expected isize, found ()
4+
LL | fn საჭმელად_გემრიელი_სადილი ( ) -> isize { //~ ERROR mismatched types
5+
| ^^^^^ expected isize, found ()
86
|
97
= note: expected type `isize`
108
found type `()`

src/test/ui/issues/issue-6458-4.stderr

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
error[E0308]: mismatched types
2-
--> $DIR/issue-6458-4.rs:1:40
2+
--> $DIR/issue-6458-4.rs:1:20
33
|
4-
LL | fn foo(b: bool) -> Result<bool,String> { //~ ERROR mismatched types
5-
| ________________________________________^
6-
LL | | Err("bar".to_string());
7-
| | - help: consider removing this semicolon
8-
LL | | }
9-
| |_^ expected enum `std::result::Result`, found ()
4+
LL | fn foo(b: bool) -> Result<bool,String> { //~ ERROR mismatched types
5+
| ^^^^^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found ()
6+
LL | Err("bar".to_string());
7+
| - help: consider removing this semicolon
108
|
119
= note: expected type `std::result::Result<bool, std::string::String>`
1210
found type `()`

src/test/ui/liveness/liveness-forgot-ret.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0308]: mismatched types
2-
--> $DIR/liveness-forgot-ret.rs:3:25
2+
--> $DIR/liveness-forgot-ret.rs:3:19
33
|
44
LL | fn f(a: isize) -> isize { if god_exists(a) { return 5; }; }
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected isize, found ()
5+
| ^^^^^ expected isize, found () - expected because of this statement
66
|
77
= note: expected type `isize`
88
found type `()`

src/test/ui/liveness/liveness-missing-ret2.stderr

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
error[E0308]: mismatched types
2-
--> $DIR/liveness-missing-ret2.rs:1:17
2+
--> $DIR/liveness-missing-ret2.rs:1:11
33
|
4-
LL | fn f() -> isize { //~ ERROR mismatched types
5-
| _________________^
6-
LL | | // Make sure typestate doesn't interpret this match expression as
7-
LL | | // the function result
8-
LL | | match true { true => { } _ => {} };
9-
LL | | }
10-
| |_^ expected isize, found ()
4+
LL | fn f() -> isize { //~ ERROR mismatched types
5+
| ^^^^^ expected isize, found ()
116
|
127
= note: expected type `isize`
138
found type `()`

src/test/ui/liveness/liveness-return-last-stmt-semi.stderr

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
error[E0308]: mismatched types
2-
--> $DIR/liveness-return-last-stmt-semi.rs:4:45
2+
--> $DIR/liveness-return-last-stmt-semi.rs:4:41
33
|
44
LL | macro_rules! test { () => { fn foo() -> i32 { 1; } } }
5-
| ^^^-^^
6-
| | |
7-
| | help: consider removing this semicolon
8-
| expected i32, found ()
5+
| ^^^ - help: consider removing this semicolon
6+
| |
7+
| expected i32, found ()
98
...
109
LL | test!();
1110
| -------- in this macro invocation
@@ -14,35 +13,30 @@ LL | test!();
1413
found type `()`
1514

1615
error[E0308]: mismatched types
17-
--> $DIR/liveness-return-last-stmt-semi.rs:7:23
16+
--> $DIR/liveness-return-last-stmt-semi.rs:7:19
1817
|
1918
LL | fn no_return() -> i32 {} //~ ERROR mismatched types
20-
| ^^ expected i32, found ()
19+
| ^^^ expected i32, found ()
2120
|
2221
= note: expected type `i32`
2322
found type `()`
2423

2524
error[E0308]: mismatched types
26-
--> $DIR/liveness-return-last-stmt-semi.rs:9:23
25+
--> $DIR/liveness-return-last-stmt-semi.rs:9:19
2726
|
28-
LL | fn bar(x: u32) -> u32 { //~ ERROR mismatched types
29-
| _______________________^
30-
LL | | x * 2;
31-
| | - help: consider removing this semicolon
32-
LL | | }
33-
| |_^ expected u32, found ()
27+
LL | fn bar(x: u32) -> u32 { //~ ERROR mismatched types
28+
| ^^^ expected u32, found ()
29+
LL | x * 2;
30+
| - help: consider removing this semicolon
3431
|
3532
= note: expected type `u32`
3633
found type `()`
3734

3835
error[E0308]: mismatched types
39-
--> $DIR/liveness-return-last-stmt-semi.rs:13:23
36+
--> $DIR/liveness-return-last-stmt-semi.rs:13:19
4037
|
41-
LL | fn baz(x: u64) -> u32 { //~ ERROR mismatched types
42-
| _______________________^
43-
LL | | x * 2;
44-
LL | | }
45-
| |_^ expected u32, found ()
38+
LL | fn baz(x: u64) -> u32 { //~ ERROR mismatched types
39+
| ^^^ expected u32, found ()
4640
|
4741
= note: expected type `u32`
4842
found type `()`

src/test/ui/missing/missing-return.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0308]: mismatched types
2-
--> $DIR/missing-return.rs:3:17
2+
--> $DIR/missing-return.rs:3:11
33
|
44
LL | fn f() -> isize { }
5-
| ^^^ expected isize, found ()
5+
| ^^^^^ expected isize, found ()
66
|
77
= note: expected type `isize`
88
found type `()`

0 commit comments

Comments
 (0)