rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.59k stars 12.62k forks source link

circular redundant import misreported as private struct import #107683

Closed alefminus closed 1 year ago

alefminus commented 1 year ago

Code

# src/definer.rs
use crate::importer::S;

pub(crate) struct S {
    f: u32,
}

# src/importer.rs
use crate::definer::S;

# src/main.rs
pub(crate) mod definer;
pub(crate) mod importer;

fn main() {
    println!("Hello, world!");
}

Current output

error[E0603]: struct import `S` is private
 --> src/definer.rs:1:22
  |
1 | use crate::importer::S;
  |                      ^ private struct import
  |
note: the struct import `S` is defined here...
 --> src/importer.rs:1:5
  |
1 | use crate::definer::S;
  |     ^^^^^^^^^^^^^^^^^
note: ...and refers to the struct `S` which is defined here
 --> src/definer.rs:3:1
  |
3 | pub(crate) struct S {
  | ^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

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

Desired output

circular import of struct defined in definer.rs:3:1
remove redundant import ...

Rationale and extra context

This happened to me when originally "importer.rs" contained S, and I refactored by moving S into "definer.rs", but forgot to remove the import.

The error message was confusing, although it did point to the right area (the struct S).

This happens with a recent nightly:

rustc 1.69.0-nightly (1e225413a 2023-01-28)

Other cases

No response

Anything else?

No response

edward-shen commented 1 year ago

I don't think this is a misreport of a circular import. use crate::definer::S; has private visibility so when definer.rs tries to use that export it fails due to privacy rules.

You can see it a little better if you rename the import: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=97bc270eea6957231a83eac4826269c4

Notably, making the import public reveals a better error message: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0a1cb80ba69afe0e88b47fda7fd11983

alefminus commented 1 year ago

That makes sense. Thanks for the help.

On Mon, Feb 6, 2023 at 10:50 AM Edward Shen @.***> wrote:

I don't think this is a misreport of a circular import. use crate::definer::S; has private visibility so when definer.rs tries to use that export it fails due to privacy rules.

You can see it a little better if you rename the import: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=97bc270eea6957231a83eac4826269c4

Notably, making the import public reveals a better error message: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0a1cb80ba69afe0e88b47fda7fd11983

— Reply to this email directly, view it on GitHub https://github.com/rust-lang/rust/issues/107683#issuecomment-1418719248, or unsubscribe https://github.com/notifications/unsubscribe-auth/AYWFZPDLO7PC6TNE6Y5SR3LWWC3NTANCNFSM6AAAAAAURV7JEI . You are receiving this because you authored the thread.Message ID: @.***>

-- Alon Levy Software Developer Greenvibe 054-2395317