Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jruderman opened this issue Sep 10, 2021 · 0 comments · Fixed by #89255
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jruderman
Copy link
Contributor

jruderman commented Sep 10, 2021

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).

@jruderman jruderman added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 10, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Sep 28, 2021
Fix incorrect disambiguation suggestion for associated items

Fixes rust-lang#88806. I have not added a new test case, because the erroneous behavior is already present in existing test cases.
@bors bors closed this as completed in 48b5d11 Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant