Skip to content

Wrong compiler error messages while compiling traits with the same method name #143739

Open
@AlexanderSerbul

Description

@AlexanderSerbul

I tried this code:

fn main() {

    println!("Overloading");

    #[derive(Debug)]
    struct Container2 {
        val: String
    };
    
    trait AName2 {
        fn name(&self) -> String;
    }
    
    trait BName2 {
        fn name(&self, v: bool) -> String;
    }

    impl AName2 for Container2 {
        fn name(&self) -> String {
            "aname2".into()
        }
    }

    impl BName2 for Container2 {
        fn name(&self, v: bool) -> String {
            "bname2".into()
        }
    }    
    
    let c2 = Container2 { val: "abc".into() };
    println!("c2 = {:?}", c2.name());
}

I expected to see helpful message from compiler.

Instead, this happened:

54 |         fn name(&self) -> String {
   |         ^^^^^^^^^^^^^^^^^^^^^^^^
note: candidate #2 is defined in an impl of the trait `BName2` for the type `Container2`
  --> src/main.rs:60:9
   |
60 |         fn name(&self, v: bool) -> String {
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: disambiguate the method for candidate #1
   |
66 -     println!("c2 = {:?}", c2.name());
66 +     println!("c2 = {:?}", AName2::name(&c2));
   |
help: disambiguate the method for candidate #2
   |
66 -     println!("c2 = {:?}", c2.name());
66 +     println!("c2 = {:?}", BName2::name(&c2));
   |

Suggestion in 66 is wrong, as BName2::name has TWO args, not one.

Meta

rustc --version --verbose:

1.88.0 stable on rust playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-method-lookupArea: Method lookup (typeck, post `rustc_resolve`, not to be confused with `A-resolve`)D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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