Skip to content

Commit f8eab33

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

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
@@ -10,6 +10,7 @@ use rustc_hir::def_id::DefId;
1010
use rustc_hir::{self as hir, HirId, LangItem};
1111
use rustc_lint_defs::builtin::{BARE_TRAIT_OBJECTS, UNUSED_ASSOCIATED_TYPE_BOUNDS};
1212
use rustc_middle::ty::elaborate::ClauseWithSupertraitSpan;
13+
use rustc_middle::ty::print::{PrintPolyTraitRefExt as _, PrintTraitRefExt as _};
1314
use rustc_middle::ty::{
1415
self, AliasTermKind, BottomUpFolder, ExistentialPredicateStableCmpExt as _, Ty, TyCtxt,
1516
TypeFoldable, TypeVisitableExt, Upcast,
@@ -183,15 +184,27 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
183184
{
184185
let kind = tcx.def_descr(item_def_id);
185186
let name = tcx.item_name(item_def_id);
187+
186188
self.dcx()
187189
.struct_span_err(span, format!("conflicting {kind} bindings for `{name}`"))
188190
.with_span_label(
189191
old_proj_span,
190-
format!("`{name}` is specified to be `{}` here", old_proj.term()),
192+
format!(
193+
"`{name}` (in `{}`) is specified to be `{}` here",
194+
old_proj
195+
.map_bound(|proj| proj.projection_term.trait_ref(tcx))
196+
.print_only_trait_path(),
197+
old_proj.term()
198+
),
191199
)
192200
.with_span_label(
193201
proj_span,
194-
format!("`{name}` is specified to be `{}` here", proj.term()),
202+
format!(
203+
"`{name}` (in `{}`) is specified to be `{}` here",
204+
proj.map_bound(|proj| proj.projection_term.trait_ref(tcx))
205+
.print_only_trait_path(),
206+
proj.term()
207+
),
195208
)
196209
.emit();
197210
}
@@ -266,8 +279,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
266279
let term = pred.term;
267280
// This clause specifies that the `kind` is equal to `term`.
268281
// We record this, and check for duplicates.
269-
if let Some(old_term) =
270-
seen_projection_bounds_ignoring_generics.insert(kind, term)
282+
if let Some((old_term, old_pred)) =
283+
seen_projection_bounds_ignoring_generics.insert(kind, (term, pred))
271284
&& old_term != term
272285
{
273286
let name = tcx.item_name(def_id);
@@ -282,8 +295,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
282295
)
283296
// FIXME: Improve diagnostics by pointing to
284297
// where the bound is specified.
285-
.with_note(format!("`{name}` is specified to be `{old_term}`"))
286-
.with_note(format!("`{name}` is also specified to be `{term}`"))
298+
.with_note(format!("`{name}` (in `{}`) is specified to be `{old_term}`",
299+
old_pred.projection_term.trait_ref(tcx).print_only_trait_path()
300+
))
301+
.with_note(format!("`{name}` (in `{}`) is also specified to be `{term}`", pred.projection_term.trait_ref(tcx).print_only_trait_path()))
287302
.emit();
288303
}
289304

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)