Skip to content

unreachable_pub lint suggests a breaking fix for associated type on trait #110923

Open
@mpalmer

Description

@mpalmer

(Apologies for the terrible title; I'm not sure what the correct terminology is for what's going on)

I tried this code:

// src/traiter.rs
pub trait CrateTrait<const X: usize> {}

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

mod traiter;

pub trait PubTrate<const X: usize> {
    type T: traiter::CrateTrait<X>;
}

This produces the following warning output:

   Compiling pub_crate v0.1.0 ([...]/pub_crate)
warning: unreachable `pub` item
 --> src/traiter.rs:1:1
  |
1 | pub trait CrateTrait<const X: usize> {}
  | ---^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  | |
  | help: consider restricting its visibility: `pub(crate)`
  |
  = help: or consider exporting it for use by other crates
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

When I took the compiler's helpful advice to run cargo fix --lib -p pub_crate, it fails, with this output:

    Checking pub_crate v0.1.0 ([...]/pub_crate)
warning: failed to automatically apply fixes suggested by rustc to crate `pub_crate`
                                              
after fixes were automatically applied the compiler reported errors within these files:
                                              
  * src/lib.rs
  * src/traiter.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see 
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0445]: crate-private trait `CrateTrait<X>` in public interface
 --> src/lib.rs:6:5
  |
6 |     type T: traiter::CrateTrait<X>;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak crate-private trait
  |
 ::: src/traiter.rs:1:1
  |
1 | pub(crate) trait CrateTrait<const X: usize> {}
  | ------------------------------------------- `CrateTrait<X>` declared as crate-private

error: aborting due to previous error

For more information about this error, try `rustc --explain E0445`.
Original diagnostics will follow.

warning: unreachable `pub` item
 --> src/traiter.rs:1:1
  |
1 | pub trait CrateTrait<const X: usize> {}
  | ---^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  | |
  | help: consider restricting its visibility: `pub(crate)`
  |
  = help: or consider exporting it for use by other crates
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.34s

Meta

rustc --version --verbose:

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

Also occurs with stable, this version produces seemingly-identical results:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-visibilityArea: Visibility / privacyC-bugCategory: This is a bug.L-unreachable_pubLint: unreachable_pubT-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