Skip to content

Commit 0f368e3

Browse files
committed
Note that type param is chosen by caller when suggesting return impl Trait
1 parent b054da8 commit 0f368e3

File tree

5 files changed

+77
-2
lines changed

5 files changed

+77
-2
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
118118
) -> Ty<'tcx> {
119119
let ty = self.check_expr_with_hint(expr, expected);
120120
// checks don't need two phase
121-
self.demand_coerce(expr, ty, expected, expected_ty_expr, AllowTwoPhase::No)
121+
let (res_ty, diag) =
122+
self.demand_coerce_diag(expr, ty, expected, expected_ty_expr, AllowTwoPhase::No);
123+
124+
if let Some(mut diag) = diag {
125+
if let ty::Param(expected_ty_as_param) = expected.kind() {
126+
diag.note(format!(
127+
"the caller chooses a type for `{}` which can be different from `{}`",
128+
expected_ty_as_param.name, ty
129+
));
130+
}
131+
132+
diag.emit();
133+
}
134+
135+
res_ty
122136
}
123137

124138
pub(super) fn check_expr_with_hint(

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
889889
self.dcx(),
890890
errors::ExpectedReturnTypeLabel::Other { span: hir_ty.span, expected },
891891
);
892-
self.try_suggest_return_impl_trait(err, expected, ty, fn_id);
892+
self.try_suggest_return_impl_trait(err, expected, found, fn_id);
893893
return true;
894894
}
895895
}

tests/ui/const-generics/issues/issue-67945-1.full.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LL | let x: S = MaybeUninit::uninit();
1111
|
1212
= note: expected type parameter `S`
1313
found union `MaybeUninit<_>`
14+
= note: the caller chooses a type for `S` which can be different from `MaybeUninit<_>`
1415

1516
error: aborting due to 1 previous error
1617

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Checks existence of a note for "a caller chooses ty for ty param" upon return ty mismatch.
2+
3+
fn f<T>() -> (T,) {
4+
(0,) //~ ERROR mismatched types
5+
}
6+
7+
fn g<U, V>() -> (U, V) {
8+
(0, "foo")
9+
//~^ ERROR mismatched types
10+
//~| ERROR mismatched types
11+
}
12+
13+
fn h() -> u8 {
14+
0u8
15+
}
16+
17+
fn main() {
18+
f::<()>();
19+
g::<(), ()>;
20+
let _ = h();
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/return-ty-mismatch-note.rs:4:6
3+
|
4+
LL | fn f<T>() -> (T,) {
5+
| - expected this type parameter
6+
LL | (0,)
7+
| ^ expected type parameter `T`, found integer
8+
|
9+
= note: expected type parameter `T`
10+
found type `{integer}`
11+
= note: the caller chooses a type for `T` which can be different from `{integer}`
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/return-ty-mismatch-note.rs:8:6
15+
|
16+
LL | fn g<U, V>() -> (U, V) {
17+
| - expected this type parameter
18+
LL | (0, "foo")
19+
| ^ expected type parameter `U`, found integer
20+
|
21+
= note: expected type parameter `U`
22+
found type `{integer}`
23+
= note: the caller chooses a type for `U` which can be different from `{integer}`
24+
25+
error[E0308]: mismatched types
26+
--> $DIR/return-ty-mismatch-note.rs:8:9
27+
|
28+
LL | fn g<U, V>() -> (U, V) {
29+
| - expected this type parameter
30+
LL | (0, "foo")
31+
| ^^^^^ expected type parameter `V`, found `&str`
32+
|
33+
= note: expected type parameter `V`
34+
found reference `&'static str`
35+
= note: the caller chooses a type for `V` which can be different from `&'static str`
36+
37+
error: aborting due to 3 previous errors
38+
39+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)