Skip to content

pub use with glob import behaves differently when re-exported vs directly used #140694

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

Open
lalala-233 opened this issue May 6, 2025 · 2 comments
Labels
A-resolve Area: Name/path resolution done by `rustc_resolve` specifically C-discussion Category: Discussion or questions that doesn't represent real issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@lalala-233
Copy link

lalala-233 commented May 6, 2025

I tried this code: Playground

mod foo {
    mod bar {
        pub(crate) fn foo() {
            println!("foo");
        }
    }
    // pub use bar::foo; // error[E0364]:`foo` is only public within the crate, and cannot be re-exported outside
    pub use bar::*;
}

fn main() {
    foo::foo();
}

I expected to see this happen:

When I use glob imports, I expected to import all importable entities that I can pub use to import directly.

Instead, this happened:

I can use foo::foo() because it is re-exported.

And compiler and clippy(-W clippy::nursery) give many warnings.

Meta

rustc --version --verbose:

rustc 1.86.0 (05f9846f8 2025-03-31) (Arch Linux rust 1:1.86.0-1)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7

Some related link:

https://doc.rust-lang.org/reference/items/use-declarations.html#glob-imports

https://doc.rust-lang.org/error_codes/E0365.html

@lalala-233 lalala-233 added the C-bug Category: This is a bug. label May 6, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 6, 2025
@bvanjoi
Copy link
Contributor

bvanjoi commented May 6, 2025

This is expected behavior because pub(crate) means it visible in crates and change it into pub(super) will make error:

mod foo {
    mod bar {
-        pub(crate) fn foo() {
+        pub(super) fn foo() {
            println!("foo");
        }
    }
    pub use bar::*;
}

fn main() {
    foo::foo();
+ //~^ ERROR
}

Additionally, the error occurs because pub use bar::foo attempts to re-export an item that only has crate-local visibility, which cannot be exposed to external crates through re-export.

@lalala-233
Copy link
Author

I know pub(crate) has this feature.

I can't use pub use bar::foo to re-export foo, so I think I can't use foo through pub use bar::*, but actually I can.

@jieyouxu jieyouxu added A-resolve Area: Name/path resolution done by `rustc_resolve` specifically T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 7, 2025
@jieyouxu jieyouxu added C-discussion Category: Discussion or questions that doesn't represent real issues. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-resolve Area: Name/path resolution done by `rustc_resolve` specifically C-discussion Category: Discussion or questions that doesn't represent real issues. 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

4 participants