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

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

Open
dtolnay opened this issue Apr 21, 2022 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. 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=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).

@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

estebank commented May 6, 2022

I think the only short term solution we can have here is not to emit the suggestion at all for these cases. A generalized solution to provide the right suggestion might require some more extensive development work than I can tackle at the moment.

@TaKO8Ki TaKO8Ki self-assigned this May 11, 2022
bors added a commit to rust-lang-ci/rust that referenced this issue May 12, 2022
…ualified-path, r=estebank

Stop suggesting non-existing fully qualified paths

This patch fixes a part of rust-lang#96295.

r? `@estebank`
@TaKO8Ki TaKO8Ki removed their assignment Jul 11, 2022
@estebank
Copy link
Contributor

estebank commented Aug 3, 2023

error[[E0282]](https://doc.rust-lang.org/stable/error_codes/E0282.html): type annotations needed
  --> src/main.rs:21:7
   |
21 |     a.method();
   |       ^^^^^^
   |
help: try using a fully qualified path to specify the expected types
   |
21 |     <A<B> as V<U>>::method(a);
   |     +++++++++++++++++++++++ ~

error[[E0283]](https://doc.rust-lang.org/stable/error_codes/E0283.html): type annotations needed
  --> src/main.rs:21:7
   |
21 |     a.method();
   |       ^^^^^^
   |
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 for `A<B>` to implement `V<_>`
  --> src/main.rs:12:12
   |
12 | impl<T, U> V<U> for A<T>
   |            ^^^^     ^^^^
13 | where
14 |     T: I<U>,
   |        ---- unsatisfied trait bound introduced here
help: try using a fully qualified path to specify the expected types
   |
21 |     <A<B> as V<U>>::method(a);
   |     +++++++++++++++++++++++ ~
error[[E0282]](https://doc.rust-lang.org/stable/error_codes/E0282.html): type annotations needed
  --> src/main.rs:21:5
   |
21 |     <A<B> as V<_>>::method(a);
   |     ^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `U` declared on the trait `V`

error[[E0283]](https://doc.rust-lang.org/stable/error_codes/E0283.html): type annotations needed
  --> src/main.rs:21:5
   |
21 |     <A<B> as V<_>>::method(a);
   |     ^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `U` declared on the trait `V`
   |
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 for `A<B>` to implement `V<_>`
  --> src/main.rs:12:12
   |
12 | impl<T, U> V<U> for A<T>
   |            ^^^^     ^^^^
13 | where
14 |     T: I<U>,
   |        ---- unsatisfied trait bound introduced here

@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. and removed D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Aug 3, 2023
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants