Skip to content

auto trait candidate selection is unsound #84857

Closed
@lcnr

Description

@lcnr
struct Always<T, U>(T, U);
unsafe impl<T, U> Send for Always<T, U> {}
struct Foo<T, U>(Always<T, U>);

trait False {}
unsafe impl<U: False> Send for Foo<u32, U> {}

trait WithAssoc {
    type Output;
}
impl<T: Send> WithAssoc for T {
    type Output = Self;
}
impl WithAssoc for Foo<u32, ()> {
    type Output = Box<i32>;
}

fn generic<T, U>(v: Foo<T, U>, f: fn(<Foo<T, U> as WithAssoc>::Output) -> i32) {
    f(foo(v));
}

fn foo<T: Send>(x: T) -> <T as WithAssoc>::Output {
    x
}

fn main() {
    generic(Foo(Always(0, ())), |b| *b);
}

Segmentation fault (core dumped)


There are two impls of Send for Foo, the explicit impl<U: False> Send for Foo<u32, U> and the implicit one if all fields are Send. The implicit one always holds in this example.

Candidate selection only acknowledges the existence of the implicit impl if the explicit one is known to not apply even when ignoring where bounds.

For Foo<T, U>: Send candidate selection can't use impl<U: False> Send for Foo<u32, U>, because Foo<T, U> is more generic than Foo<u32, U>. So we use the implicit impl which always applies.

For Foo<u32, ()> the explicit impl does apply, ignoring where bounds. This impl gets rejected however, as (): False does not hold. As we don't consider the implicit impl in this case we therefore accept impl WithAssoc for Foo<u32, ()> during coherence.

We now have two arbitrary overlapping impls which is unsound.

Metadata

Metadata

Assignees

Labels

A-trait-systemArea: Trait systemA-type-systemArea: Type systemC-bugCategory: This is a bug.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions