Skip to content

Commit cbe798f

Browse files
authored
Unrolled build for rust-lang#122195
Rollup merge of rust-lang#122195 - jieyouxu:impl-return-note, r=fmease Note that the caller chooses a type for type param ``` error[E0308]: mismatched types --> $DIR/return-impl-trait.rs:23:5 | LL | fn other_bounds<T>() -> T | - - | | | | | expected `T` because of return type | | help: consider using an impl return type: `impl Trait` | expected this type parameter ... LL | () | ^^ expected type parameter `T`, found `()` | = note: expected type parameter `T` found unit type `()` = note: the caller chooses the type of T which can be different from () ``` Tried to see if "expected this type parameter" can be replaced, but that goes all the way to `rustc_infer` so seems not worth the effort and can affect other diagnostics. Revives rust-lang#112088 and rust-lang#104755.
2 parents 85e449a + cacdf92 commit cbe798f

13 files changed

+98
-2
lines changed

compiler/rustc_hir_typeck/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ hir_typeck_no_associated_item = no {$item_kind} named `{$item_name}` found for {
103103
*[other] {" "}in the current scope
104104
}
105105
106+
hir_typeck_note_caller_chooses_ty_for_ty_param = the caller chooses a type for `{$ty_param_name}` which can be different from `{$found_ty}`
107+
106108
hir_typeck_note_edition_guide = for more on editions, read https://doc.rust-lang.org/edition-guide
107109
108110
hir_typeck_option_result_asref = use `{$def_path}::as_ref` to convert `{$expected_ty}` to `{$expr_ty}`

compiler/rustc_hir_typeck/src/errors.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_middle::ty::Ty;
1111
use rustc_span::{
1212
edition::{Edition, LATEST_STABLE_EDITION},
1313
symbol::Ident,
14-
Span,
14+
Span, Symbol,
1515
};
1616

1717
#[derive(Diagnostic)]
@@ -614,3 +614,10 @@ pub struct SuggestConvertViaMethod<'tcx> {
614614
pub expected: Ty<'tcx>,
615615
pub found: Ty<'tcx>,
616616
}
617+
618+
#[derive(Subdiagnostic)]
619+
#[note(hir_typeck_note_caller_chooses_ty_for_ty_param)]
620+
pub struct NoteCallerChoosesTyForTyParam<'tcx> {
621+
pub ty_param_name: Symbol,
622+
pub found_ty: Ty<'tcx>,
623+
}

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
888888
self.dcx(),
889889
errors::ExpectedReturnTypeLabel::Other { span: hir_ty.span, expected },
890890
);
891-
self.try_suggest_return_impl_trait(err, expected, ty, fn_id);
891+
self.try_suggest_return_impl_trait(err, expected, found, fn_id);
892+
self.note_caller_chooses_ty_for_ty_param(err, expected, found);
892893
return true;
893894
}
894895
}
@@ -898,6 +899,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
898899
false
899900
}
900901

902+
fn note_caller_chooses_ty_for_ty_param(
903+
&self,
904+
diag: &mut Diag<'_>,
905+
expected: Ty<'tcx>,
906+
found: Ty<'tcx>,
907+
) {
908+
if let ty::Param(expected_ty_as_param) = expected.kind() {
909+
diag.subdiagnostic(
910+
self.dcx(),
911+
errors::NoteCallerChoosesTyForTyParam {
912+
ty_param_name: expected_ty_as_param.name,
913+
found_ty: found,
914+
},
915+
);
916+
}
917+
}
918+
901919
/// check whether the return type is a generic type with a trait bound
902920
/// only suggest this if the generic param is not present in the arguments
903921
/// if this is true, hint them towards changing the return type to `impl Trait`

src/tools/clippy/tests/ui/builtin_type_shadow.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ LL | 42
1919
|
2020
= note: expected type parameter `u32`
2121
found type `{integer}`
22+
= note: the caller chooses a type for `u32` which can be different from `i32`
2223

2324
error: aborting due to 2 previous errors
2425

tests/ui/return/return-impl-trait-bad.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ LL | "this should not suggest impl Trait"
1010
|
1111
= note: expected type parameter `T`
1212
found reference `&'static str`
13+
= note: the caller chooses a type for `T` which can be different from `&'static str`
1314

