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
  • Loading branch information
jieyouxu committed Mar 9, 2024
1 parent b054da8 commit 0f368e3
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
16 changes: 15 additions & 1 deletion compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) -> Ty<'tcx> {
let ty = self.check_expr_with_hint(expr, expected);
// checks don't need two phase
self.demand_coerce(expr, ty, expected, expected_ty_expr, AllowTwoPhase::No)
let (res_ty, diag) =
self.demand_coerce_diag(expr, ty, expected, expected_ty_expr, AllowTwoPhase::No);

if let Some(mut diag) = diag {
if let ty::Param(expected_ty_as_param) = expected.kind() {
diag.note(format!(
"the caller chooses a type for `{}` which can be different from `{}`",
expected_ty_as_param.name, ty
));
}

diag.emit();
}

res_ty
}

pub(super) fn check_expr_with_hint(
Expand Down
2 changes: 1 addition & 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
1 change: 1 addition & 0 deletions tests/ui/const-generics/issues/issue-67945-1.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ LL | let x: S = MaybeUninit::uninit();
|
= note: expected type parameter `S`
found union `MaybeUninit<_>`
= note: the caller chooses a type for `S` which can be different from `MaybeUninit<_>`

error: aborting due to 1 previous error

Expand Down
21 changes: 21 additions & 0 deletions tests/ui/typeck/return-ty-mismatch-note.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Checks existence of a note for "a caller chooses ty for ty param" upon return ty mismatch.

fn f<T>() -> (T,) {
(0,) //~ ERROR mismatched types
}

fn g<U, V>() -> (U, V) {
(0, "foo")
//~^ ERROR mismatched types
//~| ERROR mismatched types
}

fn h() -> u8 {
0u8
}

fn main() {
f::<()>();
g::<(), ()>;
let _ = h();
}
39 changes: 39 additions & 0 deletions tests/ui/typeck/return-ty-mismatch-note.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
error[E0308]: mismatched types
--> $DIR/return-ty-mismatch-note.rs:4:6
|
LL | fn f<T>() -> (T,) {
| - expected this type parameter
LL | (0,)
| ^ expected type parameter `T`, found integer
|
= note: expected type parameter `T`
found type `{integer}`
= note: the caller chooses a type for `T` which can be different from `{integer}`

error[E0308]: mismatched types
--> $DIR/return-ty-mismatch-note.rs:8:6
|
LL | fn g<U, V>() -> (U, V) {
| - expected this type parameter
LL | (0, "foo")
| ^ expected type parameter `U`, found integer
|
= note: expected type parameter `U`
found type `{integer}`
= note: the caller chooses a type for `U` which can be different from `{integer}`

error[E0308]: mismatched types
--> $DIR/return-ty-mismatch-note.rs:8:9
|
LL | fn g<U, V>() -> (U, V) {
| - expected this type parameter
LL | (0, "foo")
| ^^^^^ expected type parameter `V`, found `&str`
|
= note: expected type parameter `V`
found reference `&'static str`
= note: the caller chooses a type for `V` which can be different from `&'static str`

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0308`.

0 comments on commit 0f368e3

Please sign in to comment.