Skip to content

Commit 3540312

Browse files
committed
Point at impl assoc type on projection error
1 parent b63cea3 commit 3540312

31 files changed

+199
-0
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,38 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
12621262
);
12631263
self.note_type_err(&mut diag, &obligation.cause, None, values, err);
12641264
self.note_obligation_cause(&mut diag, obligation);
1265+
match predicate.kind().skip_binder() {
1266+
ty::PredicateKind::Projection(proj) => {
1267+
let item = self
1268+
.tcx
1269+
.opt_associated_item(proj.projection_ty.item_def_id)
1270+
.and_then(|trait_assoc_item| {
1271+
self.tcx
1272+
.trait_of_item(proj.projection_ty.item_def_id)
1273+
.map(|id| (trait_assoc_item, id))
1274+
})
1275+
.and_then(|(trait_assoc_item, id)| {
1276+
self.tcx.find_map_relevant_impl(
1277+
id,
1278+
proj.projection_ty.self_ty(),
1279+
|did| {
1280+
self.tcx
1281+
.associated_items(did)
1282+
.in_definition_order()
1283+
.filter(|assoc| assoc.ident == trait_assoc_item.ident)
1284+
.next()
1285+
},
1286+
)
1287+
});
1288+
if let Some(item) = item {
1289+
diag.span_label(
1290+
item.ident.span,
1291+
&format!("type mismatch with `{}` here", proj.ty),
1292+
);
1293+
}
1294+
}
1295+
_ => {}
1296+
}
12651297
diag.emit();
12661298
}
12671299
});

