rust-lang / rust-lang/rust

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

Open
#143,739 1 comment 0 reactions 1 assignee View on GitHub

@xizheyin is already working on this.

Since Jul 16, 2025.

A-diagnostics A-method-lookup D-invalid-suggestion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.