Skip to content

Incorrect disambiguation suggestion for associated function, when fully-qualified syntax is required #88806

Closed
@jruderman

Description

@jruderman

Given the following code:

trait Runner {
    fn speed() -> f32;
}
trait Swimmer {
    fn speed() -> f32;
}
struct Jake;
impl Runner for Jake {
    fn speed() -> f32 { 7. }
}
impl Swimmer for Jake {
    fn speed() -> f32 { 3. }
}
fn main() {
    println!("Jake's speed is {} mph", Jake::speed());
}

The current output is:

error[E0034]: multiple applicable items in scope
  --> src/main.rs:15:46
   |
15 |     println!("Jake's speed is {} mph", Jake::speed());
   |                                              ^^^^^ multiple `speed` found
   |
note: candidate #1 is defined in an impl of the trait `Runner` for the type `Jake`
  --> src/main.rs:9:5
   |
9  |     fn speed() -> f32 { 7. }
   |     ^^^^^^^^^^^^^^^^^
note: candidate #2 is defined in an impl of the trait `Swimmer` for the type `Jake`
  --> src/main.rs:12:5
   |
12 |     fn speed() -> f32 { 3. }
   |     ^^^^^^^^^^^^^^^^^
help: disambiguate the associated function for candidate #1
   |
15 |     println!("Jake's speed is {} mph", Runner::speed());
   |                                        ~~~~~~~~
help: disambiguate the associated function for candidate #2
   |
15 |     println!("Jake's speed is {} mph", Swimmer::speed());
   |                                        ~~~~~~~~~

The disambiguation suggestion Runner::speed() does not work; it leads to a "cannot infer type" error (E0283). In this case, the suggestion should use the fully qualified syntax, specifying both the type and the trait, e.g. <Jake as Runner>::speed().

For comparison, here's a case where the suggestion to qualify by only the trait name does work:

trait F {
    fn m(&self);
}

trait G {
    fn m(&self);
}

struct X;

impl F for X { fn m(&self) { println!("I am F"); } }
impl G for X { fn m(&self) { println!("I am G"); } }

fn main() {
    let x = X;

    x.m();
}

In this case, rustc suggests F::m(&x), which is accepted as unambiguous (thanks to the &x passed in as &self, iiuc).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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