Skip to content

Trait method takes precedence over inherent method on ambiguous numeric type #99405

Open
@steffahn

Description

@steffahn

This is arguably quite confusing:

trait Trait {
    fn abs(self) -> Self;
}

impl Trait for i64 {
    fn abs(self) -> Self {
        2 * self
    }
}

fn main() {
    let x = 42;
    println!("{}", x.abs());
    println!("{}", x.abs());
}

Ouput:

84
42

So the first x.abs() call resolves to the trait method, which is then used to influence type inference to make x an i64. With x being i64, the second x.abs() call calls the inherent method.

@rustbot label T-compiler, T-lang, A-traits, A-typesystem

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceA-trait-systemArea: Trait systemA-type-systemArea: Type systemC-discussionCategory: Discussion or questions that doesn't represent real issues.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.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