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

unreachable_pub lint thinks a pub fn in a pub(crate) struct is public #110922

Closed
mpalmer opened this issue Apr 28, 2023 · 2 comments · Fixed by #111496
Closed

unreachable_pub lint thinks a pub fn in a pub(crate) struct is public #110922

mpalmer opened this issue Apr 28, 2023 · 2 comments · Fixed by #111496
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-visibility Area: Visibility / privacy C-bug Category: This is a bug.

Comments

@mpalmer
Copy link
Contributor

mpalmer commented Apr 28, 2023

I tried this code:

// src/foo.rs
pub(crate) struct Foo();

impl Foo {
    pub fn foo() {
        println!("foo!");
    }
}

// src/lib.rs
#[warn(unreachable_pub)]

mod foo;

use foo::Foo;

pub fn bar() {
    Foo::foo();
}

I expected that this code would compile without warnings, because the Foo struct isn't visible outside the crate, and thus its associated functions won't be visible either (I'm making a bit of an assumption on this point; maybe you can access a private struct's functions outside the crate, and I just don't know enough Rust to know how?)

Instead, the compiler output this warning:

   Compiling pub_crate v0.1.0 ([...]/pub_crate)
warning: unreachable `pub` item
 --> src/foo.rs:4:5
  |
4 |     pub fn foo() {
  |     ---^^^^^^^^^
  |     |
  |     help: consider restricting its visibility: `pub(crate)`
  |
note: the lint level is defined here
 --> src/lib.rs:1:8
  |
1 | #[warn(unreachable_pub)]
  |        ^^^^^^^^^^^^^^^

warning: `pub_crate` (lib) generated 1 warning (run `cargo fix --lib -p pub_crate` to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 0.11s

Meta

rustc --version --verbose:

rustc 1.69.0 (84c898d65 2023-04-16)
binary: rustc
commit-hash: 84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc
commit-date: 2023-04-16
host: x86_64-unknown-linux-gnu
release: 1.69.0
LLVM version: 15.0.7

The output is the same if I run with rustup run nightly cargo build, where rustup run nightly rustc --version --verbose says:

rustc 1.71.0-nightly (1a6ae3d69 2023-04-27)
binary: rustc
commit-hash: 1a6ae3d692cfb52b21d0f45ba50b659486e53d6c
commit-date: 2023-04-27
host: x86_64-unknown-linux-gnu
release: 1.71.0-nightly
LLVM version: 16.0.2
@mpalmer mpalmer added the C-bug Category: This is a bug. label Apr 28, 2023
@Noratrieb
Copy link
Member

The lint is correct, albeit a little confusingly. pub means "publically accessible outside the crate", which your function is indeed not. You need to change it to pub(crate).

@Noratrieb Noratrieb added the A-visibility Area: Visibility / privacy label Apr 28, 2023
@mpalmer
Copy link
Contributor Author

mpalmer commented Apr 28, 2023

Ooooooooooh, I understand the lint's purpose better now. The pub keyword is both an action ("make this available outside this namespace") and also a declaration of intent ("I want this to be available outside this crate"). Since pub has this dual meaning, but in order to make it actually accessible outside the crate you need a "chain" of pubs from the the identifier to the crate root, the lint is identifying situations where the actions don't match the intention. Since the function is in a non-pub struct, it lacks the "chain", and so fwackoom.

I can't think of a pithy way to summarise all of the above such that it is suitable for inclusion in the compiler's error message itself, but would a PR to modify the lint's documentation to add something like the above be welcomed?

@Noratrieb Noratrieb added A-diagnostics Area: Messages for errors, warnings, and lints A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools labels Apr 28, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 1, 2023
…kfelix

Extra context for unreachable_pub lint

While experienced Rustaceans no doubt know this sort of thing already, as more of a newbie I had trouble understanding why I was triggering the lint. Hopefully this expanded explanation saves someone else some head-scratching.

Fixes rust-lang#110922
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 1, 2023
…kfelix

Extra context for unreachable_pub lint

While experienced Rustaceans no doubt know this sort of thing already, as more of a newbie I had trouble understanding why I was triggering the lint. Hopefully this expanded explanation saves someone else some head-scratching.

Fixes rust-lang#110922
@bors bors closed this as completed in 3e71f4d Jun 2, 2023
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 A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-visibility Area: Visibility / privacy C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants