Skip to content

Invalid suggestion with private import #156060

@inkreasing

Description

@inkreasing

Code

mod one {
    pub struct One();
}

mod two {
    use crate::one::One;
    pub struct Two();
}

mod test {
    use crate::two::{One, Two};
}

Current output

error[E0603]: struct import `One` is private
  --> src/lib.rs:11:22
   |
11 |     use crate::two::{One, Two};
   |                      ^^^ private struct import
   |
note: the struct import `One` is defined here...
  --> src/lib.rs:6:9
   |
 6 |     use crate::one::One;
   |         ^^^^^^^^^^^^^^^
note: ...and refers to the struct `One` which is defined here
  --> src/lib.rs:2:5
   |
 2 |     pub struct One();
   |     ^^^^^^^^^^^^^^^^^ you could import this directly
help: import `One` through the re-export
   |
11 |     use crate::two::{one::One, Two};
   |                      +++++

For more information about this error, try `rustc --explain E0603`.
warning: `diagnostic_import_bug` (lib) generated 1 warning
error: could not compile `diagnostic_import_bug` (lib) due to 1 previous error; 1 warning emitted

Desired output

Rationale and extra context

The suggestion is invalid. If i try to apply it i get the error:

error[E0432]: unresolved import `crate::two::one`
  --> src/lib.rs:11:27
   |
11 |     use crate::two::{Two, one::One};
   |                           ^^^ could not find `one` in `two`

It should suggest to import One from the correct location, but gets confused by the private import. I found this when moving a struct that is used in two places from one to the other.

Other cases

Rust Version

rustc 1.97.0-nightly (f53b654a8 2026-04-30)
binary: rustc
commit-hash: f53b654a8882fd5fc036c4ca7a4ff41ce32497a6
commit-date: 2026-04-30
host: aarch64-apple-darwin
release: 1.97.0-nightly
LLVM version: 22.1.4

Anything else?

I am unsure if this is even the correct error, as it says that the struct is private. Instead it maybe should give the error i get after applying the suggestion about not being able to resolve it immediately.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-resolveArea: Name/path resolution done by `rustc_resolve` specificallyD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions