Skip to content

Commit 280d4e1

Browse files
committed
Point at impl assoc type on projection error
1 parent 1d15d43 commit 280d4e1

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

0 commit comments

Comments
 (0)