Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid setting wrong obligation cause span of associated type mismatch #72807

Merged
merged 2 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions src/librustc_trait_selection/traits/wf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,25 +172,18 @@ fn extend_cause_with_original_assoc_item_obligation<'tcx>(
};
match pred.kind() {
ty::PredicateKind::Projection(proj) => {
// The obligation comes not from the current `impl` nor the `trait` being
// implemented, but rather from a "second order" obligation, like in
// `src/test/ui/associated-types/point-at-type-on-obligation-failure.rs`.
let trait_assoc_item = tcx.associated_item(proj.projection_def_id());
if let Some(impl_item_span) =
items.iter().find(|item| item.ident == trait_assoc_item.ident).map(fix_span)
{
cause.span = impl_item_span;
} else {
let kind = &proj.ty().skip_binder().kind;
if let ty::Projection(projection_ty) = kind {
// This happens when an associated type has a projection coming from another
// associated type. See `traits-assoc-type-in-supertrait-bad.rs`.
let trait_assoc_item = tcx.associated_item(projection_ty.item_def_id);
if let Some(impl_item_span) =
items.iter().find(|item| item.ident == trait_assoc_item.ident).map(fix_span)
{
cause.span = impl_item_span;
}
// The obligation comes not from the current `impl` nor the `trait` being implemented,
// but rather from a "second order" obligation, where an associated type has a
// projection coming from another associated type. See
// `src/test/ui/associated-types/point-at-type-on-obligation-failure.rs` and
// `traits-assoc-type-in-supertrait-bad.rs`.
let kind = &proj.ty().skip_binder().kind;
if let ty::Projection(projection_ty) = kind {
let trait_assoc_item = tcx.associated_item(projection_ty.item_def_id);
if let Some(impl_item_span) =
items.iter().find(|item| item.ident == trait_assoc_item.ident).map(fix_span)
{
cause.span = impl_item_span;
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/ui/associated-types/issue-72806.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
trait Bar {
type Ok;
type Sibling: Bar2<Ok=char>;
}
trait Bar2 {
type Ok;
}

struct Foo;
struct Foo2;

impl Bar for Foo { //~ ERROR type mismatch resolving `<Foo2 as Bar2>::Ok == char`
type Ok = ();
type Sibling = Foo2;
}
impl Bar2 for Foo2 {
type Ok = u32;
}

fn main() {}
9 changes: 9 additions & 0 deletions src/test/ui/associated-types/issue-72806.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0271]: type mismatch resolving `<Foo2 as Bar2>::Ok == char`
--> $DIR/issue-72806.rs:12:6
|
LL | impl Bar for Foo {
| ^^^ expected `u32`, found `char`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0271`.