Skip to content

E0603: import directly/through re-export should attach good suggestions for grouped imports #157453

@fee1-dead

Description

@fee1-dead

Code

mod one {
    pub struct One();
}

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

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

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

error[E0603]: struct `Path` is private
  --> src/lib.rs:12:19
   |
12 |     use std::{fs::Path};
   |                   ^^^^ private struct
   |
note: the struct `Path` is defined here
  --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/fs.rs:46:19
   |
46 | use crate::path::{Path, PathBuf};
   |                   ^^^^

Desired 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
   |
10 |   +use crate::one::One;
11 |   ~use crate::two::Two;

error[E0603]: struct `Path` is private
  --> src/lib.rs:12:19
   |
12 |     use std::{fs::Path};
   |                   ^^^^ private struct
   |
note: the struct `Path` is defined here
  --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/fs.rs:46:19
   |
46 | use crate::path::{Path, PathBuf};
   |                   ^^^^
help: import `Path` directly
   |
12 |   ~use std::path::Path;

Rationale and extra context

#156244 fixed #156060 and #155348 by not emitting a suggestion at all, however I think we could still emit good suggestion, by: extracting an import out of a group and use the right path if the path is inside a group; or change the entire import if we're in a group of 1.

Other cases

Rust Version

latest playground nightly

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.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