Skip to content

Inappropriate Self type name in "use the fully qualified path for the potential candidates" suggestion #96292

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

Closed
dtolnay opened this issue Apr 21, 2022 · 1 comment · Fixed by #96347
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=ef40cf437f8d092234c378e4c9b22d4e

struct Thing<X>(X);

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

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

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

fn main() {
    let thing = Thing(true);
    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<bool>: Method<_>` found
  --> src/main.rs:7:1
   |
7  | impl<X> Method<i32> for Thing<X> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
11 | impl<X> Method<u32> for Thing<X> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use the fully qualified path for the potential candidates
   |
17 |     <Thing<X> as Method<i32>>::method(thing);
   |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17 |     <Thing<X> as Method<u32>>::method(thing);
   |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Neither of the suggestions compiles, because X does not refer to anything inside the body of main.

error[E0412]: cannot find type `X` in this scope
  --> src/main.rs:17:12
   |
17 |     <Thing<X> as Method<i32>>::method(thing);
   |            ^ not found in this scope
   |
help: consider importing this struct
   |
1  | use nalgebra::coordinates::X;
   |

Instead, a correct suggestion would use any of the following 5 correct possibilities:

  • <Thing<bool> as Method<i32>>::method(thing);
  • <Thing<_> as Method<i32>>::method(thing);
  • <_ as Method<i32>>::method(thing);
  • <Method<i32>>::method(thing);
  • Method::<i32>::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
@estebank
Copy link
Contributor

Accomplishing the second output is likely to be the easiest to get right, by replacing all the named type params that haven't been materialized with _.

@estebank estebank self-assigned this Apr 23, 2022
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 30, 2022
Erase type params when suggesting fully qualified path

When suggesting the use of a fully qualified path for a method call that
is ambiguous because it has multiple candidates, erase type params in
the resulting code, as they would result in an error when applied. We
replace them with `_` in the output to rely on inference. There might be
cases where this still produces slighlty incomplete suggestions, but it
otherwise produces many more errors in relatively common cases.

Fix rust-lang#96292
@bors bors closed this as completed in acee1f4 Apr 30, 2022
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.

2 participants