Skip to content

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

Closed
@mcy

Description

@mcy

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-resolveArea: Name/path resolution done by `rustc_resolve` specificallyD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.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