Skip to content

Commit b029f1b

Browse files
committed
In diag for duplicated assoc types in dyn, mention the relevant trait.
1 parent b5838a9 commit b029f1b

12 files changed

Lines changed: 59 additions & 44 deletions

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_trait.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_hir::def_id::DefId;
1212
use rustc_hir::{self as hir, HirId, LangItem};
1313
use rustc_lint_defs::builtin::{BARE_TRAIT_OBJECTS, UNUSED_ASSOCIATED_TYPE_BOUNDS};
1414
use rustc_middle::ty::elaborate::ClauseWithSupertraitSpan;
15+
use rustc_middle::ty::print::{PrintPolyTraitRefExt as _, PrintTraitRefExt as _};
1516
use rustc_middle::ty::{
1617
self, AliasTermKind, BottomUpFolder, ExistentialPredicateStableCmpExt as _, Ty, TyCtxt,
1718
TypeFoldable, TypeVisitableExt, Upcast,
@@ -185,15 +186,27 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
185186
{
186187
let kind = tcx.def_descr(item_def_id);
187188
let name = tcx.item_name(item_def_id);
189+
188190
self.dcx()
189191
.struct_span_err(span, format!("conflicting {kind} bindings for `{name}`"))
190192
.with_span_label(
191193
old_proj_span,
192-
format!("`{name}` is specified to be `{}` here", old_proj.term()),
194+
format!(
195+
"`{name}` (in `{}`) is specified to be `{}` here",
196+
old_proj
197+
.map_bound(|proj| proj.projection_term.trait_ref(tcx))
198+
.print_only_trait_path(),
199+
old_proj.term()
200+
),
193201
)
194202
.with_span_label(
195203
proj_span,
196-
format!("`{name}` is specified to be `{}` here", proj.term()),
204+
format!(
205+
"`{name}` (in `{}`) is specified to be `{}` here",
206+
proj.map_bound(|proj| proj.projection_term.trait_ref(tcx))
207+
.print_only_trait_path(),
208+
proj.term()
209+
),
197210
)
198211
.emit();
199212
}
@@ -267,8 +280,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
267280
let term = pred.term;
268281
// This clause specifies that the `kind` is equal to `term`.
269282
// We record this, and check for duplicates.
270-
if let Some(old_term) =
271-
seen_projection_bounds_ignoring_generics.insert(kind, term)
283+
if let Some((old_term, old_pred)) =
284+
seen_projection_bounds_ignoring_generics.insert(kind, (term, pred))
272285
&& old_term != term
273286
{
274287
let name = tcx.item_name(kind.def_id());
@@ -283,8 +296,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
283296
)
284297
// FIXME: Improve diagnostics by pointing to
285298
// where the bound is specified.
286-
.with_note(format!("`{name}` is specified to be `{old_term}`"))
287-
.with_note(format!("`{name}` is also specified to be `{term}`"))
299+
.with_note(format!("`{name}` (in `{}`) is specified to be `{old_term}`",
300+
old_pred.projection_term.trait_ref(tcx).print_only_trait_path()
301+
))
302+
.with_note(format!("`{name}` (in `{}`) is also specified to be `{term}`", pred.projection_term.trait_ref(tcx).print_only_trait_path()))
288303
.emit();
289304
}
290305

tests/ui/associated-type-bounds/conflicting-bounds-different-generics-complex.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error: conflicting associated type bindings for `Assoc`
44
LL | require_trait::<D, dyn Sub<D>>();
55
| ^^^^^^^^^^
66
|
7-
= note: `Assoc` is specified to be `i64`
8-
= note: `Assoc` is also specified to be `i32`
7+
= note: `Assoc` (in `Super<<D as Dummy>::DummyAssoc2>`) is specified to be `i64`
8+
= note: `Assoc` (in `Super<<D as Dummy>::DummyAssoc1>`) is also specified to be `i32`
99

1010
error: aborting due to 1 previous error
1111

tests/ui/associated-type-bounds/conflicting-bounds-different-generics-simple.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error: conflicting associated type bindings for `Assoc`
44
LL | fn foo(_: &dyn Sub) {}
55
| ^^^^^^^
66
|
7-
= note: `Assoc` is specified to be `u64`
8-
= note: `Assoc` is also specified to be `u32`
7+
= note: `Assoc` (in `Super<i64>`) is specified to be `u64`
8+
= note: `Assoc` (in `Super<i32>`) is also specified to be `u32`
99

1010
error: aborting due to 1 previous error
1111

tests/ui/associated-type-bounds/conflicting-bounds-different-generics-unsound.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error: conflicting associated type bindings for `Assoc`
44
LL | require_trait::<'a, A1, A2, dyn Sub<'a, A1, A2>, C>(payload)
55
| ^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: `Assoc` is specified to be `&'static Box<i32>`
8-
= note: `Assoc` is also specified to be `&'a Box<i32>`
7+
= note: `Assoc` (in `Super<A2>`) is specified to be `&'static Box<i32>`
8+
= note: `Assoc` (in `Super<A1>`) is also specified to be `&'a Box<i32>`
99

1010
error: aborting due to 1 previous error
1111

tests/ui/associated-type-bounds/duplicate-bound-err.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,17 @@ error: conflicting associated type bindings for `Item`
126126
LL | type MustFail = dyn Iterator<Item = i32, Item = u32>;
127127
| ^^^^^^^^^^^^^----------^^----------^
128128
| | |
129-
| | `Item` is specified to be `u32` here
130-
| `Item` is specified to be `i32` here
129+
| | `Item` (in `Iterator`) is specified to be `u32` here
130+
| `Item` (in `Iterator`) is specified to be `i32` here
131131

132132
error: conflicting associated constant bindings for `ASSOC`
133133
--> $DIR/duplicate-bound-err.rs:85:18
134134
|
135135
LL | type MustFail2 = dyn Trait2<ASSOC = 3u32, ASSOC = 4u32>;
136136
| ^^^^^^^^^^^------------^^------------^
137137
| | |
138-
| | `ASSOC` is specified to be `4` here
139-
| `ASSOC` is specified to be `3` here
138+
| | `ASSOC` (in `Trait2`) is specified to be `4` here
139+
| `ASSOC` (in `Trait2`) is specified to be `3` here
140140

141141
error[E0271]: expected `Empty<u32>` to be an iterator that yields `i32`, but it yields `u32`
142142
--> $DIR/duplicate-bound-err.rs:105:16

tests/ui/associated-types/associated-types-overridden-binding-2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ error: conflicting associated type bindings for `Item`
22
--> $DIR/associated-types-overridden-binding-2.rs:6:13
33
|
44
LL | trait I32Iterator = Iterator<Item = i32>;
5-
| ---------- `Item` is specified to be `i32` here
5+
| ---------- `Item` (in `Iterator`) is specified to be `i32` here
66
...
77
LL | let _: &dyn I32Iterator<Item = u32> = &vec![42].into_iter();
88
| ^^^^^^^^^^^^^^^^----------^
99
| |
10-
| `Item` is specified to be `u32` here
10+
| `Item` (in `Iterator`) is specified to be `u32` here
1111

1212
error: aborting due to 1 previous error
1313

tests/ui/associated-types/associated-types-overridden-binding.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ error: conflicting associated type bindings for `Item`
2626
--> $DIR/associated-types-overridden-binding.rs:10:13
2727
|
2828
LL | trait I32Iterator = Iterator<Item = i32>;
29-
| ---------- `Item` is specified to be `i32` here
29+
| ---------- `Item` (in `Iterator`) is specified to be `i32` here
3030
...
3131
LL | let _: &dyn I32Iterator<Item = u32>;
3232
| ^^^^^^^^^^^^^^^^----------^
3333
| |
34-
| `Item` is specified to be `u32` here
34+
| `Item` (in `Iterator`) is specified to be `u32` here
3535

3636
error: aborting due to 3 previous errors
3737

tests/ui/dyn-compatibility/multiple-supers-should-work.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ error: conflicting associated type bindings for `Assoc`
44
LL | let x: &dyn Trait<(), _> = &();
55
| ^^^^^^^^^^^^^^^^
66
|
7-
= note: `Assoc` is specified to be `_`
8-
= note: `Assoc` is also specified to be `()`
7+
= note: `Assoc` (in `Sup<_>`) is specified to be `_`
8+
= note: `Assoc` (in `Sup<()>`) is also specified to be `()`
99

1010
error: conflicting associated type bindings for `Assoc`
1111
--> $DIR/multiple-supers-should-work.rs:21:13
1212
|
1313
LL | let y: &dyn Trait<_, ()> = x;
1414
| ^^^^^^^^^^^^^^^^
1515
|
16-
= note: `Assoc` is specified to be `()`
17-
= note: `Assoc` is also specified to be `_`
16+
= note: `Assoc` (in `Sup<()>`) is specified to be `()`
17+
= note: `Assoc` (in `Sup<_>`) is also specified to be `_`
1818

1919
error: aborting due to 2 previous errors
2020

tests/ui/higher-ranked/trait-bounds/normalize-under-binder/issue-81809.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ error: conflicting associated type bindings for `Output`
44
LL | fn foo(st: &impl StoreIndex) -> &dyn StoreIndex {
55
| ^^^^^^^^^^^^^^
66
|
7-
= note: `Output` is specified to be `u16`
8-
= note: `Output` is also specified to be `u8`
7+
= note: `Output` (in `Index<<u16 as Indexable>::Idx>`) is specified to be `u16`
8+
= note: `Output` (in `Index<<u8 as Indexable>::Idx>`) is also specified to be `u8`
99

1010
error: conflicting associated type bindings for `Output`
1111
--> $DIR/issue-81809.rs:19:12
1212
|
1313
LL | st as &dyn StoreIndex
1414
| ^^^^^^^^^^^^^^
1515
|
16-
= note: `Output` is specified to be `u16`
17-
= note: `Output` is also specified to be `u8`
16+
= note: `Output` (in `Index<<u16 as Indexable>::Idx>`) is specified to be `u16`
17+
= note: `Output` (in `Index<<u8 as Indexable>::Idx>`) is also specified to be `u8`
1818

1919
error: aborting due to 2 previous errors
2020

tests/ui/traits/next-solver/supertrait-alias-4.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ error: conflicting associated type bindings for `Assoc`
44
LL | let x: &dyn Foo<_, _, Other = ()> = todo!();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: `Assoc` is specified to be `_`
8-
= note: `Assoc` is also specified to be `_`
7+
= note: `Assoc` (in `Sup<_>`) is specified to be `_`
8+
= note: `Assoc` (in `Sup<_>`) is also specified to be `_`
99

1010
error: conflicting associated type bindings for `Assoc`
1111
--> $DIR/supertrait-alias-4.rs:25:13
1212
|
1313
LL | let y: &dyn Foo<i32, u32, Other = ()> = x;
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1515
|
16-
= note: `Assoc` is specified to be `u32`
17-
= note: `Assoc` is also specified to be `i32`
16+
= note: `Assoc` (in `Sup<u32>`) is specified to be `u32`
17+
= note: `Assoc` (in `Sup<i32>`) is also specified to be `i32`
1818

1919
error: aborting due to 2 previous errors
2020

0 commit comments

Comments
 (0)