Skip to content

Commit

Permalink
Rollup merge of rust-lang#116908 - estebank:issue-78206, r=compiler-e…
Browse files Browse the repository at this point in the history
…rrors

Tweak wording of type errors involving type params

Fix rust-lang#78206.
  • Loading branch information
fmease authored Oct 19, 2023
2 parents 42c7d21 + bd8b468 commit 64ed233
Show file tree
Hide file tree
Showing 31 changed files with 72 additions and 55 deletions.
23 changes: 19 additions & 4 deletions compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
{
let p_def_id = tcx.generics_of(body_owner_def_id).type_param(p, tcx).def_id;
let p_span = tcx.def_span(p_def_id);
let expected = match (values.expected.kind(), values.found.kind()) {
(ty::Param(_), _) => "expected ",
(_, ty::Param(_)) => "found ",
_ => "",
};
if !sp.contains(p_span) {
diag.span_label(p_span, "this type parameter");
diag.span_label(p_span, format!("{expected}this type parameter"));
}
let hir = tcx.hir();
let mut note = true;
Expand Down Expand Up @@ -168,8 +173,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
| (ty::Dynamic(..) | ty::Alias(ty::Opaque, ..), ty::Param(p)) => {
let generics = tcx.generics_of(body_owner_def_id);
let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
let expected = match (values.expected.kind(), values.found.kind()) {
(ty::Param(_), _) => "expected ",
(_, ty::Param(_)) => "found ",
_ => "",
};
if !sp.contains(p_span) {
diag.span_label(p_span, "this type parameter");
diag.span_label(p_span, format!("{expected}this type parameter"));
}
diag.help("type parameters must be constrained to match other types");
if tcx.sess.teach(&diag.get_code().unwrap()) {
Expand Down Expand Up @@ -209,7 +219,7 @@ impl<T> Trait<T> for X {
let generics = tcx.generics_of(body_owner_def_id);
let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
if !sp.contains(p_span) {
diag.span_label(p_span, "this type parameter");
diag.span_label(p_span, "expected this type parameter");
}
diag.help(format!(
"every closure has a distinct type and so could not always match the \
Expand All @@ -219,8 +229,13 @@ impl<T> Trait<T> for X {
(ty::Param(p), _) | (_, ty::Param(p)) => {
let generics = tcx.generics_of(body_owner_def_id);
let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
let expected = match (values.expected.kind(), values.found.kind()) {
(ty::Param(_), _) => "expected ",
(_, ty::Param(_)) => "found ",
_ => "",
};
if !sp.contains(p_span) {
diag.span_label(p_span, "this type parameter");
diag.span_label(p_span, format!("{expected}this type parameter"));
}
}
(ty::Alias(ty::Projection | ty::Inherent, proj_ty), _)
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/tests/ui/builtin_type_shadow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ error[E0308]: mismatched types
LL | fn foo<u32>(a: u32) -> u32 {
| --- --- expected `u32` because of return type
| |
| this type parameter
| expected this type parameter
LL | 42
| ^^ expected type parameter `u32`, found integer
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/associated-type-bounds/elision.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ LL | fn f(x: &mut dyn Iterator<Item: Iterator<Item = &'_ ()>>) -> Option<&'_ ()>
| ----------------------------- -------------- ^^^^^^^^ expected `Option<&()>`, found `Option<impl Iterator<Item = &'_ ()>>`
| | |
| | expected `Option<&()>` because of return type
| this type parameter
| found this type parameter
|
= note: expected enum `Option<&()>`
found enum `Option<impl Iterator<Item = &'_ ()>>`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0271]: type mismatch resolving `<Adapter<I> as Iterator>::Item == Option<
--> $DIR/associated-types-issue-20346.rs:34:36
|
LL | fn test_adapter<T, I: Iterator<Item=Option<T>>>(it: I) {
| - this type parameter
| - found this type parameter
...
LL | is_iterator_of::<Option<T>, _>(&adapter);
| ------------------------------ ^^^^^^^^ type mismatch resolving `<Adapter<I> as Iterator>::Item == Option<T>`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error[E0271]: type mismatch resolving `<T as Deref>::Target == T`
--> $DIR/hr-associated-type-projection-1.rs:13:33
|
LL | impl<T: Copy + std::ops::Deref> UnsafeCopy<'_, T> for T {
| - this type parameter ^^^^^^^^^^^^^^^^^ expected type parameter `T`, found associated type
| - ^^^^^^^^^^^^^^^^^ expected type parameter `T`, found associated type
| |
| expected this type parameter
|
= note: expected type parameter `T`
found associated type `<T as Deref>::Target`
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/issues/issue-67945-1.full.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/issue-67945-1.rs:10:20
|
LL | struct Bug<S> {
| - this type parameter
| - expected this type parameter
...
LL | let x: S = MaybeUninit::uninit();
| - ^^^^^^^^^^^^^^^^^^^^^ expected type parameter `S`, found `MaybeUninit<_>`
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/generic-associated-types/issue-68648-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/issue-68648-2.rs:12:17
|
LL | fn bug<'a, T: Fun<F<'a> = T>>(t: T) -> T::F<'a> {
| - this type parameter
| - expected this type parameter
LL | T::identity(())
| ----------- ^^ expected type parameter `T`, found `()`
| |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0271]: type mismatch resolving `<T as Deref>::Target == T`
--> $DIR/issue-68656-unsized-values.rs:13:21
|
LL | impl<T: Copy + std::ops::Deref> UnsafeCopy<T> for T {
| - this type parameter
| - expected this type parameter
LL | type Item<'a> = T;
| ^ expected type parameter `T`, found associated type
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/generic-associated-types/issue-88360.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/issue-88360.rs:15:9
|
LL | trait SuperTrait<T>
| - this type parameter
| - found this type parameter
...
LL | fn copy(&self) -> Self::Gat<'_> where T: Copy {
| ------------- expected `&T` because of return type
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/generic-associated-types/missing-bounds.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ error[E0308]: mismatched types
--> $DIR/missing-bounds.rs:11:11
|
LL | impl<B> Add for A<B> where B: Add {
| - this type parameter
| - expected this type parameter
...
LL | A(self.0 + rhs.0)
| - ^^^^^^^^^^^^^^ expected type parameter `B`, found associated type
Expand Down Expand Up @@ -44,7 +44,7 @@ error[E0308]: mismatched types
--> $DIR/missing-bounds.rs:21:14
|
LL | impl<B: Add> Add for C<B> {
| - this type parameter
| - expected this type parameter
...
LL | Self(self.0 + rhs.0)
| ---- ^^^^^^^^^^^^^^ expected type parameter `B`, found associated type
Expand Down Expand Up @@ -80,7 +80,7 @@ error[E0308]: mismatched types
--> $DIR/missing-bounds.rs:42:14
|
LL | impl<B: Add> Add for E<B> where <B as Add>::Output = B {
| - this type parameter
| - expected this type parameter
...
LL | Self(self.0 + rhs.0)
| ---- ^^^^^^^^^^^^^^ expected type parameter `B`, found associated type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | | &mut Header,
LL | | &mut [EntryMetadata],
LL | | &mut [Entry<C::EncodedKey, C::EncodedValue>]
LL | | ) -> R,
| |__________- this type parameter
| |__________- expected this type parameter
LL | ) {
LL | let () = y;
| ^^ - this expression has type `impl FnOnce(&mut Header, &mut [EntryMetadata], &mut [Entry<C::EncodedKey, C::EncodedValue>]) -> R`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | fn early<'late, T>(_: &'late ()) {}
| | |
| | expected type parameter `T`, found `()`
| | help: change the parameter type to match the trait: `&T`
| this type parameter
| expected this type parameter
|
note: type in trait
--> $DIR/method-signature-matches.rs:52:28
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/impl-trait/in-trait/specialization-broken.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0053]: method `bar` has an incompatible type for trait
--> $DIR/specialization-broken.rs:15:22
|
LL | default impl<U> Foo for U
| - this type parameter
| - found this type parameter
...
LL | fn bar(&self) -> U {
| ^
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/impl-trait/universal-mismatched-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0308]: mismatched types
LL | fn foo(x: impl Debug) -> String {
| ---------- ------ expected `String` because of return type
| |
| this type parameter
| found this type parameter
LL | x
| ^ expected `String`, found type parameter `impl Debug`
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/inference/issue-107090.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ error[E0308]: mismatched types
--> $DIR/issue-107090.rs:22:5
|
LL | fn badboi<'in_, 'out, T>(x: Foo<'in_, 'out, T>, sadness: &'in_ Foo<'short, 'out, T>) -> &'out T {
| - this type parameter ------- expected `&'out T` because of return type
| - expected this type parameter ------- expected `&'out T` because of return type
LL |
LL | sadness.cast()
| ^^^^^^^^^^^^^^ expected `&T`, found `&Foo<'_, '_, T>`
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/issues/issue-20225.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0053]: method `call` has an incompatible type for trait
--> $DIR/issue-20225.rs:6:43
|
LL | impl<'a, T> Fn<(&'a T,)> for Foo {
| - this type parameter
| - found this type parameter
LL | extern "rust-call" fn call(&self, (_,): (T,)) {}
| ^^^^
| |
Expand All @@ -16,7 +16,7 @@ error[E0053]: method `call_mut` has an incompatible type for trait
--> $DIR/issue-20225.rs:11:51
|
LL | impl<'a, T> FnMut<(&'a T,)> for Foo {
| - this type parameter
| - found this type parameter
LL | extern "rust-call" fn call_mut(&mut self, (_,): (T,)) {}
| ^^^^
| |
Expand All @@ -30,7 +30,7 @@ error[E0053]: method `call_once` has an incompatible type for trait
--> $DIR/issue-20225.rs:18:47
|
LL | impl<'a, T> FnOnce<(&'a T,)> for Foo {
| - this type parameter
| - found this type parameter
...
LL | extern "rust-call" fn call_once(self, (_,): (T,)) {}
| ^^^^
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-51154.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/issue-51154.rs:2:30
|
LL | fn foo<F: FnMut()>() {
| - this type parameter
| - expected this type parameter
LL | let _: Box<F> = Box::new(|| ());
| -------- ^^^^^ expected type parameter `F`, found closure
| |
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/issues/issue-69306.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/issue-69306.rs:5:28
|
LL | impl<T> S0<T> {
| - this type parameter
| - expected this type parameter
LL | const C: S0<u8> = Self(0);
| ---- ^ expected type parameter `T`, found integer
| |
Expand All @@ -20,7 +20,7 @@ error[E0308]: mismatched types
--> $DIR/issue-69306.rs:5:23
|
LL | impl<T> S0<T> {
| - this type parameter
| - found this type parameter
LL | const C: S0<u8> = Self(0);
| ^^^^^^^ expected `S0<u8>`, found `S0<T>`
|
Expand All @@ -31,7 +31,7 @@ error[E0308]: mismatched types
--> $DIR/issue-69306.rs:10:14
|
LL | impl<T> S0<T> {
| - this type parameter
| - expected this type parameter
...
LL | Self(0);
| ---- ^ expected type parameter `T`, found integer
Expand All @@ -50,7 +50,7 @@ error[E0308]: mismatched types
--> $DIR/issue-69306.rs:27:14
|
LL | impl<T> Foo<T> for <S0<T> as Fun>::Out {
| - this type parameter
| - expected this type parameter
LL | fn foo() {
LL | Self(0);
| ---- ^ expected type parameter `T`, found integer
Expand All @@ -69,7 +69,7 @@ error[E0308]: mismatched types
--> $DIR/issue-69306.rs:33:32
|
LL | impl<T> S1<T, u8> {
| - this type parameter
| - expected this type parameter
LL | const C: S1<u8, u8> = Self(0, 1);
| ---- ^ expected type parameter `T`, found integer
| |
Expand All @@ -87,7 +87,7 @@ error[E0308]: mismatched types
--> $DIR/issue-69306.rs:33:27
|
LL | impl<T> S1<T, u8> {
| - this type parameter
| - found this type parameter
LL | const C: S1<u8, u8> = Self(0, 1);
| ^^^^^^^^^^ expected `S1<u8, u8>`, found `S1<T, u8>`
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/lang-items/start_lang_item_args.main_ret.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0308]: lang item `start` function has wrong type
LL | fn start<T>(_main: fn() -> u16, _argc: isize, _argv: *const *const u8, _sigpipe: u8) -> isize {
| - ^^^^^^^^^^^ expected type parameter `T`, found `u16`
| |
| this type parameter
| expected this type parameter
|
= note: expected signature `fn(fn() -> T, _, _, _) -> _`
found signature `fn(fn() -> u16, _, _, _) -> _`
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/mismatched_types/issue-35030.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/issue-35030.rs:9:14
|
LL | impl<bool> Parser<bool> for bool {
| ---- this type parameter
| ---- expected this type parameter
LL | fn parse(text: &str) -> Option<bool> {
LL | Some(true)
| ---- ^^^^ expected type parameter `bool`, found `bool`
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/return/return-impl-trait-bad.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0308]: mismatched types
LL | fn bad_echo<T>(_t: T) -> T {
| - - expected `T` because of return type
| |
| this type parameter
| expected this type parameter
LL | "this should not suggest impl Trait"
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `&str`
|
Expand All @@ -17,7 +17,7 @@ error[E0308]: mismatched types
LL | fn bad_echo_2<T: Trait>(_t: T) -> T {
| - - expected `T` because of return type
| |
| this type parameter
| expected this type parameter
LL | "this will not suggest it, because that would probably be wrong"
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `&str`
|
Expand All @@ -30,7 +30,7 @@ error[E0308]: mismatched types
LL | fn other_bounds_bad<T>() -> T
| - - expected `T` because of return type
| |
| this type parameter
| expected this type parameter
...
LL | "don't suggest this, because Option<T> places additional constraints"
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `&str`
Expand All @@ -46,7 +46,7 @@ LL | fn used_in_trait<T>() -> T
| | |
| | expected `T` because of return type
| | help: consider using an impl return type: `impl Send`
| this type parameter
| expected this type parameter
...
LL | "don't suggest this, because the generic param is used in the bound."
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `&str`
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/return/return-impl-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | fn bar<T: Trait + std::marker::Sync>() -> T
| - -
| | |
| | expected `T` because of return type
| this type parameter help: consider using an impl return type: `impl Trait + std::marker::Sync + Send`
| expected this type parameter help: consider using an impl return type: `impl Trait + std::marker::Sync + Send`
...
LL | ()
| ^^ expected type parameter `T`, found `()`
Expand All @@ -21,7 +21,7 @@ LL | fn other_bounds<T>() -> T
| | |
| | expected `T` because of return type
| | help: consider using an impl return type: `impl Trait`
| this type parameter
| expected this type parameter
...
LL | ()
| ^^ expected type parameter `T`, found `()`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0308]: mismatched types
LL | fn wat<T>(t: &T) -> T {
| - - expected `T` because of return type
| |
| this type parameter
| expected this type parameter
LL | t.clone()
| ^^^^^^^^^ expected type parameter `T`, found `&T`
|
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/suggestions/expected-boxed-future-isnt-pinned.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/expected-boxed-future-isnt-pinned.rs:11:5
|
LL | fn foo<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
| - this type parameter ----------------------- expected `Pin<Box<(dyn Future<Output = i32> + Send + 'static)>>` because of return type
| - found this type parameter ----------------------- expected `Pin<Box<(dyn Future<Output = i32> + Send + 'static)>>` because of return type
LL | // We could instead use an `async` block, but this way we have no std spans.
LL | x
| ^ expected `Pin<Box<...>>`, found type parameter `F`
Expand Down Expand Up @@ -30,7 +30,7 @@ error[E0308]: mismatched types
--> $DIR/expected-boxed-future-isnt-pinned.rs:19:14
|
LL | fn baz<F: Future<Output=i32> + Send + 'static>(x: F) -> BoxFuture<'static, i32> {
| - this type parameter
| - found this type parameter
LL | Pin::new(x)
| -------- ^ expected `Box<dyn Future<Output = ...> + Send>`, found type parameter `F`
| |
Expand Down
Loading

0 comments on commit 64ed233

Please sign in to comment.