rust-lang / rust-lang/rust

Should public_private_dependencies consider unreachable visibility?

Open
#130,079 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug F-public_private_dependencies L-public_private_dependencies T-compiler T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

mod foo {
    pub fn example() -> regex::Regex {
        regex::Regex::new("test").unwrap()
    }
}

pub fn x() {
    foo::example();
}

with cargo's public-dependency feature enabled, and regex is a private dependency.

I expected to see this happen: No warning

Instead, this happened: Generated a warning about the dependency in a public interface, but there is no exposure of the dependency in the public interface.

warning: type `regex::Regex` from private dependency 'regex' in public interface
 --> src/lib.rs:2:5
  |
2 |     pub fn example() -> regex::Regex {
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(exported_private_dependencies)]` on by default

In https://github.com/rust-lang/rust/issues/44663#issuecomment-1552721144, bjorn3 mentioned:

We consider any pub item to be public, even if not actually reachable. This is also why for example mod sealed { pub trait Sealed {} } pub trait MyTrait: Sealed {} is allowed despite Sealed not being reachable.

However, I'm not sure I completely agree with that reasoning. In the example above, there is no exposure of the private dependency in any types. This would make more sense if unreachable-pub was on by default, but it's not. Although I can sympathize that unreachable pub is probably bad form, it is very common in Rust code and would be a significant hurdle for false-positives of exported_private_dependencies.

Meta
rustc 1.83.0-nightly (9c01301c5 2024-09-05)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example with Cargo's public-dependency feature and the exported_private_dependencies lint enabled. Read the handling of public items alongside the unreachable-pub lint, then determine whether unreachable visibility should suppress this warning; done means the behavior and corresponding regression coverage match the agreed visibility rule.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.