Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AldaronLau opened this issue Mar 17, 2025 · 3 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@AldaronLau
Copy link
Contributor

AldaronLau commented Mar 17, 2025

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

@AldaronLau AldaronLau added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 17, 2025
@xizheyin
Copy link
Contributor

bar is private, so it can not be a candidate unless bar is pub. Just like,

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

    pub use self::foo::Foo;
}

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

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

It reports

$ rustc src/lib.rs 
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 one of these structs instead
   |
16 -     pub use crate::two::foo::Foo;
16 +     pub use one::Foo;
   |
16 -     pub use crate::two::foo::Foo;
16 +     pub use two::foo::bar::Foo;
   |

error: aborting due to 1 previous error

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

But crate::one::Foo; is indeed a false positive.

@AldaronLau
Copy link
Contributor Author

bar is private, so it can not be a candidate unless bar is pub

Yeah, I'm aware of this - just thought it was a better message than the one provided (I suppose even better would be asking if you meant to make that module public as well). Even just removing the help text in this case would be an improvement IMO.

@xizheyin
Copy link
Contributor

Yes, your suggestion is very useful. I'll try to fix it.
@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants