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 #112088 and #104755.

Co-authored-by: sladynnunes <[email protected]>
Co-authored-by: Thalia Nero <[email protected]>
  • Loading branch information
3 people committed Mar 8, 2024
1 parent 74acabe commit a29ac16
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 a type for `{}` 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 a type for `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 a type for `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 a type for `T` which can be different from `()`

error: aborting due to 2 previous errors

Expand Down

0 comments on commit a29ac16

Please sign in to comment.