Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8e83a7e

Browse files
authored
Rollup merge of rust-lang#72807 - xiaotianrandom:fix-assoc-type-diagnostics, r=estebank
Avoid setting wrong obligation cause span of associated type mismatch Removes code that sets wrong obligation cause span of associated type mismatch. See the linked issue for details. Closes rust-lang#72806.
2 parents 6cd9a67 + 1a68c8e commit 8e83a7e

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

src/librustc_trait_selection/traits/wf.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -172,25 +172,18 @@ fn extend_cause_with_original_assoc_item_obligation<'tcx>(
172172
};
173173
match pred.kind() {
174174
ty::PredicateKind::Projection(proj) => {
175-
// The obligation comes not from the current `impl` nor the `trait` being
176-
// implemented, but rather from a "second order" obligation, like in
177-
// `src/test/ui/associated-types/point-at-type-on-obligation-failure.rs`.
178-
let trait_assoc_item = tcx.associated_item(proj.projection_def_id());
179-
if let Some(impl_item_span) =
180-
items.iter().find(|item| item.ident == trait_assoc_item.ident).map(fix_span)
181-
{
182-
cause.span = impl_item_span;
183-
} else {
184-
let kind = &proj.ty().skip_binder().kind;
185-
if let ty::Projection(projection_ty) = kind {
186-
// This happens when an associated type has a projection coming from another
187-
// associated type. See `traits-assoc-type-in-supertrait-bad.rs`.
188-
let trait_assoc_item = tcx.associated_item(projection_ty.item_def_id);
189-
if let Some(impl_item_span) =
190-
items.iter().find(|item| item.ident == trait_assoc_item.ident).map(fix_span)
191-
{
192-
cause.span = impl_item_span;
193-
}
175+
// The obligation comes not from the current `impl` nor the `trait` being implemented,
176+
// but rather from a "second order" obligation, where an associated type has a
177+
// projection coming from another associated type. See
178+
// `src/test/ui/associated-types/point-at-type-on-obligation-failure.rs` and
179+
// `traits-assoc-type-in-supertrait-bad.rs`.
180+
let kind = &proj.ty().skip_binder().kind;
181+
if let ty::Projection(projection_ty) = kind {
182+
let trait_assoc_item = tcx.associated_item(projection_ty.item_def_id);
183+
if let Some(impl_item_span) =
184+
items.iter().find(|item| item.ident == trait_assoc_item.ident).map(fix_span)
185+
{
186+
cause.span = impl_item_span;
194187
}
195188
}
196189
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
trait Bar {
2+
type Ok;
3+
type Sibling: Bar2<Ok=char>;
4+
}
5+
trait Bar2 {
6+
type Ok;
7+
}
8+
9+
struct Foo;
10+
struct Foo2;
11+
12+
impl Bar for Foo { //~ ERROR type mismatch resolving `<Foo2 as Bar2>::Ok == char`
13+
type Ok = ();
14+
type Sibling = Foo2;
15+
}
16+
impl Bar2 for Foo2 {
17+
type Ok = u32;
18+
}
19+
20+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0271]: type mismatch resolving `<Foo2 as Bar2>::Ok == char`
2+
--> $DIR/issue-72806.rs:12:6
3+
|
4+
LL | impl Bar for Foo {
5+
| ^^^ expected `u32`, found `char`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0271`.

0 commit comments

Comments
 (0)