1415
error[E0308]: mismatched types
1516
--> $DIR/return-impl-trait-bad.rs:9:5
@@ -23,6 +24,7 @@ LL | "this will not suggest it, because that would probably be wrong"
2324
|
2425
= note: expected type parameter `T`
2526
found reference `&'static str`
27+
= note: the caller chooses a type for `T` which can be different from `&'static str`
2628

2729
error[E0308]: mismatched types
2830
--> $DIR/return-impl-trait-bad.rs:17:5
@@ -37,6 +39,7 @@ LL | "don't suggest this, because Option<T> places additional constraints"
3739
|
3840
= note: expected type parameter `T`
3941
found reference `&'static str`
42+
= note: the caller chooses a type for `T` which can be different from `&'static str`
4043

4144
error[E0308]: mismatched types
4245
--> $DIR/return-impl-trait-bad.rs:28:5
@@ -53,6 +56,7 @@ LL | "don't suggest this, because the generic param is used in the bound."
5356
|
5457
= note: expected type parameter `T`
5558
found reference `&'static str`
59+
= note: the caller chooses a type for `T` which can be different from `&'static str`
5660

5761
error: aborting due to 4 previous errors
5862

tests/ui/return/return-impl-trait.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ LL | ()
1212
|
1313
= note: expected type parameter `T`
1414
found unit type `()`
15+
= note: the caller chooses a type for `T` which can be different from `()`
1516

1617
error[E0308]: mismatched types
1718
--> $DIR/return-impl-trait.rs:23:5
@@ -28,6 +29,7 @@ LL | ()
2829
|
2930
= note: expected type parameter `T`
3031
found unit type `()`
32+
= note: the caller chooses a type for `T` which can be different from `()`
3133

3234
error: aborting due to 2 previous errors
3335

+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,36 @@
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+
12+
error[E0308]: mismatched types
13+
--> $DIR/return-ty-mismatch-note.rs:8:6
14+
|
15+
LL | fn g<U, V>() -> (U, V) {
16+
| - expected this type parameter
17+
LL | (0, "foo")
18+
| ^ expected type parameter `U`, found integer
19+
|
20+
= note: expected type parameter `U`
21+
found type `{integer}`
22+
23+
error[E0308]: mismatched types
24+
--> $DIR/return-ty-mismatch-note.rs:8:9
25+
|
26+
LL | fn g<U, V>() -> (U, V) {
27+
| - expected this type parameter
28+
LL | (0, "foo")
29+
| ^^^^^ expected type parameter `V`, found `&str`
30+
|
31+
= note: expected type parameter `V`
32+
found reference `&'static str`
33+
34+
error: aborting due to 3 previous errors
35+
36+
For more information about this error, try `rustc --explain E0308`.

tests/ui/suggestions/clone-on-unconstrained-borrowed-type-param.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ LL | t.clone()
1010
|
1111
= note: expected type parameter `_`
1212
found reference `&_`
13+
= note: the caller chooses a type for `T` which can be different from `&T`
1314
note: `T` does not implement `Clone`, so `&T` was cloned instead
1415
--> $DIR/clone-on-unconstrained-borrowed-type-param.rs:3:5
1516
|

tests/ui/trait-bounds/restrict-assoc-type-of-generic-bound.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ LL | return a.bar();
1010
|
1111
= note: expected type parameter `B`
1212
found associated type `<A as MyTrait>::T`
13+
= note: the caller chooses a type for `B` which can be different from `<A as MyTrait>::T`
1314
help: consider further restricting this bound
1415
|
1516
LL | pub fn foo<A: MyTrait<T = B>, B>(a: A) -> B {

tests/ui/type/type-parameter-names.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ LL | x
1313
found type parameter `Foo`
1414
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
1515
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
16+
= note: the caller chooses a type for `Bar` which can be different from `Foo`
1617

1718
error: aborting due to 1 previous error
1819

tests/ui/type/type-params-in-different-spaces-3.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LL | u
1414
found type parameter `X`
1515
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
1616
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
17+
= note: the caller chooses a type for `Self` which can be different from `X`
1718

1819
error: aborting due to 1 previous error
1920

tests/ui/typeck/issue-13853.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LL | self.iter()
99
|
1010
= note: expected type parameter `I`
1111
found struct `std::slice::Iter<'_, N>`
12+
= note: the caller chooses a type for `I` which can be different from `std::slice::Iter<'_, N>`
1213

1314
error[E0599]: no method named `iter` found for reference `&G` in the current scope
1415
--> $DIR/issue-13853.rs:27:23

0 commit comments

Comments
 (0)