Skip to content

unresolved import consider importing this struct through its public re-export instead (not a public re-export) #138626

Closed
@AldaronLau

Description

@AldaronLau

Code

pub mod one {
    mod foo {
        pub struct Foo;
    }

    pub use self::foo::Foo;
}

pub mod two {
    mod foo {
        mod bar {
            pub struct Foo;
        }
    }

    pub use crate::two::foo::Foo;
}

Current output

    Checking rust-reexport-instead-demo v0.0.0 (/home/ald/rust-reexport-instead-demo)
error[E0432]: unresolved import `crate::two::foo::Foo`
  --> src/lib.rs:16:13
   |
16 |     pub use crate::two::foo::Foo;
   |             ^^^^^^^^^^^^^^^^^^^^ no `Foo` in `two::foo`
   |
help: consider importing this struct through its public re-export instead
   |
16 |     pub use crate::one::Foo;
   |             ~~~~~~~~~~~~~~~

For more information about this error, try `rustc --explain E0432`.
error: could not compile `rust-reexport-instead-demo` (lib) due to 1 previous error

Desired output

    Checking rust-reexport-instead-demo v0.0.0 (/home/ald/rust-reexport-instead-demo)
error[E0432]: unresolved import `crate::two::foo::Foo`
  --> src/lib.rs:16:13
   |
16 |     pub use crate::two::foo::Foo;
   |             ^^^^^^^^^^^^^^^^^^^^ no `Foo` in `two::foo`
   |
help: `Foo` exists in a different path under module `two`, consider importing:
   |
16 |     pub use crate::two::foo::bar::Foo;
   |             ~~~~~~~~~~~~~~~

For more information about this error, try `rustc --explain E0432`.
error: could not compile `rust-reexport-instead-demo` (lib) due to 1 previous error

Rationale and extra context

I found the statement that it "is a public re-export" misleading, since it is not the same type. Since the original type isn't found, the error diagnostics shouldn't assume a type on a completely different path with the same name is the one trying to be used.

Other cases

Rust Version

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-unknown-linux-gnu
release: 1.85.0
LLVM version: 19.1.7

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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