Skip to content

Invalid code suggestion for ambigous trait generic argument #94197

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
russelltg opened this issue Feb 20, 2022 · 2 comments
Closed

Invalid code suggestion for ambigous trait generic argument #94197

russelltg opened this issue Feb 20, 2022 · 2 comments
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. 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

@russelltg
Copy link

russelltg commented Feb 20, 2022

I tried this code:

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

trait I<T> {}

impl I<u32> for B {}
impl I<u64> for B {}

trait V<T> {
    fn create(&self) -> T;
}

impl<T, U> V<U> for A<T> where T: I<U>, U: Default {
    fn create(&self) -> U {
        U::default()
    }
}

fn main() {
    let a = A(B);
    a.create();
    // <B as I<u32>>::create(a);

}

playground

I expected to see this happen: Compiler error with either correct suggestion or no suggestion at all

Instead, this happened: Suggestion that does not compile:

help: use the fully qualified path for the potential candidates
   |
22 |     <B as I<u32>>::create(a);
   |     ~~~~~~~~~~~~~~~~~~~~~~~~
22 |     <B as I<u64>>::create(a);
   |     ~~~~~~~~~~~~~~~~~~~~~~~~

What even is the correct way to disambiguate this situation?

Meta

rustc --version --verbose:

rustc 1.61.0-nightly (3b348d932 2022-02-19)
binary: rustc
commit-hash: 3b348d932aa5c9884310d025cf7c516023fd0d9a
commit-date: 2022-02-19
host: x86_64-unknown-linux-gnu
release: 1.61.0-nightly
LLVM version: 14.0.0

@russelltg russelltg added the C-bug Category: This is a bug. label Feb 20, 2022
@dtolnay
Copy link
Member

dtolnay commented Apr 21, 2022

Seems like the correct suggestions would be:

<A<B> as V<u32>>::create(&a);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<A<B> as V<u64>>::create(&a);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@dtolnay dtolnay added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Apr 21, 2022
@dtolnay dtolnay closed this as completed Apr 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. 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

No branches or pull requests

2 participants