Skip to content

Method resolution for trait object types does not prefer inherent methods #51402

Open
@withoutboats

Description

@withoutboats

Normally, inherent methods are given precedence over all trait methods, so that there is no ambiguity.

However, this behavior does not appear to be implemented for trait object types:

trait Foo {
    fn f(&self) { }
}

impl Foo {
    fn f(&self) { }
}

impl Foo for i32 { }

struct Bar;

impl Bar {
    fn f(&self) { }
}

impl Foo for Bar { }

fn main() {
    let x: &Foo = &42i32;
    x.f();
    let bar: &Bar = &Bar;
    bar.f();
}

https://play.rust-lang.org/?gist=7d4534a5d973249c66244b9affa03199&version=stable&mode=debug

What's worse, UFCS does not allow for specifying that you want the inherent method, because Foo::f(x) in this code will remain ambiguous. In general, UFCS isn't designed to support disambiguating to inherent methods because inherent methods are supposed to always take precedence.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-dyn-traitArea: trait objects, vtable layoutA-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-langRelevant to the language teamT-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