Skip to content

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

Closed
@dtolnay

Description

@dtolnay

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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