Skip to content

Commit 583b5fc

Browse files
Use full expr span for return suggestion on type error/ambiguity
1 parent b8d7dd8 commit 583b5fc

File tree

3 files changed

+56
-16
lines changed

3 files changed

+56
-16
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2042,7 +2042,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20422042
}
20432043
if block_num > 1 && found_semi {
20442044
err.span_suggestion_verbose(
2045-
span.shrink_to_lo(),
2045+
// use the span of the *whole* expr
2046+
self.tcx.hir().span(binding_hir_id).shrink_to_lo(),
20462047
"you might have meant to return this to infer its type parameters",
20472048
"return ",
20482049
Applicability::MaybeIncorrect,

tests/ui/return/tail-expr-as-potential-return.rs

+28-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ edition:2018
2+
13
// > Suggest returning tail expressions that match return type
24
// >
35
// > Some newcomers are confused by the behavior of tail expressions,
@@ -8,24 +10,24 @@
810
//
911
// This test was amended to also serve as a regression test for #92308, where
1012
// this suggestion would not trigger with async functions.
11-
//
12-
//@ edition:2018
1313

1414
fn main() {
1515
}
1616

1717
fn foo(x: bool) -> Result<f64, i32> {
1818
if x {
19-
Err(42) //~ ERROR mismatched types
20-
//| HELP you might have meant to return this value
19+
Err(42)
20+
//~^ ERROR mismatched types
21+
//~| HELP you might have meant to return this value
2122
}
2223
Ok(42.0)
2324
}
2425

2526
async fn bar(x: bool) -> Result<f64, i32> {
2627
if x {
27-
Err(42) //~ ERROR mismatched types
28-
//| HELP you might have meant to return this value
28+
Err(42)
29+
//~^ ERROR mismatched types
30+
//~| HELP you might have meant to return this value
2931
}
3032
Ok(42.0)
3133
}
@@ -40,8 +42,26 @@ impl<T> Identity for T {
4042

4143
async fn foo2() -> i32 {
4244
if true {
43-
1i32 //~ ERROR mismatched types
44-
//| HELP you might have meant to return this value
45+
1i32
46+
//~^ ERROR mismatched types
47+
//~| HELP you might have meant to return this value
4548
}
4649
0
4750
}
51+
52+
struct Receiver;
53+
impl Receiver {
54+
fn generic<T>(self) -> Option<T> {
55+
None
56+
}
57+
}
58+
fn method() -> Option<i32> {
59+
if true {
60+
Receiver.generic();
61+
//~^ ERROR type annotations needed
62+
//~| HELP consider specifying the generic argument
63+
//~| HELP you might have meant to return this to infer its type parameters
64+
}
65+
66+
None
67+
}

tests/ui/return/tail-expr-as-potential-return.stderr

+26-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
error[E0308]: mismatched types
2-
--> $DIR/tail-expr-as-potential-return.rs:27:9
2+
--> $DIR/tail-expr-as-potential-return.rs:28:9
33
|
44
LL | / if x {
55
LL | | Err(42)
66
| | ^^^^^^^ expected `()`, found `Result<_, {integer}>`
7-
LL | | //| HELP you might have meant to return this value
7+
LL | |
8+
LL | |
89
LL | | }
910
| |_____- expected this to be `()`
1011
|
@@ -16,12 +17,13 @@ LL | return Err(42);
1617
| ++++++ +
1718

1819
error[E0308]: mismatched types
19-
--> $DIR/tail-expr-as-potential-return.rs:43:9
20+
--> $DIR/tail-expr-as-potential-return.rs:45:9
2021
|
2122
LL | / if true {
2223
LL | | 1i32
2324
| | ^^^^ expected `()`, found `i32`
24-
LL | | //| HELP you might have meant to return this value
25+
LL | |
26+
LL | |
2527
LL | | }
2628
| |_____- expected this to be `()`
2729
|
@@ -36,7 +38,8 @@ error[E0308]: mismatched types
3638
LL | / if x {
3739
LL | | Err(42)
3840
| | ^^^^^^^ expected `()`, found `Result<_, {integer}>`
39-
LL | | //| HELP you might have meant to return this value
41+
LL | |
42+
LL | |
4043
LL | | }
4144
| |_____- expected this to be `()`
4245
|
@@ -47,6 +50,22 @@ help: you might have meant to return this value
4750
LL | return Err(42);
4851
| ++++++ +
4952

50-
error: aborting due to 3 previous errors
53+
error[E0282]: type annotations needed
54+
--> $DIR/tail-expr-as-potential-return.rs:60:18
55+
|
56+
LL | Receiver.generic();
57+
| ^^^^^^^ cannot infer type of the type parameter `T` declared on the method `generic`
58+
|
59+
help: consider specifying the generic argument
60+
|
61+
LL | Receiver.generic::<T>();
62+
| +++++
63+
help: you might have meant to return this to infer its type parameters
64+
|
65+
LL | return Receiver.generic();
66+
| ++++++
67+
68+
error: aborting due to 4 previous errors
5169

52-
For more information about this error, try `rustc --explain E0308`.
70+
Some errors have detailed explanations: E0282, E0308.
71+
For more information about an error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)