Skip to content

"Private trait in public interface warning" changes to error when an unrelated pub(crate) is present #89857

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

Closed
acceleratesage opened this issue Oct 13, 2021 · 4 comments
Labels
C-bug Category: This is a bug.

Comments

@acceleratesage
Copy link

Implementing a public trait for all types that implement a private trait emits a warning. However the presence of pub(crate) in an unrelated piece of code turns that warning into an error.

The warning is: warning: private trait `PrivateTrait` in public interface (error E0445)
The error is: error[E0445]: private trait `PrivateTrait` in public interface

The behaviour can be reproduced by commenting out pub(crate) struct Foo in this code:

trait PrivateTrait {}

pub trait PublicTrait {}

impl<T> PublicTrait for T where T: PrivateTrait {}


/*
Comment this out and the error is gone and the code compiles with a warning
*/
pub(crate) struct Foo;

Meta

I confirmed the presence of this behaviour on the rust playground for:

  • Stable channel
    Build using the Stable version: 1.55.0

  • Beta channel
    Build using the Beta version: 1.56.0-beta.4
    (2021-10-04 e6e620e)

  • Nightly channel
    Build using the Nightly version: 1.57.0-nightly
    (2021-10-12 d7c97a0)

References

@acceleratesage acceleratesage added the C-bug Category: This is a bug. label Oct 13, 2021
@trentj
Copy link

trentj commented Oct 13, 2021

I believe #48054 means this should one day be accepted without any diagnostic, although the current implementation status is that it still triggers the legacy public-in-private lint. Even so, that lint ought to be a warning, not an error.

@SNCPlay42
Copy link
Contributor

This comment looks relevant:

////////////////////////////////////////////////////////////////////////////////
/// Visitor used to determine if pub(restricted) is used anywhere in the crate.
///
/// This is done so that `private_in_public` warnings can be turned into hard errors
/// in crates that have been updated to use pub(restricted).
////////////////////////////////////////////////////////////////////////////////

I think the intent is that since newer crates can use pub(crate) and similar visibilities, they no longer need to use private-in-public items to achieve the same effect, and so the compiler can emit a hard error instead of accepting the behavior for compatibility reasons when the new visibilities are being used.

@PatchMixolydic
Copy link
Contributor

That seems like pretty surprising behaviour, especially given Rust's avoidance of action at a distance. Granted, the warning is a future compat lint, but I'd expect it to always warn or always error in a given release using a given edition.

@Dylan-DPC
Copy link
Member

This code now compiles fine without the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

5 participants