Skip to content

Wrong trait in "use the fully qualified path for the potential candidates" suggestion #96295

Open
@dtolnay

Description

@dtolnay

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

struct A<T>(T);
struct B;

trait I<T> {}
impl I<i32> for B {}
impl I<u32> for B {}

trait V<U> {
    fn method(self) -> U;
}

impl<T, U> V<U> for A<T>
where
    T: I<U>,
{
    fn method(self) -> U { unimplemented!() }
}

fn main() {
    let a = A(B);
    a.method();
}

The current output is:

error[E0283]: type annotations needed
  --> src/main.rs:21:7
   |
21 |     a.method();
   |     --^^^^^^--
   |     | |
   |     | cannot infer type for type parameter `U`
   |     this method call resolves to `U`
   |
note: multiple `impl`s satisfying `B: I<_>` found
  --> src/main.rs:5:1
   |
5  | impl I<i32> for B {}
   | ^^^^^^^^^^^^^^^^^
6  | impl I<u32> for B {}
   | ^^^^^^^^^^^^^^^^^
note: required because of the requirements on the impl of `V<_>` for `A<B>`
  --> src/main.rs:12:12
   |
12 | impl<T, U> V<U> for A<T>
   |            ^^^^     ^^^^
help: use the fully qualified path for the potential candidates
   |
21 |     <B as I<i32>>::method(a);
   |     ~~~~~~~~~~~~~~~~~~~~~~~~
21 |     <B as I<u32>>::method(a);
   |     ~~~~~~~~~~~~~~~~~~~~~~~~

The two suggestions <B as I<i32>>::method(a) and <B as I<u32>>::method(a) are nonsensical because a is not of type B and the trait I<T> does not have a method called method. This is not how the ambiguous type would need to be disambiguated.

error[E0576]: cannot find method or associated constant `method` in trait `I`
  --> src/main.rs:21:20
   |
21 |     <B as I<i32>>::method(a);
   |                    ^^^^^^ not found in `I`

Instead a correct suggestion would look like one of the following:

  • <A<B> as V<i32>>::method(a)
  • <A<_> as V<i32>>::method(a)
  • <_ as V<i32>>::method(a)
  • <V<i32>>::method(a)
  • V::<i32>::method(a)

Exactly which one of the above 5 correct possibilities is chosen is related to #96292, but this issue focuses on the fact that the current suggestion's trait is wrong (I vs V).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-papercutDiagnostics: An error or lint that needs small tweaks.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