Skip to content

Coherence violation when inherent method is added to type #62320

@ebfull

Description

@ebfull

It seems that because inherent methods are always called before trait methods, when a dependency adds an inherent method to a struct (which should be a backwards-compatible change to its API requiring no major semver bump) your code that was previously calling a trait method on that struct will (silently) switch to calling the inherent method.

As an example, I could make an extension trait for Option defining a method named foo, publish my_cool_crate, and then if an inherent method named foo is added to Option in a new version of core then my crate can suddenly behave differently despite merely upgrading my Rust compiler. Perhaps it could also fail to compile (like if the signature is "compatible" at the callsite but then a typechecking error happens elsewhere).

I'm not sure if this is a coherence violation or not, or whether this has already been observed/discussed elsewhere. (Had no luck when I searched for github issues about it.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemT-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
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions