Skip to content

Commit

Permalink
Use full expr span for return suggestion on type error/ambiguity
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jun 29, 2024
1 parent 19a1d2b commit d5bddee
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2042,7 +2042,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
if block_num > 1 && found_semi {
err.span_suggestion_verbose(
span.shrink_to_lo(),
// use the span of the *whole* expr
self.tcx.hir().span(binding_hir_id).shrink_to_lo(),
"you might have meant to return this to infer its type parameters",
"return ",
Applicability::MaybeIncorrect,
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/return/tail-expr-as-potential-return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,19 @@ async fn foo2() -> i32 {
}
0
}

struct Receiver;
impl Receiver {
fn generic<T>(self) -> Option<T> {
None
}
}
fn method() -> Option<i32> {
if true {
Receiver.generic();
//~^ ERROR type annotations needed
//| HELP you might have meant to return this value
}

None
}
20 changes: 18 additions & 2 deletions tests/ui/return/tail-expr-as-potential-return.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ help: you might have meant to return this value
LL | return Err(42);
| ++++++ +

error: aborting due to 3 previous errors
error[E0282]: type annotations needed
--> $DIR/tail-expr-as-potential-return.rs:57:18
|
LL | Receiver.generic();
| ^^^^^^^ cannot infer type of the type parameter `T` declared on the method `generic`
|
help: consider specifying the generic argument
|
LL | Receiver.generic::<T>();
| +++++
help: you might have meant to return this to infer its type parameters
|
LL | return Receiver.generic();
| ++++++

error: aborting due to 4 previous errors

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

0 comments on commit d5bddee

Please sign in to comment.