Skip to content

Suggestion to fix clashing trait and generic names is incorrect when desired trait comes from module #109950

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

Open
schuelermine opened this issue Apr 4, 2023 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@schuelermine
Copy link
Contributor

Code

use crate::a::B;

mod a {
    pub trait B {}
}

struct C<B: B>(B);

Current output

error[E0404]: expected trait, found type parameter `B`
 --> src/lib.rs:7:13
  |
1 | use crate::a::B;
  |               - you might have meant to refer to this trait
...
7 | struct C<B: B>(B);
  |          -  ^ not a trait
  |          |
  |          found this type parameter
  |
help: consider importing this trait instead
  |
1 | use crate::a::B;
  |

Desired output

error[E0404]: expected trait, found type parameter `B`
 --> src/lib.rs:7:13
  |
1 | use crate::a::B;
  |               - you might have meant to refer to this trait
...
7 | struct C<B: B>(B);
  |          -  ^ not a trait
  |          |
  |          found this type parameter
  |

Rationale and extra context

The import is already there. Ideally it should tell you that the import is being shadowed by the generic, but barring that, it definitely shouldn’t suggest adding a useless line of code that already exists and only creates more errors.

Other cases

No response

Anything else?

This happens on stable and nightly.

@schuelermine schuelermine 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. labels Apr 4, 2023
@fmease
Copy link
Member

fmease commented Apr 5, 2023

The desired diagnostic should probably suggest fully qualifying the trait:

- struct C<B: B>(B);
+ struct C<B: crate::a::B>(B);

Technically speaking, in this specific case (where we aren't inside of a function but in a module), it could instead suggest self::B in order to pick up the otherwise unused import use crate::a::B; at the top of the file but that might be a bit esoteric. 😅

@Noratrieb Noratrieb added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Apr 5, 2023
@chenyukang
Copy link
Member

seems the label you might have meant to refer to this trait is clear enough to point out the issue 😅

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 C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

4 participants