Skip to content

Commit

Permalink
Note that type param is chosen by caller when suggesting return impl …
Browse files Browse the repository at this point in the history
…Trait

Revives rust-lang#112088 and rust-lang#104755.

Co-authored-by: sladynnunes <snunes@usc.edu>
Co-authored-by: Thalia Nero <chrisvn00@gmail.com>
  • Loading branch information
3 people committed Mar 8, 2024
1 parent 1b2c53a commit 7852895
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
7 changes: 6 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.dcx(),
errors::ExpectedReturnTypeLabel::Other { span: hir_ty.span, expected },
);
self.try_suggest_return_impl_trait(err, expected, ty, fn_id);
self.try_suggest_return_impl_trait(err, expected, found, fn_id);
return true;
}
}
Expand Down Expand Up @@ -1010,6 +1010,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
format!("impl {all_bounds_str}"),
Applicability::MaybeIncorrect,
);

err.note(format!(
"the caller chooses the type of {} which can be different from {}",
expected_ty_as_param.name, found
));
}

pub(in super::super) fn suggest_missing_break_or_return_expr(
Expand Down
1 change: 1 addition & 0 deletions tests/ui/return/return-impl-trait-bad.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ LL | "don't suggest this, because the generic param is used in the bound."
|
= note: expected type parameter `T`
found reference `&'static str`
= note: the caller chooses the type of T which can be different from &'static str

error: aborting due to 4 previous errors

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/return/return-impl-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ LL | ()
|
= note: expected type parameter `T`
found unit type `()`
= note: the caller chooses the type of T which can be different from ()

error[E0308]: mismatched types
--> $DIR/return-impl-trait.rs:23:5
Expand All @@ -28,6 +29,7 @@ LL | ()
|
= note: expected type parameter `T`
found unit type `()`
= note: the caller chooses the type of T which can be different from ()

error: aborting due to 2 previous errors

Expand Down

0 comments on commit 7852895

Please sign in to comment.