Skip to content

Commit bd8b468

Browse files
committed
Tweak wording of type errors involving type params
Fix #78206.
1 parent cc705b8 commit bd8b468

31 files changed

+72
-55
lines changed

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
8585
{
8686
let p_def_id = tcx.generics_of(body_owner_def_id).type_param(p, tcx).def_id;
8787
let p_span = tcx.def_span(p_def_id);
88+
let expected = match (values.expected.kind(), values.found.kind()) {
89+
(ty::Param(_), _) => "expected ",
90+
(_, ty::Param(_)) => "found ",
91+
_ => "",
92+
};
8893
if !sp.contains(p_span) {
89-
diag.span_label(p_span, "this type parameter");
94+
diag.span_label(p_span, format!("{expected}this type parameter"));
9095
}
9196
let hir = tcx.hir();
9297
let mut note = true;
@@ -168,8 +173,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
168173
| (ty::Dynamic(..) | ty::Alias(ty::Opaque, ..), ty::Param(p)) => {
169174
let generics = tcx.generics_of(body_owner_def_id);
170175
let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
176+
let expected = match (values.expected.kind(), values.found.kind()) {
177+
(ty::Param(_), _) => "expected ",
178+
(_, ty::Param(_)) => "found ",
179+
_ => "",
180+
};
171181
if !sp.contains(p_span) {
172-
diag.span_label(p_span, "this type parameter");
182+
diag.span_label(p_span, format!("{expected}this type parameter"));
173183
}
174184
diag.help("type parameters must be constrained to match other types");
175185
if tcx.sess.teach(&diag.get_code().unwrap()) {
@@ -209,7 +219,7 @@ impl<T> Trait<T> for X {
209219
let generics = tcx.generics_of(body_owner_def_id);
210220
let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
211221
if !sp.contains(p_span) {
212-
diag.span_label(p_span, "this type parameter");
222+
diag.span_label(p_span, "expected this type parameter");
213223
}
214224
diag.help(format!(
215225
"every closure has a distinct type and so could not always match the \
@@ -219,8 +229,13 @@ impl<T> Trait<T> for X {
219229
(ty::Param(p), _) | (_, ty::Param(p)) => {
220230
let generics = tcx.generics_of(body_owner_def_id);
221231
let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
232+
let expected = match (values.expected.kind(), values.found.kind()) {
233+
(ty::Param(_), _) => "expected ",
234+
(_, ty::Param(_)) => "found ",
235+
_ => "",
236+
};
222237
if !sp.contains(p_span) {
223-
diag.span_label(p_span, "this type parameter");
238+
diag.span_label(p_span, format!("{expected}this type parameter"));
224239
}
225240
}
226241
(ty::Alias(ty::Projection | ty::Inherent, proj_ty), _)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ error[E0308]: mismatched types
1313
LL | fn foo<u32>(a: u32) -> u32 {
1414
| --- --- expected `u32` because of return type
1515
| |
16-
| this type parameter
16+
| expected this type parameter
1717
LL | 42
1818
| ^^ expected type parameter `u32`, found integer
1919
|

tests/ui/associated-type-bounds/elision.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LL | fn f(x: &mut dyn Iterator<Item: Iterator<Item = &'_ ()>>) -> Option<&'_ ()>
1717
| ----------------------------- -------------- ^^^^^^^^ expected `Option<&()>`, found `Option<impl Iterator<Item = &'_ ()>>`
1818
| | |
1919
| | expected `Option<&()>` because of return type
20-
| this type parameter
20+
| found this type parameter
2121
|
2222
= note: expected enum `Option<&()>`
2323
found enum `Option<impl Iterator<Item = &'_ ()>>`

tests/ui/associated-types/associated-types-issue-20346.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0271]: type mismatch resolving `<Adapter<I> as Iterator>::Item == Option<
22
--> $DIR/associated-types-issue-20346.rs:34:36
33
|
44
LL | fn test_adapter<T, I: Iterator<Item=Option<T>>>(it: I) {
5-
| - this type parameter
5+
| - found this type parameter
66
...
77
LL | is_iterator_of::<Option<T>, _>(&adapter);
88
| ------------------------------ ^^^^^^^^ type mismatch resolving `<Adapter<I> as Iterator>::Item == Option<T>`

tests/ui/associated-types/hr-associated-type-projection-1.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0271]: type mismatch resolving `<T as Deref>::Target == T`
22
--> $DIR/hr-associated-type-projection-1.rs:13:33
33
|
44
LL | impl<T: Copy + std::ops::Deref> UnsafeCopy<'_, T> for T {
5-
| - this type parameter ^^^^^^^^^^^^^^^^^ expected type parameter `T`, found associated type
5+
| - ^^^^^^^^^^^^^^^^^ expected type parameter `T`, found associated type
6+
| |
7+
| expected this type parameter
68
|
79
= note: expected type parameter `T`
810
found associated type `<T as Deref>::Target`

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/issue-67945-1.rs:10:20
33
|
44
LL | struct Bug<S> {
5-
| - this type parameter
5+
| - expected this type parameter
66
...
77
LL | let x: S = MaybeUninit::uninit();
88
| - ^^^^^^^^^^^^^^^^^^^^^ expected type parameter `S`, found `MaybeUninit<_>`

tests/ui/generic-associated-types/issue-68648-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/issue-68648-2.rs:12:17
33
|
44
LL | fn bug<'a, T: Fun<F<'a> = T>>(t: T) -> T::F<'a> {
5-
| - this type parameter
5+
| - expected this type parameter
66
LL | T::identity(())
77
| ----------- ^^ expected type parameter `T`, found `()`
88
| |

tests/ui/generic-associated-types/issue-68656-unsized-values.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0271]: type mismatch resolving `<T as Deref>::Target == T`
22
--> $DIR/issue-68656-unsized-values.rs:13:21
33
|
44
LL | impl<T: Copy + std::ops::Deref> UnsafeCopy<T> for T {
5-
| - this type parameter
5+
| - expected this type parameter
66
LL | type Item<'a> = T;
77
| ^ expected type parameter `T`, found associated type
88
|

tests/ui/generic-associated-types/issue-88360.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/issue-88360.rs:15:9
33
|
44
LL | trait SuperTrait<T>
5-
| - this type parameter
5+
| - found this type parameter
66
...
77
LL | fn copy(&self) -> Self::Gat<'_> where T: Copy {
88
| ------------- expected `&T` because of return type

tests/ui/generic-associated-types/missing-bounds.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error[E0308]: mismatched types
1414
--> $DIR/missing-bounds.rs:11:11
1515
|
1616
LL | impl<B> Add for A<B> where B: Add {
17-
| - this type parameter
17+
| - expected this type parameter
1818
...
1919
LL | A(self.0 + rhs.0)
2020
| - ^^^^^^^^^^^^^^ expected type parameter `B`, found associated type
@@ -44,7 +44,7 @@ error[E0308]: mismatched types
4444
--> $DIR/missing-bounds.rs:21:14
4545
|
4646
LL | impl<B: Add> Add for C<B> {
47-
| - this type parameter
47+
| - expected this type parameter
4848
...
4949
LL | Self(self.0 + rhs.0)
5050
| ---- ^^^^^^^^^^^^^^ expected type parameter `B`, found associated type
@@ -80,7 +80,7 @@ error[E0308]: mismatched types
8080
--> $DIR/missing-bounds.rs:42:14
8181
|
8282
LL | impl<B: Add> Add for E<B> where <B as Add>::Output = B {
83-
| - this type parameter
83+
| - expected this type parameter
8484
...
8585
LL | Self(self.0 + rhs.0)
8686
| ---- ^^^^^^^^^^^^^^ expected type parameter `B`, found associated type

tests/ui/impl-trait/arg-position-impl-trait-too-long.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | | &mut Header,
77
LL | | &mut [EntryMetadata],
88
LL | | &mut [Entry<C::EncodedKey, C::EncodedValue>]
99
LL | | ) -> R,
10-
| |__________- this type parameter
10+
| |__________- expected this type parameter
1111
LL | ) {
1212
LL | let () = y;
1313
| ^^ - this expression has type `impl FnOnce(&mut Header, &mut [EntryMetadata], &mut [Entry<C::EncodedKey, C::EncodedValue>]) -> R`

tests/ui/impl-trait/in-trait/method-signature-matches.lt.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | fn early<'late, T>(_: &'late ()) {}
66
| | |
77
| | expected type parameter `T`, found `()`
88
| | help: change the parameter type to match the trait: `&T`
9-
| this type parameter
9+
| expected this type parameter
1010
|
1111
note: type in trait
1212
--> $DIR/method-signature-matches.rs:52:28

tests/ui/impl-trait/in-trait/specialization-broken.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0053]: method `bar` has an incompatible type for trait
22
--> $DIR/specialization-broken.rs:15:22
33
|
44
LL | default impl<U> Foo for U
5-
| - this type parameter
5+
| - found this type parameter
66
...
77
LL | fn bar(&self) -> U {
88
| ^

tests/ui/impl-trait/universal-mismatched-type.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0308]: mismatched types
44
LL | fn foo(x: impl Debug) -> String {
55
| ---------- ------ expected `String` because of return type
66
| |
7-
| this type parameter
7+
| found this type parameter
88
LL | x
99
| ^ expected `String`, found type parameter `impl Debug`
1010
|

tests/ui/inference/issue-107090.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ error[E0308]: mismatched types
7070
--> $DIR/issue-107090.rs:22:5
7171
|
7272
LL | fn badboi<'in_, 'out, T>(x: Foo<'in_, 'out, T>, sadness: &'in_ Foo<'short, 'out, T>) -> &'out T {
73-
| - this type parameter ------- expected `&'out T` because of return type
73+
| - expected this type parameter ------- expected `&'out T` because of return type
7474
LL |
7575
LL | sadness.cast()
7676
| ^^^^^^^^^^^^^^ expected `&T`, found `&Foo<'_, '_, T>`

tests/ui/issues/issue-20225.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0053]: method `call` has an incompatible type for trait
22
--> $DIR/issue-20225.rs:6:43
33
|
44
LL | impl<'a, T> Fn<(&'a T,)> for Foo {
5-
| - this type parameter
5+
| - found this type parameter
66
LL | extern "rust-call" fn call(&self, (_,): (T,)) {}
77
| ^^^^
88
| |
@@ -16,7 +16,7 @@ error[E0053]: method `call_mut` has an incompatible type for trait
1616
--> $DIR/issue-20225.rs:11:51
1717
|
1818
LL | impl<'a, T> FnMut<(&'a T,)> for Foo {
19-
| - this type parameter
19+
| - found this type parameter
2020
LL | extern "rust-call" fn call_mut(&mut self, (_,): (T,)) {}
2121
| ^^^^
2222
| |
@@ -30,7 +30,7 @@ error[E0053]: method `call_once` has an incompatible type for trait
3030
--> $DIR/issue-20225.rs:18:47
3131
|
3232
LL | impl<'a, T> FnOnce<(&'a T,)> for Foo {
33-
| - this type parameter
33+
| - found this type parameter
3434
...
3535
LL | extern "rust-call" fn call_once(self, (_,): (T,)) {}
3636
| ^^^^

tests/ui/issues/issue-51154.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/issue-51154.rs:2:30
33
|
44
LL | fn foo<F: FnMut()>() {
5-
| - this type parameter
5+
| - expected this type parameter
66
LL | let _: Box<F> = Box::new(|| ());
77
| -------- ^^^^^ expected type parameter `F`, found closure
88
| |

tests/ui/issues/issue-69306.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/issue-69306.rs:5:28
33
|
44
LL | impl<T> S0<T> {
5-
| - this type parameter
5+
| - expected this type parameter
66
LL | const C: S0<u8> = Self(0);
77
| ---- ^ expected type parameter `T`, found integer
88
| |
@@ -20,7 +20,7 @@ error[E0308]: mismatched types
2020
--> $DIR/issue-69306.rs:5:23
2121
|
2222
LL | impl<T> S0<T> {
23-
| - this type parameter
23+
| - found this type parameter
2424
LL | const C: S0<u8> = Self(0);
2525
| ^^^^^^^ expected `S0<u8>`, found `S0<T>`
2626
|
@@ -31,7 +31,7 @@ error[E0308]: mismatched types
3131
--> $DIR/issue-69306.rs:10:14
3232
|
3333
LL | impl<T> S0<T> {
34-
| - this type parameter
34+
| - expected this type parameter
3535
...
3636
LL | Self(0);
3737
| ---- ^ expected type parameter `T`, found integer
@@ -50,7 +50,7 @@ error[E0308]: mismatched types
5050
--> $DIR/issue-69306.rs:27:14
5151
|
5252
LL | impl<T> Foo<T> for <S0<T> as Fun>::Out {
53-
| - this type parameter
53+
| - expected this type parameter
5454
LL | fn foo() {
5555
LL | Self(0);
5656
| ---- ^ expected type parameter `T`, found integer
@@ -69,7 +69,7 @@ error[E0308]: mismatched types
6969
--> $DIR/issue-69306.rs:33:32
7070
|
7171
LL | impl<T> S1<T, u8> {
72-
| - this type parameter
72+
| - expected this type parameter
7373
LL | const C: S1<u8, u8> = Self(0, 1);
7474
| ---- ^ expected type parameter `T`, found integer
7575
| |
@@ -87,7 +87,7 @@ error[E0308]: mismatched types
8787
--> $DIR/issue-69306.rs:33:27
8888
|
8989
LL | impl<T> S1<T, u8> {
90-
| - this type parameter
90+
| - found this type parameter
9191
LL | const C: S1<u8, u8> = Self(0, 1);
9292
| ^^^^^^^^^^ expected `S1<u8, u8>`, found `S1<T, u8>`
9393
|

tests/ui/lang-items/start_lang_item_args.main_ret.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0308]: lang item `start` function has wrong type
44
LL | fn start<T>(_main: fn() -> u16, _argc: isize, _argv: *const *const u8, _sigpipe: u8) -> isize {
55
| - ^^^^^^^^^^^ expected type parameter `T`, found `u16`
66
| |
7-
| this type parameter
7+
| expected this type parameter
88
|
99
= note: expected signature `fn(fn() -> T, _, _, _) -> _`
1010
found signature `fn(fn() -> u16, _, _, _) -> _`

tests/ui/mismatched_types/issue-35030.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/issue-35030.rs:9:14
33
|
44
LL | impl<bool> Parser<bool> for bool {
5-
| ---- this type parameter
5+
| ---- expected this type parameter
66
LL | fn parse(text: &str) -> Option<bool> {
77
LL | Some(true)
88
| ---- ^^^^ expected type parameter `bool`, found `bool`

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0308]: mismatched types
44
LL | fn bad_echo<T>(_t: T) -> T {
55
| - - expected `T` because of return type
66
| |
7-
| this type parameter
7+
| expected this type parameter
88
LL | "this should not suggest impl Trait"
99
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `&str`
1010
|
@@ -17,7 +17,7 @@ error[E0308]: mismatched types
1717
LL | fn bad_echo_2<T: Trait>(_t: T) -> T {
1818
| - - expected `T` because of return type
1919
| |
20-
| this type parameter
20+
| expected this type parameter
2121
LL | "this will not suggest it, because that would probably be wrong"
2222
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `&str`
2323
|
@@ -30,7 +30,7 @@ error[E0308]: mismatched types
3030
LL | fn other_bounds_bad<T>() -> T
3131
| - - expected `T` because of return type
3232
| |
33-
| this type parameter
33+
| expected this type parameter
3434
...
3535
LL | "don't suggest this, because Option<T> places additional constraints"
3636
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `&str`
@@ -46,7 +46,7 @@ LL | fn used_in_trait<T>() -> T
4646
| | |
4747
| | expected `T` because of return type
4848
| | help: consider using an impl return type: `impl Send`
49-
| this type parameter
49+
| expected this type parameter
5050
...
5151
LL | "don't suggest this, because the generic param is used in the bound."
5252
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `&str`

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | fn bar<T: Trait + std::marker::Sync>() -> T
55
| - -
66
| | |
77
| | expected `T` because of return type
8-
| this type parameter help: consider using an impl return type: `impl Trait + std::marker::Sync + Send`
8+
| expected this type parameter help: consider using an impl return type: `impl Trait + std::marker::Sync + Send`
99
...
1010
LL | ()
1111
| ^^ expected type parameter `T`, found `()`
@@ -21,7 +21,7 @@ LL | fn other_bounds<T>() -> T
2121
| | |
2222
| | expected `T` because of return type
2323
| | help: consider using an impl return type: `impl Trait`
24-
| this type parameter
24+
| expected this type parameter
2525
...
2626
LL | ()
2727
| ^^ expected type parameter `T`, found `()`

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0308]: mismatched types
44
LL | fn wat<T>(t: &T) -> T {
55
| - - expected `T` because of return type
66
| |
7-
| this type parameter
7+
| expected this type parameter
88
LL | t.clone()
99
| ^^^^^^^^^ expected type parameter `T`, found `&T`
1010
|

tests/ui/suggestions/expected-boxed-future-isnt-pinned.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/expected-boxed-future-isnt-pinned.rs:11:5
33
|
44
LL | fn foo<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
5-
| - this type parameter ----------------------- expected `Pin<Box<(dyn Future<Output = i32> + Send + 'static)>>` because of return type
5+
| - found this type parameter ----------------------- expected `Pin<Box<(dyn Future<Output = i32> + Send + 'static)>>` because of return type
66
LL | // We could instead use an `async` block, but this way we have no std spans.
77
LL | x
88
| ^ expected `Pin<Box<...>>`, found type parameter `F`
@@ -30,7 +30,7 @@ error[E0308]: mismatched types
3030
--> $DIR/expected-boxed-future-isnt-pinned.rs:19:14
3131
|
3232
LL | fn baz<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
33-
| - this type parameter
33+
| - found this type parameter
3434
LL | Pin::new(x)
3535
| -------- ^ expected `Box<dyn Future<Output = ...> + Send>`, found type parameter `F`
3636
| |

0 commit comments

Comments
 (0)