Rollup merge of #66014 - dkadashev:47319-type-param-def-location, r=estebank
Show type parameter name and definition in type mismatch error messages Fixes #47319 r? estebank
This commit is contained in:
commit
f746d99f68
@ -1234,8 +1234,16 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
||||
}
|
||||
}
|
||||
|
||||
// In some (most?) cases cause.body_id points to actual body, but in some cases
|
||||
// it's a actual definition. According to the comments (e.g. in
|
||||
// librustc_typeck/check/compare_method.rs:compare_predicate_entailment) the latter
|
||||
// is relied upon by some other code. This might (or might not) need cleanup.
|
||||
let body_owner_def_id = self.tcx.hir().opt_local_def_id(cause.body_id)
|
||||
.unwrap_or_else(|| {
|
||||
self.tcx.hir().body_owner_def_id(hir::BodyId { hir_id: cause.body_id })
|
||||
});
|
||||
self.check_and_note_conflicting_crates(diag, terr, span);
|
||||
self.tcx.note_and_explain_type_err(diag, terr, span);
|
||||
self.tcx.note_and_explain_type_err(diag, terr, span, body_owner_def_id);
|
||||
|
||||
// It reads better to have the error origin as the final
|
||||
// thing.
|
||||
|
@ -241,7 +241,7 @@ impl<'tcx> ty::TyS<'tcx> {
|
||||
ty::Infer(ty::FreshFloatTy(_)) => "fresh floating-point type".into(),
|
||||
ty::Projection(_) => "associated type".into(),
|
||||
ty::UnnormalizedProjection(_) => "non-normalized associated type".into(),
|
||||
ty::Param(_) => "type parameter".into(),
|
||||
ty::Param(p) => format!("type parameter `{}`", p).into(),
|
||||
ty::Opaque(..) => "opaque type".into(),
|
||||
ty::Error => "type error".into(),
|
||||
}
|
||||
@ -254,6 +254,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||
db: &mut DiagnosticBuilder<'_>,
|
||||
err: &TypeError<'tcx>,
|
||||
sp: Span,
|
||||
body_owner_def_id: DefId,
|
||||
) {
|
||||
use self::TypeError::*;
|
||||
|
||||
@ -288,7 +289,16 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||
);
|
||||
}
|
||||
},
|
||||
(ty::Param(_), ty::Param(_)) => {
|
||||
(ty::Param(expected), ty::Param(found)) => {
|
||||
let generics = self.generics_of(body_owner_def_id);
|
||||
let e_span = self.def_span(generics.type_param(expected, self).def_id);
|
||||
if !sp.contains(e_span) {
|
||||
db.span_label(e_span, "expected type parameter");
|
||||
}
|
||||
let f_span = self.def_span(generics.type_param(found, self).def_id);
|
||||
if !sp.contains(f_span) {
|
||||
db.span_label(f_span, "found type parameter");
|
||||
}
|
||||
db.note("a type parameter was expected, but a different one was found; \
|
||||
you might be missing a type parameter or trait bound");
|
||||
db.note("for more information, visit \
|
||||
@ -301,7 +311,12 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||
(ty::Param(_), ty::Projection(_)) | (ty::Projection(_), ty::Param(_)) => {
|
||||
db.note("you might be missing a type parameter or trait bound");
|
||||
}
|
||||
(ty::Param(_), _) | (_, ty::Param(_)) => {
|
||||
(ty::Param(p), _) | (_, ty::Param(p)) => {
|
||||
let generics = self.generics_of(body_owner_def_id);
|
||||
let p_span = self.def_span(generics.type_param(p, self).def_id);
|
||||
if !sp.contains(p_span) {
|
||||
db.span_label(p_span, "this type parameter");
|
||||
}
|
||||
db.help("type parameters must be constrained to match other types");
|
||||
if self.sess.teach(&db.get_code().unwrap()) {
|
||||
db.help("given a type parameter `T` and a method `foo`:
|
||||
|
@ -4,8 +4,11 @@ error[E0271]: type mismatch resolving `<Adapter<I> as Iterator>::Item == std::op
|
||||
LL | fn is_iterator_of<A, I: Iterator<Item=A>>(_: &I) {}
|
||||
| -------------- ------ required by this bound in `is_iterator_of`
|
||||
...
|
||||
LL | fn test_adapter<T, I: Iterator<Item=Option<T>>>(it: I) {
|
||||
| - this type parameter
|
||||
...
|
||||
LL | is_iterator_of::<Option<T>, _>(&adapter);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `std::option::Option`, found type parameter
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `std::option::Option`, found type parameter `T`
|
||||
|
|
||||
= note: expected type `std::option::Option<T>`
|
||||
found type `T`
|
||||
|
@ -5,7 +5,10 @@ LL | fn b<C:Clone,D>(&self, x: C) -> C;
|
||||
| - type in trait
|
||||
...
|
||||
LL | fn b<F:Clone,G>(&self, _x: G) -> G { panic!() }
|
||||
| ^ expected type parameter, found a different type parameter
|
||||
| - - ^ expected type parameter `F`, found type parameter `G`
|
||||
| | |
|
||||
| | found type parameter
|
||||
| expected type parameter
|
||||
|
|
||||
= note: expected type `fn(&E, F) -> F`
|
||||
found type `fn(&E, G) -> G`
|
||||
|
@ -5,7 +5,9 @@ LL | fn foo<A: Debug>(&self, a: &A, b: &impl Debug);
|
||||
| -- type in trait
|
||||
...
|
||||
LL | fn foo<B: Debug>(&self, a: &impl Debug, b: &B) { }
|
||||
| ^^^^^^^^^^^ expected type parameter, found a different type parameter
|
||||
| - ^^^^^^^^^^^ expected type parameter `B`, found type parameter `impl Debug`
|
||||
| |
|
||||
| expected type parameter
|
||||
|
|
||||
= note: expected type `fn(&(), &B, &impl Debug)`
|
||||
found type `fn(&(), &impl Debug, &B)`
|
||||
|
@ -2,9 +2,11 @@ error[E0308]: mismatched types
|
||||
--> $DIR/universal-mismatched-type.rs:4:5
|
||||
|
|
||||
LL | fn foo(x: impl Debug) -> String {
|
||||
| ------ expected `std::string::String` because of return type
|
||||
| ---------- ------ expected `std::string::String` because of return type
|
||||
| |
|
||||
| this type parameter
|
||||
LL | x
|
||||
| ^ expected struct `std::string::String`, found type parameter
|
||||
| ^ expected struct `std::string::String`, found type parameter `impl Debug`
|
||||
|
|
||||
= note: expected type `std::string::String`
|
||||
found type `impl Debug`
|
||||
|
@ -1,11 +1,16 @@
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/universal-two-impl-traits.rs:5:9
|
||||
|
|
||||
LL | fn foo(x: impl Debug, y: impl Debug) -> String {
|
||||
| ---------- ---------- found type parameter
|
||||
| |
|
||||
| expected type parameter
|
||||
LL | let mut a = x;
|
||||
LL | a = y;
|
||||
| ^ expected type parameter, found a different type parameter
|
||||
| ^ expected type parameter `impl Debug`, found a different type parameter `impl Debug`
|
||||
|
|
||||
= note: expected type `impl Debug` (type parameter)
|
||||
found type `impl Debug` (type parameter)
|
||||
= note: expected type `impl Debug` (type parameter `impl Debug`)
|
||||
found type `impl Debug` (type parameter `impl Debug`)
|
||||
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
|
||||
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
|
||||
|
||||
|
@ -2,10 +2,10 @@ error[E0308]: mismatched types
|
||||
--> $DIR/issue-13853.rs:14:9
|
||||
|
|
||||
LL | fn nodes<'a, I: Iterator<Item=&'a N>>(&self) -> I
|
||||
| - expected `I` because of return type
|
||||
| - this type parameter - expected `I` because of return type
|
||||
...
|
||||
LL | self.iter()
|
||||
| ^^^^^^^^^^^ expected type parameter, found struct `std::slice::Iter`
|
||||
| ^^^^^^^^^^^ expected type parameter `I`, found struct `std::slice::Iter`
|
||||
|
|
||||
= note: expected type `I`
|
||||
found type `std::slice::Iter<'_, N>`
|
||||
|
@ -1,8 +1,10 @@
|
||||
error[E0053]: method `call` has an incompatible type for trait
|
||||
--> $DIR/issue-20225.rs:6:3
|
||||
|
|
||||
LL | impl<'a, T> Fn<(&'a T,)> for Foo {
|
||||
| - this type parameter
|
||||
LL | extern "rust-call" fn call(&self, (_,): (T,)) {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected reference, found type parameter
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected reference, found type parameter `T`
|
||||
|
|
||||
= note: expected type `extern "rust-call" fn(&Foo, (&'a T,))`
|
||||
found type `extern "rust-call" fn(&Foo, (T,))`
|
||||
@ -12,8 +14,10 @@ LL | extern "rust-call" fn call(&self, (_,): (T,)) {}
|
||||
error[E0053]: method `call_mut` has an incompatible type for trait
|
||||
--> $DIR/issue-20225.rs:12:3
|
||||
|
|
||||
LL | impl<'a, T> FnMut<(&'a T,)> for Foo {
|
||||
| - this type parameter
|
||||
LL | extern "rust-call" fn call_mut(&mut self, (_,): (T,)) {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected reference, found type parameter
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected reference, found type parameter `T`
|
||||
|
|
||||
= note: expected type `extern "rust-call" fn(&mut Foo, (&'a T,))`
|
||||
found type `extern "rust-call" fn(&mut Foo, (T,))`
|
||||
@ -23,8 +27,11 @@ LL | extern "rust-call" fn call_mut(&mut self, (_,): (T,)) {}
|
||||
error[E0053]: method `call_once` has an incompatible type for trait
|
||||
--> $DIR/issue-20225.rs:20:3
|
||||
|
|
||||
LL | impl<'a, T> FnOnce<(&'a T,)> for Foo {
|
||||
| - this type parameter
|
||||
...
|
||||
LL | extern "rust-call" fn call_once(self, (_,): (T,)) {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected reference, found type parameter
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected reference, found type parameter `T`
|
||||
|
|
||||
= note: expected type `extern "rust-call" fn(Foo, (&'a T,))`
|
||||
found type `extern "rust-call" fn(Foo, (T,))`
|
||||
|
@ -5,7 +5,7 @@ LL | trait Trait: Sized {
|
||||
| ------------------ required by `Trait`
|
||||
...
|
||||
LL | fn test<T: Trait<B=i32>>(b: i32) -> T where T::A: MultiDispatch<i32> { T::new(b) }
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter, found associated type
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found associated type
|
||||
|
|
||||
= note: expected type `T`
|
||||
found type `<<T as Trait>::A as MultiDispatch<i32>>::O`
|
||||
|
@ -4,7 +4,7 @@ fn foo<T, U>(x: T, y: U) {
|
||||
//~^ ERROR mismatched types
|
||||
//~| expected type `T`
|
||||
//~| found type `U`
|
||||
//~| expected type parameter, found a different type parameter
|
||||
//~| expected type parameter `T`, found type parameter `U`
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
@ -1,8 +1,13 @@
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/issue-2951.rs:3:10
|
||||
|
|
||||
LL | fn foo<T, U>(x: T, y: U) {
|
||||
| - - found type parameter
|
||||
| |
|
||||
| expected type parameter
|
||||
LL | let mut xx = x;
|
||||
LL | xx = y;
|
||||
| ^ expected type parameter, found a different type parameter
|
||||
| ^ expected type parameter `T`, found type parameter `U`
|
||||
|
|
||||
= note: expected type `T`
|
||||
found type `U`
|
||||
|
@ -1,10 +1,13 @@
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/issue-35030.rs:9:14
|
||||
|
|
||||
LL | impl<bool> Parser<bool> for bool {
|
||||
| ---- this type parameter
|
||||
LL | fn parse(text: &str) -> Option<bool> {
|
||||
LL | Some(true)
|
||||
| ^^^^ expected type parameter, found bool
|
||||
| ^^^^ expected type parameter `bool`, found bool
|
||||
|
|
||||
= note: expected type `bool` (type parameter)
|
||||
= note: expected type `bool` (type parameter `bool`)
|
||||
found type `bool` (bool)
|
||||
= help: type parameters must be constrained to match other types
|
||||
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
|
||||
|
@ -7,8 +7,13 @@ LL | Self { inner: 1.5f32 };
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/struct-path-self-type-mismatch.rs:15:20
|
||||
|
|
||||
LL | impl<T> Foo<T> {
|
||||
| - expected type parameter
|
||||
LL | fn new<U>(u: U) -> Foo<U> {
|
||||
| - found type parameter
|
||||
...
|
||||
LL | inner: u
|
||||
| ^ expected type parameter, found a different type parameter
|
||||
| ^ expected type parameter `T`, found type parameter `U`
|
||||
|
|
||||
= note: expected type `T`
|
||||
found type `U`
|
||||
@ -18,14 +23,18 @@ LL | inner: u
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/struct-path-self-type-mismatch.rs:13:9
|
||||
|
|
||||
LL | impl<T> Foo<T> {
|
||||
| - found type parameter
|
||||
LL | fn new<U>(u: U) -> Foo<U> {
|
||||
| ------ expected `Foo<U>` because of return type
|
||||
| - ------ expected `Foo<U>` because of return type
|
||||
| |
|
||||
| expected type parameter
|
||||
LL | / Self {
|
||||
LL | |
|
||||
LL | | inner: u
|
||||
LL | |
|
||||
LL | | }
|
||||
| |_________^ expected type parameter, found a different type parameter
|
||||
| |_________^ expected type parameter `U`, found type parameter `T`
|
||||
|
|
||||
= note: expected type `Foo<U>`
|
||||
found type `Foo<T>`
|
||||
|
@ -1,4 +1,4 @@
|
||||
error[E0071]: expected struct, variant or union type, found type parameter
|
||||
error[E0071]: expected struct, variant or union type, found type parameter `Self`
|
||||
--> $DIR/struct-path-self.rs:5:17
|
||||
|
|
||||
LL | let s = Self {};
|
||||
@ -10,13 +10,13 @@ error[E0109]: type arguments are not allowed for this type
|
||||
LL | let z = Self::<u8> {};
|
||||
| ^^ type argument not allowed
|
||||
|
||||
error[E0071]: expected struct, variant or union type, found type parameter
|
||||
error[E0071]: expected struct, variant or union type, found type parameter `Self`
|
||||
--> $DIR/struct-path-self.rs:7:17
|
||||
|
|
||||
LL | let z = Self::<u8> {};
|
||||
| ^^^^^^^^^^ not a struct
|
||||
|
||||
error[E0071]: expected struct, variant or union type, found type parameter
|
||||
error[E0071]: expected struct, variant or union type, found type parameter `Self`
|
||||
--> $DIR/struct-path-self.rs:11:13
|
||||
|
|
||||
LL | Self { .. } => {}
|
||||
|
@ -1,8 +1,11 @@
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/enum-variant-generic-args.rs:13:25
|
||||
|
|
||||
LL | impl<T> Enum<T> {
|
||||
| - this type parameter
|
||||
LL | fn ts_variant() {
|
||||
LL | Self::TSVariant(());
|
||||
| ^^ expected type parameter, found ()
|
||||
| ^^ expected type parameter `T`, found ()
|
||||
|
|
||||
= note: expected type `T`
|
||||
found type `()`
|
||||
@ -24,8 +27,11 @@ LL | Self::<()>::TSVariant(());
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/enum-variant-generic-args.rs:17:31
|
||||
|
|
||||
LL | impl<T> Enum<T> {
|
||||
| - this type parameter
|
||||
...
|
||||
LL | Self::<()>::TSVariant(());
|
||||
| ^^ expected type parameter, found ()
|
||||
| ^^ expected type parameter `T`, found ()
|
||||
|
|
||||
= note: expected type `T`
|
||||
found type `()`
|
||||
@ -47,8 +53,11 @@ LL | Self::<()>::TSVariant::<()>(());
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/enum-variant-generic-args.rs:26:29
|
||||
|
|
||||
LL | impl<T> Enum<T> {
|
||||
| - this type parameter
|
||||
...
|
||||
LL | Self::SVariant { v: () };
|
||||
| ^^ expected type parameter, found ()
|
||||
| ^^ expected type parameter `T`, found ()
|
||||
|
|
||||
= note: expected type `T`
|
||||
found type `()`
|
||||
@ -64,8 +73,11 @@ LL | Self::SVariant::<()> { v: () };
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/enum-variant-generic-args.rs:28:35
|
||||
|
|
||||
LL | impl<T> Enum<T> {
|
||||
| - this type parameter
|
||||
...
|
||||
LL | Self::SVariant::<()> { v: () };
|
||||
| ^^ expected type parameter, found ()
|
||||
| ^^ expected type parameter `T`, found ()
|
||||
|
|
||||
= note: expected type `T`
|
||||
found type `()`
|
||||
@ -81,8 +93,11 @@ LL | Self::<()>::SVariant { v: () };
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/enum-variant-generic-args.rs:31:35
|
||||
|
|
||||
LL | impl<T> Enum<T> {
|
||||
| - this type parameter
|
||||
...
|
||||
LL | Self::<()>::SVariant { v: () };
|
||||
| ^^ expected type parameter, found ()
|
||||
| ^^ expected type parameter `T`, found ()
|
||||
|
|
||||
= note: expected type `T`
|
||||
found type `()`
|
||||
@ -104,8 +119,11 @@ LL | Self::<()>::SVariant::<()> { v: () };
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/enum-variant-generic-args.rs:34:41
|
||||
|
|
||||
LL | impl<T> Enum<T> {
|
||||
| - this type parameter
|
||||
...
|
||||
LL | Self::<()>::SVariant::<()> { v: () };
|
||||
| ^^ expected type parameter, found ()
|
||||
| ^^ expected type parameter `T`, found ()
|
||||
|
|
||||
= note: expected type `T`
|
||||
found type `()`
|
||||
|
@ -6,7 +6,7 @@ fn foo<Foo, Bar>(x: Foo) -> Bar {
|
||||
//~^ ERROR mismatched types
|
||||
//~| expected type `Bar`
|
||||
//~| found type `Foo`
|
||||
//~| expected type parameter, found a different type parameter
|
||||
//~| expected type parameter `Bar`, found type parameter `Foo`
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
@ -2,9 +2,12 @@ error[E0308]: mismatched types
|
||||
--> $DIR/type-parameter-names.rs:5:5
|
||||
|
|
||||
LL | fn foo<Foo, Bar>(x: Foo) -> Bar {
|
||||
| --- expected `Bar` because of return type
|
||||
| --- --- --- expected `Bar` because of return type
|
||||
| | |
|
||||
| | expected type parameter
|
||||
| found type parameter
|
||||
LL | x
|
||||
| ^ expected type parameter, found a different type parameter
|
||||
| ^ expected type parameter `Bar`, found type parameter `Foo`
|
||||
|
|
||||
= note: expected type `Bar`
|
||||
found type `Foo`
|
||||
|
@ -5,7 +5,7 @@ trait BrokenAdd: Copy + Add<Output=Self> {
|
||||
*self + rhs //~ ERROR mismatched types
|
||||
//~| expected type `Self`
|
||||
//~| found type `T`
|
||||
//~| expected type parameter, found a different type parameter
|
||||
//~| expected type parameter `Self`, found type parameter `T`
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,16 @@
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/type-params-in-different-spaces-1.rs:5:17
|
||||
|
|
||||
LL | *self + rhs
|
||||
| ^^^ expected type parameter, found a different type parameter
|
||||
LL | / trait BrokenAdd: Copy + Add<Output=Self> {
|
||||
LL | | fn broken_add<T>(&self, rhs: T) -> Self {
|
||||
| | - found type parameter
|
||||
LL | | *self + rhs
|
||||
| | ^^^ expected type parameter `Self`, found type parameter `T`
|
||||
LL | |
|
||||
... |
|
||||
LL | | }
|
||||
LL | | }
|
||||
| |_- expected type parameter
|
||||
|
|
||||
= note: expected type `Self`
|
||||
found type `T`
|
||||
|
@ -1,10 +1,16 @@
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/type-params-in-different-spaces-3.rs:3:9
|
||||
|
|
||||
LL | fn test<X>(u: X) -> Self {
|
||||
| ---- expected `Self` because of return type
|
||||
LL | u
|
||||
| ^ expected type parameter, found a different type parameter
|
||||
LL | / trait Tr : Sized {
|
||||
LL | | fn test<X>(u: X) -> Self {
|
||||
| | - ---- expected `Self` because of return type
|
||||
| | |
|
||||
| | found type parameter
|
||||
LL | | u
|
||||
| | ^ expected type parameter `Self`, found type parameter `X`
|
||||
LL | | }
|
||||
LL | | }
|
||||
| |_- expected type parameter
|
||||
|
|
||||
= note: expected type `Self`
|
||||
found type `X`
|
||||
|
Loading…
x
Reference in New Issue
Block a user