Skip to content

Commit 2e06d9c

Browse files
committed
Point at return type when appropriate
1 parent 954769e commit 2e06d9c

8 files changed

+30
-5
lines changed

src/librustc_typeck/check/coercion.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -1250,14 +1250,26 @@ impl<'gcx, 'tcx, 'exprs, E> CoerceMany<'gcx, 'tcx, 'exprs, E>
12501250
}
12511251
}
12521252
}
1253-
_ => {
1253+
ObligationCauseCode::ReturnType(_id) => {
12541254
db = fcx.report_mismatched_types(cause, expected, found, err);
1255-
if let Some(sp) = fcx.ret_coercion_span.borrow().as_ref() {
1255+
let _id = fcx.tcx.hir().get_parent_node(_id);
1256+
let mut pointing_at_return_type = false;
1257+
if let Some((fn_decl, can_suggest)) = fcx.get_fn_decl(_id) {
1258+
pointing_at_return_type = fcx.suggest_missing_return_type(
1259+
&mut db, &fn_decl, expected, found, can_suggest);
1260+
}
1261+
if let (Some(sp), false) = (
1262+
fcx.ret_coercion_span.borrow().as_ref(),
1263+
pointing_at_return_type,
1264+
) {
12561265
if !sp.overlaps(cause.span) {
12571266
db.span_label(*sp, reason_label);
12581267
}
12591268
}
12601269
}
1270+
_ => {
1271+
db = fcx.report_mismatched_types(cause, expected, found, err);
1272+
}
12611273
}
12621274

12631275
if let Some(augment_error) = augment_error {

src/test/ui/fully-qualified-type/fully-qualified-type-name2.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0308]: mismatched types
22
--> $DIR/fully-qualified-type-name2.rs:12:12
33
|
4+
LL | fn bar(x: x::Foo) -> y::Foo {
5+
| ------ expected `y::Foo` because of return type
46
LL | return x;
57
| ^ expected enum `y::Foo`, found enum `x::Foo`
68
|

src/test/ui/fully-qualified-type/fully-qualified-type-name4.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0308]: mismatched types
22
--> $DIR/fully-qualified-type-name4.rs:6:12
33
|
4+
LL | fn bar(x: usize) -> Option<usize> {
5+
| ------------- expected `std::option::Option<usize>` because of return type
46
LL | return x;
57
| ^ expected enum `std::option::Option`, found usize
68
|

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $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 () - expected because of this statement
5+
| - ^^^^^ expected isize, found ()
66
| |
77
| this function's body doesn't return
88
|

src/test/ui/proc-macro/span-preservation.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ LL | let x: usize = "hello";;;;; //~ ERROR mismatched types
1515
error[E0308]: mismatched types
1616
--> $DIR/span-preservation.rs:19:29
1717
|
18+
LL | fn b(x: Option<isize>) -> usize {
19+
| ----- expected `usize` because of return type
20+
LL | match x {
1821
LL | Some(x) => { return x }, //~ ERROR mismatched types
1922
| ^ expected usize, found isize
2023

src/test/ui/return/return-from-diverging.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0308]: mismatched types
22
--> $DIR/return-from-diverging.rs:4:12
33
|
4+
LL | fn fail() -> ! {
5+
| - expected `!` because of return type
46
LL | return "wow"; //~ ERROR mismatched types
57
| ^^^^^ expected !, found reference
68
|

src/test/ui/tail-typeck.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0308]: mismatched types
22
--> $DIR/tail-typeck.rs:3:26
33
|
44
LL | fn f() -> isize { return g(); }
5-
| ^^^ expected isize, found usize
5+
| ----- ^^^ expected isize, found usize
6+
| |
7+
| expected `isize` because of return type
68

79
error: aborting due to previous error
810

src/test/ui/wrong-ret-type.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0308]: mismatched types
22
--> $DIR/wrong-ret-type.rs:2:49
33
|
44
LL | fn mk_int() -> usize { let i: isize = 3; return i; }
5-
| ^ expected usize, found isize
5+
| ----- ^ expected usize, found isize
6+
| |
7+
| expected `usize` because of return type
68

79
error: aborting due to previous error
810

0 commit comments

Comments
 (0)