Skip to content

Trait selection caching does not handle subtyping properly #30225

Closed
@arielb1

Description

@arielb1

STR

trait Foo<U,V> : Sized {
    fn foo(self, u: Option<U>, v: Option<V>) {}
}

struct A;
struct B;

impl Foo<A, B> for () {}      // impl A

fn toxic() {
    // cache the resolution <() as Foo<$0,$1>> = impl A
    let u = None;
    let v = None;
    Foo::foo((), u, v);
}

fn bomb() {
    let mut u = None; // type is Option<$0>
    let mut v = None; // type is Option<$1>
    let mut x = None; // type is Option<$2>

    Foo::foo(x.unwrap(),u,v); // register <$2 as Foo<$0, $1>>
    u = v; // mark $0 and $1 in a subtype relationship
    x = Some(()); // set $2 = (), allowing impl selection
                  // to proceed for <() as Foo<$0, $1>> = impl A.
                  // kaboom
                  // NOTE: the obligation's resolution must be delayed for the
                  // ICE to occur to prevent coercions from interfering.
}

fn main() {}

Results

error: internal compiler error: Impl DefId { krate: 0, node: DefIndex(15) => ().Foo<A, B> } was matchable against Obligation(predicate=Binder(TraitPredicate(<_ as Foo<_, _>>)),depth=0) but now is not
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports
note: run with `RUST_BACKTRACE=1` for a backtrace
thread 'rustc' panicked at 'Box<Any>', ../src/libsyntax/diagnostic.rs:253

Comments

@nikomatsakis and me were worried that this might be possible since we had multidispatch, but this is the first working example. I found this while poking with the unification machinery.

Metadata

Metadata

Assignees

Labels

A-trait-systemArea: Trait systemE-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️P-mediumMedium priorityT-compilerRelevant to the compiler 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