Skip to content

Commit 7ac8186

Browse files
Use full expr span for return suggestion on type error/ambiguity
1 parent 19a1d2b commit 7ac8186

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
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

+16
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,19 @@ async fn foo2() -> i32 {
4545
}
4646
0
4747
}
48+
49+
struct Receiver;
50+
impl Receiver {
51+
fn generic<T>(self) -> Option<T> {
52+
None
53+
}
54+
}
55+
fn method() -> Option<i32> {
56+
if true {
57+
Receiver.generic();
58+
//~^ ERROR type annotations needed
59+
//| HELP you might have meant to return this value
60+
}
61+
62+
None
63+
}

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ help: you might have meant to return this value
4747
LL | return Err(42);
4848
| ++++++ +
4949

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

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

0 commit comments

Comments
 (0)