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

Missing & in "use the fully qualified path for the potential candidates" suggestion #96291

Closed
dtolnay opened this issue Apr 21, 2022 · 2 comments · Fixed by #96772
Closed

Missing & in "use the fully qualified path for the potential candidates" suggestion #96291

dtolnay opened this issue Apr 21, 2022 · 2 comments · Fixed by #96772
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Apr 21, 2022

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=d14f1277f1ad02ee89b35d73b15d2ad6

struct Thing;

trait Method<T> {
    fn method(&self) -> T;
}

impl Method<i32> for Thing {
    fn method(&self) -> i32 { 0 }
}

impl Method<u32> for Thing {
    fn method(&self) -> u32 { 0 }
}

fn main() {
    let thing = Thing;
    thing.method();
}

The current output is:

error[E0283]: type annotations needed
  --> src/main.rs:17:11
   |
17 |     thing.method();
   |     ------^^^^^^--
   |     |     |
   |     |     cannot infer type for type parameter `T` declared on the trait `Method`
   |     this method call resolves to `T`
   |
note: multiple `impl`s satisfying `Thing: Method<_>` found
  --> src/main.rs:7:1
   |
7  | impl Method<i32> for Thing {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^
...
11 | impl Method<u32> for Thing {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use the fully qualified path for the potential candidates
   |
17 |     <Thing as Method<i32>>::method(thing);
   |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17 |     <Thing as Method<u32>>::method(thing);
   |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Neither of the suggestions compiles because, unlike ., the suggested syntax does not perform autoref.

The correct output would observe that thing.method() is undergoing autoref and instead suggest:

<Thing as Method<i32>>::method(&thing);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<Thing as Method<u32>>::method(&thing);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@dtolnay dtolnay 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. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Apr 21, 2022
@TaKO8Ki TaKO8Ki self-assigned this Apr 22, 2022
@dtolnay
Copy link
Member Author

dtolnay commented Apr 23, 2022

Note: similarly I would expect <…>::method(&mut thing) for methods that involve autoref with a &mut self receiver.

@estebank
Copy link
Contributor

estebank commented Apr 23, 2022

@TaKO8Ki the second commit in #96347 uses multipart suggestions, which will likely make the "find where to put the &mut " easier. The "looking at the method definition self receiver and figure out the type of the expression to see if & or &mut should be added" would remain the same.

Edit: also, the same code needed to figure out the expr's type and check what's needed to make it match against the candidate can be used to skip the structured suggestion in the case of #96295. If the expression cannot be coerced/autoderefed to any of Self, &Self or &mut Self, then we don't supply a suggestion at all.

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 D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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.

3 participants