Skip to content

rustc incorrectly suggests removing imports that shadow shadowing glob imports #92904

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
mcy opened this issue Jan 14, 2022 · 0 comments · Fixed by #109599
Closed

rustc incorrectly suggests removing imports that shadow shadowing glob imports #92904

mcy opened this issue Jan 14, 2022 · 0 comments · Fixed by #109599
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name/path resolution done by `rustc_resolve` specifically D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. 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

@mcy
Copy link
Contributor

mcy commented Jan 14, 2022

Given the following code: https://godbolt.org/z/vdc4YYafK

pub struct Foo(bar::Bar);

pub mod bar {
  pub struct Foo(pub Bar);
  pub struct Bar(pub char);
}

pub fn warning() -> Foo {
  use bar::*;
  use self::Foo;
  Foo(Bar('a'))
}

pub fn error() -> Foo {
  use bar::*;
  // use self::Foo;
  Foo(Bar('a'))
}

The current output is:

warning: the item `Foo` is imported redundantly
  --> <source>:10:7
   |
1  | pub struct Foo(bar::Bar);
   | ------------------------- the item `Foo` is already defined here
...
10 |   use self::Foo;
   |       ^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

error[E0308]: mismatched types
  --> <source>:17:3
   |
14 | pub fn error() -> Foo {
   |                   --- expected `Foo` because of return type
...
17 |   Foo(Bar('a'))
   |   ^^^^^^^^^^^^^ expected struct `Foo`, found struct `bar::Foo`

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0308`.

The warning for redundant import is incorrect, and is required for code to compile: bar::* shadows the implicit self::Foo in this scope, so making it explicit shadows the implicit bar::*. This should either be an error or not a warning; the latter is probably correct.

The non-glob version is handled correctly: https://godbolt.org/z/653K6svve

@mcy mcy 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 Jan 14, 2022
@mcy mcy changed the title rustc incorrectly suggests removing imports that shadow shadowing imports rustc incorrectly suggests removing imports that shadow shadowing glob imports Jan 14, 2022
@estebank estebank added A-resolve Area: Name/path resolution done by `rustc_resolve` specifically D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-papercut Diagnostics: An error or lint that needs small tweaks. labels Jan 14, 2022
@bors bors closed this as completed in 35d06f9 Apr 4, 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-resolve Area: Name/path resolution done by `rustc_resolve` specifically D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. 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

Successfully merging a pull request may close this issue.

2 participants