src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0271]: type mismatch resolving `<ModelT as Vehicle>::Color == Blue`
22
--> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:31:10
33
|
4+
LL | impl Vehicle for ModelT { type Color = Black; }
5+
| ----- type mismatch with `Blue` here
6+
...
47
LL | fn blue_car<C:Car<Color=Blue>>(c: C) {
58
| ---------- required by this bound in `blue_car`
69
...
@@ -10,6 +13,9 @@ LL | fn b() { blue_car(ModelT); }
1013
error[E0271]: type mismatch resolving `<ModelU as Vehicle>::Color == Black`
1114
--> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:32:10
1215
|
16+
LL | impl Vehicle for ModelU { type Color = Blue; }
17+
| ----- type mismatch with `Black` here
18+
...
1319
LL | fn black_car<C:Car<Color=Black>>(c: C) {
1420
| ----------- required by this bound in `black_car`
1521
...

src/test/ui/associated-types/associated-types-eq-3.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ LL | fn foo2<I: Foo<A = Bar>>(x: I) {
1616
error[E0271]: type mismatch resolving `<isize as Foo>::A == Bar`
1717
--> $DIR/associated-types-eq-3.rs:38:5
1818
|
19+
LL | type A = usize;
20+
| - type mismatch with `Bar` here
21+
...
1922
LL | fn foo1<I: Foo<A=Bar>>(x: I) {
2023
| ----- required by this bound in `foo1`
2124
...
@@ -25,6 +28,9 @@ LL | foo1(a);
2528
error[E0271]: type mismatch resolving `<isize as Foo>::A == Bar`
2629
--> $DIR/associated-types-eq-3.rs:41:9
2730
|
31+
LL | type A = usize;
32+
| - type mismatch with `Bar` here
33+
...
2834
LL | baz(&a);
2935
| ^^ expected struct `Bar`, found `usize`
3036
|

src/test/ui/associated-types/associated-types-eq-hr.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0271]: type mismatch resolving `for<'x> <UintStruct as TheTrait<&'x isize>>::A == &'x isize`
22
--> $DIR/associated-types-eq-hr.rs:87:5
33
|
4+
LL | type A = &'a usize;
5+
| - type mismatch with `&'x isize` here
6+
...
47
LL | fn foo<T>()
58
| --- required by a bound in this
69
LL | where
@@ -16,6 +19,9 @@ LL | foo::<UintStruct>();
1619
error[E0271]: type mismatch resolving `for<'x> <IntStruct as TheTrait<&'x isize>>::A == &'x usize`
1720
--> $DIR/associated-types-eq-hr.rs:91:5
1821
|
22+
LL | type A = &'a isize;
23+
| - type mismatch with `&'x usize` here
24+
...
1925
LL | fn bar<T>()
2026
| --- required by a bound in this
2127
LL | where

src/test/ui/associated-types/associated-types-issue-20346.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ error[E0271]: type mismatch resolving `<Adapter<I> as Iterator>::Item == Option<
44
LL | fn is_iterator_of<A, I: Iterator<Item=A>>(_: &I) {}
55
| ------ required by this bound in `is_iterator_of`
66
...
7+
LL | type Item = T;
8+
| ---- type mismatch with `Option<T>` here
9+
...
710
LL | fn test_adapter<T, I: Iterator<Item=Option<T>>>(it: I) {
811
| - this type parameter
912
...

src/test/ui/associated-types/associated-types-overridden-binding-2.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ error[E0271]: type mismatch resolving `<std::vec::IntoIter<u32> as Iterator>::It
33
|
44
LL | let _: &dyn I32Iterator<Item = u32> = &vec![42].into_iter();
55
| ^^^^^^^^^^^^^^^^^^^^^ expected `i32`, found `u32`
6+
|
7+
::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
8+
|
9+
LL | type Item = I::Item;
10+
| ---- type mismatch with `i32` here
611
|
712
= note: required for the cast to the object type `dyn Iterator<Item = u32, Item = i32>`
813

src/test/ui/associated-types/issue-44153.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ error[E0271]: type mismatch resolving `<() as Array>::Element == &()`
44
LL | fn visit() {}
55
| ---------- required by `Visit::visit`
66
...
7+
LL | type Element = ();
8+
| ------- type mismatch with `&()` here
9+
...
710
LL | <() as Visit>::visit();
811
| ^^^^^^^^^^^^^^^^^^^^ expected `&()`, found `()`
912
|

src/test/ui/associated-types/issue-72806.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ LL | type Sibling: Bar2<Ok=char>;
66
...
77
LL | type Sibling = Foo2;
88
| ^^^^ expected `char`, found `u32`
9+
...
10+
LL | type Ok = u32;
11+
| -- type mismatch with `char` here
912

1013
error: aborting due to previous error
1114

src/test/ui/associated-types/point-at-type-on-obligation-failure.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ LL | type Sibling: Bar2<Ok=Self::Ok>;
66
...
77
LL | type Sibling = Foo2;
88
| ^^^^ expected `()`, found `u32`
9+
...
10+
LL | type Ok = u32;
11+
| -- type mismatch with `()` here
912

1013
error: aborting due to previous error
1114

src/test/ui/async-await/async-block-control-flow-static-semantics.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ error[E0271]: type mismatch resolving `<impl Future as Future>::Output == ()`
3636
|
3737
LL | let _: &dyn Future<Output = ()> = &block;
3838
| ^^^^^^ expected `()`, found `u8`
39+
|
40+
::: $SRC_DIR/core/src/future/future.rs:LL:COL
41+
|
42+
LL | type Output = F::Output;
43+
| ------ type mismatch with `()` here
3944
|
4045
= note: required for the cast to the object type `dyn Future<Output = ()>`
4146

@@ -52,6 +57,11 @@ error[E0271]: type mismatch resolving `<impl Future as Future>::Output == ()`
5257
|
5358
LL | let _: &dyn Future<Output = ()> = &block;
5459
| ^^^^^^ expected `()`, found `u8`
60+
|
61+
::: $SRC_DIR/core/src/future/future.rs:LL:COL
62+
|
63+
LL | type Output = F::Output;
64+
| ------ type mismatch with `()` here
5565
|
5666
= note: required for the cast to the object type `dyn Future<Output = ()>`
5767

src/test/ui/error-codes/E0271.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ error[E0271]: type mismatch resolving `<i8 as Trait>::AssociatedType == u32`
44
LL | fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
55
| ------------------ required by this bound in `foo`
66
...
7+
LL | impl Trait for i8 { type AssociatedType = &'static str; }
8+
| -------------- type mismatch with `u32` here
9+
...
710
LL | foo(3_i8);
811
| ^^^ expected `u32`, found `&str`
912

src/test/ui/generator/generator-yielding-or-returning-itself.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ LL | where T: Generator<Yield = (), Return = T>
88
...
99
LL | want_cyclic_generator_return(|| {
1010
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cyclic type of infinite size
11+
|
12+
::: $SRC_DIR/core/src/ops/generator.rs:LL:COL
13+
|
14+
LL | type Return = G::Return;
15+
| ------ type mismatch with `[generator@$DIR/generator-yielding-or-returning-itself.rs:15:34: 19:6]` here
1116
|
1217
= note: closures cannot capture themselves or take themselves as argument;
1318
this error may be the result of a recent compiler bug-fix,
@@ -24,6 +29,11 @@ LL | where T: Generator<Yield = T, Return = ()>
2429
...
2530
LL | want_cyclic_generator_yield(|| {
2631
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ cyclic type of infinite size
32+
|
33+
::: $SRC_DIR/core/src/ops/generator.rs:LL:COL
34+
|
35+
LL | type Yield = G::Yield;
36+
| ----- type mismatch with `[generator@$DIR/generator-yielding-or-returning-itself.rs:28:33: 32:6]` here
2737
|
2838
= note: closures cannot capture themselves or take themselves as argument;
2939
this error may be the result of a recent compiler bug-fix,

src/test/ui/generator/type-mismatch-signature-deduction.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ error[E0271]: type mismatch resolving `<[generator@$DIR/type-mismatch-signature-
1717
|
1818
LL | fn foo() -> impl Generator<Return = i32> {
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `i32`, found enum `Result`
20+
|
21+
::: $SRC_DIR/core/src/ops/generator.rs:LL:COL
22+
|
23+
LL | type Return = G::Return;
24+
| ------ type mismatch with `i32` here
2025
|
2126
= note: expected type `i32`
2227
found enum `Result<{integer}, _>`

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ LL | impl<T: Copy + std::ops::Deref> UnsafeCopy<T> for T {
1717
| - this type parameter
1818
LL | type Item<'a> = T;
1919
| ^ expected type parameter `T`, found associated type
20+
|
21+
::: $SRC_DIR/core/src/ops/deref.rs:LL:COL
22+
|
23+
LL | type Target = T;
24+
| ------ type mismatch with `T` here
2025
|
2126
= note: expected type parameter `T`
2227
found associated type `<T as Deref>::Target`

src/test/ui/generic-associated-types/issue-74684-2.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ LL | #![feature(generic_associated_types)]
1010
error[E0271]: type mismatch resolving `<{integer} as Fun>::F<'_> == [u8]`
1111
--> $DIR/issue-74684-2.rs:24:5
1212
|
13+
LL | type F<'a> = i32;
14+
| - type mismatch with `[u8]` here
15+
...
1316
LL | fn bug<'a, T: ?Sized + Fun<F<'a> = [u8]>>(t: Box<T>) -> &'static T::F<'a> {
1417
| ------------ required by this bound in `bug`
1518
...

src/test/ui/hrtb/issue-62203-hrtb-ice.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0271]: type mismatch resolving `for<'r> <L<[closure@$DIR/issue-62203-hrtb-ice.rs:42:17: 42:39]> as T0<'r, (<Unit2 as Ty<'r>>::V,)>>::O == <_ as Ty<'r>>::V`
22
--> $DIR/issue-62203-hrtb-ice.rs:38:19
33
|
4+
LL | type O = T::Output;
5+
| - type mismatch with `<_ as Ty<'r>>::V` here
6+
...
47
LL | let v = Unit2.m(
58
| ^ expected struct `Unit4`, found associated type
69
|
@@ -14,6 +17,11 @@ error[E0271]: type mismatch resolving `<[closure@$DIR/issue-62203-hrtb-ice.rs:42
1417
|
1518
LL | let v = Unit2.m(
1619
| ^ expected struct `Unit4`, found struct `Unit3`
20+
|
21+
::: $SRC_DIR/core/src/ops/function.rs:LL:COL
22+
|
23+
LL | type Output = F::Output;
24+
| ------ type mismatch with `Unit3` here
1725
|
1826
note: required because of the requirements on the impl of `for<'r> T0<'r, (<Unit2 as Ty<'r>>::V,)>` for `L<[closure@$DIR/issue-62203-hrtb-ice.rs:42:17: 42:39]>`
1927
--> $DIR/issue-62203-hrtb-ice.rs:17:16

src/test/ui/impl-trait/bound-normalization-fail.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ LL | #![feature(impl_trait_in_bindings)]
1010
error[E0271]: type mismatch resolving `<Foo<()> as FooLike>::Output == <T as impl_trait::Trait>::Assoc`
1111
--> $DIR/bound-normalization-fail.rs:26:32
1212
|
13+
LL | type Output = T;
14+
| ------ type mismatch with `<T as impl_trait::Trait>::Assoc` here
15+
...
1316
LL | fn foo_fail<T: Trait>() -> impl FooLike<Output=T::Assoc> {
1417
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected associated type, found `()`
1518
|
@@ -29,6 +32,9 @@ LL | fn foo2_fail<'a, T: Trait<'a>>() -> impl FooLike<Output=T::Assoc> {
2932
error[E0271]: type mismatch resolving `<Foo<()> as FooLike>::Output == <T as lifetimes::Trait<'static>>::Assoc`
3033
--> $DIR/bound-normalization-fail.rs:42:41
3134
|
35+
LL | type Output = T;
36+
| ------ type mismatch with `<T as lifetimes::Trait<'static>>::Assoc` here
37+
...
3238
LL | fn foo2_fail<'a, T: Trait<'a>>() -> impl FooLike<Output=T::Assoc> {
3339
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected associated type, found `()`
3440
|

src/test/ui/impl-trait/issues/issue-70877.full_tait.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ LL | type FooRet = impl std::fmt::Debug;
66
...
77
LL | type Foo = impl Iterator<Item = FooItem>;
88
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `Option`, found opaque type
9+
|
10+
::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
11+
|
12+
LL | type Item = I::Item;
13+
| ---- type mismatch with `Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> Option<String> + 'static)>` here
914
|
1015
= note: expected struct `Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> Option<String> + 'static)>`
1116
found struct `Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> impl Debug + 'static)>`

src/test/ui/impl-trait/issues/issue-70877.min_tait.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ LL | type FooRet = impl std::fmt::Debug;
66
...
77
LL | type Foo = impl Iterator<Item = FooItem>;
88
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `Option`, found opaque type
9+
|
10+
::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
11+
|
12+
LL | type Item = I::Item;
13+
| ---- type mismatch with `Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> Option<String> + 'static)>` here
914
|
1015
= note: expected struct `Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> Option<String> + 'static)>`
1116
found struct `Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> impl Debug + 'static)>`

src/test/ui/impl-trait/projection-mismatch-in-impl-where-clause.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0271]: type mismatch resolving `<() as Super>::Assoc == ()`
22
--> $DIR/projection-mismatch-in-impl-where-clause.rs:13:14
33
|
4+
LL | type Assoc = u8;
5+
| ----- type mismatch with `()` here
6+
...
47
LL | fn test() -> impl Test {
58
| ^^^^^^^^^ expected `()`, found `u8`
69
|

src/test/ui/issues/issue-31173.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ error[E0271]: type mismatch resolving `<TakeWhile<&mut std::vec::IntoIter<u8>, [
33
|
44
LL | .cloned()
55
| ^^^^^^ expected `u8`, found reference
6+
|
7+
::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
8+
|
9+
LL | type Item = I::Item;
10+
| ---- type mismatch with `&_` here
611
|
712
= note: expected type `u8`
813
found reference `&_`

src/test/ui/issues/issue-33941.stderr

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ error[E0271]: type mismatch resolving `<std::collections::hash_map::Iter<'_, _,
33
|
44
LL | for _ in HashMap::new().iter().cloned() {}
55
| ^^^^^^ expected tuple, found reference
6+
|
7+
::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
8+
|
9+
LL | type Item = I::Item;
10+
| ---- type mismatch with `&_` here
611
|
712
= note: expected tuple `(&_, &_)`
813
found reference `&_`
@@ -12,6 +17,11 @@ error[E0271]: type mismatch resolving `<std::collections::hash_map::Iter<'_, _,
1217
|
1318
LL | for _ in HashMap::new().iter().cloned() {}
1419
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected reference, found tuple
20+
|
21+
::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
22+
|
23+
LL | type Item = I::Item;
24+
| ---- type mismatch with `&_` here
1525
|
1626
= note: expected reference `&_`
1727
found tuple `(&_, &_)`
@@ -24,6 +34,11 @@ error[E0271]: type mismatch resolving `<std::collections::hash_map::Iter<'_, _,
2434
|
2535
LL | for _ in HashMap::new().iter().cloned() {}
2636
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected reference, found tuple
37+
|
38+
::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
39+
|
40+
LL | type Item = I::Item;
41+
| ---- type mismatch with `&_` here
2742
|
2843
= note: expected reference `&_`
2944
found tuple `(&_, &_)`

src/test/ui/issues/issue-39970.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ error[E0271]: type mismatch resolving `for<'a> <() as Array<'a>>::Element == ()`
44
LL | fn visit() {}
55
| ---------- required by `Visit::visit`
66
...
7+
LL | type Element = &'a ();
8+
| ------- type mismatch with `()` here
9+
...
710
LL | <() as Visit>::visit();
811
| ^^^^^^^^^^^^^^^^^^^^ expected `()`, found `&()`
912
|

src/test/ui/issues/issue-67039-unsound-pin-partialeq.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ error[E0271]: type mismatch resolving `<Rc<Apple> as Deref>::Target == Rc<Apple>
33
|
44
LL | let _ = Pin::new(Apple) == Rc::pin(Apple);
55
| ^^ expected struct `Apple`, found struct `Rc`
6+
|
7+
::: $SRC_DIR/core/src/ops/deref.rs:LL:COL
8+
|
9+
LL | type Target = T;
10+
| ------ type mismatch with `Rc<Apple>` here
611
|
712
= note: expected type `Apple`
813
found struct `Rc<Apple>`

0 commit comments

Comments
 (0)