Skip to content

Rustc selects the wrong impl for trait objects when a trait is implemented multiple times with different parameters on the same struct #26339

Closed
@jorisgio

Description

@jorisgio

In the following test case, rustc selects the second implementation instead of the first one :

trait A : PartialEq<Foo> + PartialEq<Bar> { }

struct Foo;
struct Bar;

struct Aimpl;

impl PartialEq<Foo> for Aimpl {
    fn eq(&self, _rhs: &Foo) -> bool {
        panic!("Compare with foo")
    }
}

impl PartialEq<Bar> for Aimpl {
    fn eq(&self, _rhs: &Bar) -> bool {
        panic!("Compare with bar")
    }
}

impl A for Aimpl { }

fn main() {
    let a = &Aimpl as &A;

    *a == Foo;
}

This can lead to uninitialised memory access. Thanks to bluss for further reducing the testcase.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P-highHigh 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