Skip to content

Revert "Do not check privacy for RPITIT."#146470

Closed
mladedav wants to merge 2 commits intorust-lang:mainfrom
mladedav:dm/reenable-private-in-public-rpitit-errors
Closed

Revert "Do not check privacy for RPITIT."#146470
mladedav wants to merge 2 commits intorust-lang:mainfrom
mladedav:dm/reenable-private-in-public-rpitit-errors

Conversation

@mladedav
Copy link
Copy Markdown
Contributor

@mladedav mladedav commented Sep 12, 2025

The changes here were first merged in #143357 and later reverted in #144098 as it introduces new hard errors. There was a crater run tracked in #144139 to see how much projects would be broken (not that many, a few repositories on github are affected).

This reenables hard errors for privacy in RPITIT.

Fixes #143531
Closes #144139
Hopefully closes #71043

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 12, 2025
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Sep 12, 2025

compiler-errors is not on the review rotation at the moment.
They may take a while to respond.

@compiler-errors
Copy link
Copy Markdown
Contributor

r? cjgillot

@rustbot rustbot assigned cjgillot and unassigned compiler-errors Sep 12, 2025
@mladedav
Copy link
Copy Markdown
Contributor Author

Gentle ping @cjgillot

@cjgillot cjgillot added the I-lang-nominated Nominated for discussion during a lang team meeting. label Nov 2, 2025
@traviscross traviscross added needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang T-lang Relevant to the language team I-lang-radar Items that are on lang's radar and will need eventual work or consideration. labels Nov 5, 2025
@joshtriplett
Copy link
Copy Markdown
Member

joshtriplett commented Nov 5, 2025

This came up in today's @rust-lang/lang meeting. It's clear why this needed an FCP (as it's a breaking change), but we didn't feel like we had the context. Could we get a clear ask for what exactly the new hard error is that we're reviewing?

Does this just make it a hard error to write a public trait that has something like -> impl Trait using a private trait? Or is there more to it than that? The discussion in #144139 makes it sound like it's substantially more complex and subtle than that.

@mladedav
Copy link
Copy Markdown
Contributor Author

mladedav commented Nov 6, 2025

It is a little bit more subtle in the current form, the main weirdness I remember is that creating a required method returning a private impl trait does not error out, only providing an implementation does, so

pub trait Foo {
    fn required_impl_trait() -> impl Private;
}

does not error while

pub trait Foo {
    fn required_impl_trait() -> impl Private;
}

impl Foo for S {
    fn required_impl_trait() -> impl Private { X }
}

and

pub trait Foo {
    fn provided_impl_trait() -> impl Private { X }
}

both error out.

The error is also reported when the Private trait is used in generic bounds of the method.

And then for AFIT it seems to work the same after desugaring, so async fn required_async_concrete() -> PrivateStruct; works the same way as it desugars to returning an impl trait which is considered private due to its private associated type. So declaring the method in the trait is not linted while implementing it is.

As I understand it, this is not as strict as it should be based on @petrochenkov's comment and even the first case of defining the trait should be rejected.

Here is a playground with more cases to see what does and does not produce errors (though the errors are just comments but compiling the code on this branch should provide the stated results).


To summarize, this adds errors when using a private trait in RPITIT but when the offending trait is not used in a trait bound and an implementation is not provided, there is a false negative an the error is not emitted even though it should be.

@traviscross
Copy link
Copy Markdown
Contributor

@bors2 try

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Nov 6, 2025
…-errors, r=<try>

Revert "Do not check privacy for RPITIT."
@traviscross

This comment was marked as resolved.

@traviscross

This comment was marked as resolved.

@traviscross
Copy link
Copy Markdown
Contributor

traviscross commented Nov 6, 2025

@mladedav: I'm having trouble working out the reason why we'd give a hard error for the RPIT-in-trait-impl,

trait PrivTr {}
impl PrivTr for () {}
#[expect(private_bounds)]
pub trait PubTr {
    fn f1() -> impl PrivTr;
}
impl<T> PubTr for T {
    #[expect(private_interfaces)]
    fn f1() -> impl PrivTr {}
    //~^ error[E0446]: private trait `PrivTr` in public interface
    //~| help: can't leak private trait
}

given that we don't give an error for an RPIT-in-free-function,

trait PrivTr {}
impl PrivTr for () {}

#[expect(private_interfaces)]
pub fn f2() -> impl PrivTr {} //~ OK

and given that we allow the comparable associated type desugaring of the RPITIT:

trait PrivTr {}
impl PrivTr for () {}
pub trait PubTr {
    #[expect(private_bounds)]
    type F1: PrivTr; //~ OK
    fn f1() -> Self::F1;
}
impl<T> PubTr for T {
    type F1 = ();
    fn f1() -> Self::F1 {}
}

What's the rationale here?

cc @petrochenkov


I note that on nightly we give an error for this, when desugaring the RPIT-in-trait-impl to ATPIT:

#![feature(impl_trait_in_assoc_type)]
trait PrivTr {}
impl PrivTr for () {}
pub trait PubTr {
    #[expect(private_bounds)]
    type F1: PrivTr; //~ OK
    fn f1() -> Self::F1;
}
impl<T> PubTr for T {
    type F1 = impl PrivTr;
    //~^ error[E0446]: private trait `PrivTr` in public interface
    fn f1() -> Self::F1 {}
}

What's the rationale here? It makes sense why we can't leak a private type in this way -- we'd then be allowing a private type to be named. Why does this rise to the level of a hard error for a private trait in an impl trait bound?


Also, on the PR, I notice that placing the expect over the trait item doesn't work.

trait PrivTr {}
impl PrivTr for () {}
pub trait PubTr {
    #[expect(private_bounds)] //~ warning: this lint expectation is unfulfilled
    fn f1() -> impl PrivTr;
    //~^ warning: trait `PrivTr` is more private than the item `PubTr::f1::{anon_assoc#0}`
}

Should it?

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Nov 6, 2025

☀️ Try build successful (CI)
Build commit: e117153 (e117153a45c546e883c1f91d82611775fcaeffe0, parent: c90bcb9571b7aab0d8beaa2ce8a998ffaf079d38)

@traviscross
Copy link
Copy Markdown
Contributor

@craterbot check

@craterbot
Copy link
Copy Markdown
Collaborator

👌 Experiment pr-146470 created and queued.
🤖 Automatically detected try build e117153
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 6, 2025
@craterbot
Copy link
Copy Markdown
Collaborator

🚧 Experiment pr-146470 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@traviscross traviscross added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed I-lang-nominated Nominated for discussion during a lang team meeting. labels Nov 10, 2025
@petrochenkov
Copy link
Copy Markdown
Contributor

Lite version of this PR, just fixing lints - #151168.

@petrochenkov
Copy link
Copy Markdown
Contributor

Sigh, there are more bugs due to missing RPITIT visiting - 7c16584.
It explains different diagnostics in RPITITs in trait functions with and without body in examples in the thread.

@petrochenkov
Copy link
Copy Markdown
Contributor

I could not construct anything similar from private Generics, Predicates and Bounds so far.

I think I'm convinced that you cannot leak anything private into the associated type RHS through Bounds and Predicates, if the RHS itself is checked (#146470 (comment)).
Most of the regressions in #146470 (comment) are from the bounds.

After #151168 is merged I'll make my version of the fix and run it through crater.
@rustbot blocked

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 16, 2026
@theemathas
Copy link
Copy Markdown
Contributor

I found a privacy-related bug, filed as #151284. I have not tested it with this PR, since this PR does not compile on my computer.

Error from compiling this PR
error[E0432]: unresolved import `rustc_middle::ty::AssocContainer`
  --> compiler\rustc_privacy\src\lib.rs:36:11
   |
36 |     self, AssocContainer, Const, GenericParamDefKind, TraitRef, Ty, TyCtxt, TypeSuperVisitable,
   |           ^^^^^^^^^^^^^^
   |           |
   |           no `AssocContainer` in `ty`
   |           help: a similar name exists in the module: `AssocItemContainer`

For more information about this error, try `rustc --explain E0432`.
error: could not compile `rustc_privacy` (lib) due to 1 previous error

rust-bors bot pushed a commit that referenced this pull request Jan 30, 2026
privacy: Fix privacy lints in RPITITs

Visit RPITITs and report `private_interfaces`, `private_bounds` and `exported_private_dependencies` in them (these are regular, non-deprecation lints).
New hard errors are not reported, #146470 is for hard errors.
So this PR doesn't contain any breakage or language changes.
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Jan 30, 2026

☔ The latest upstream changes (presumably #151168) made this pull request unmergeable. Please resolve the merge conflicts.

RalfJung pushed a commit to RalfJung/miri that referenced this pull request Feb 1, 2026
privacy: Fix privacy lints in RPITITs

Visit RPITITs and report `private_interfaces`, `private_bounds` and `exported_private_dependencies` in them (these are regular, non-deprecation lints).
New hard errors are not reported, rust-lang/rust#146470 is for hard errors.
So this PR doesn't contain any breakage or language changes.
@petrochenkov petrochenkov added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Feb 2, 2026
Kobzol pushed a commit to Kobzol/portable-simd that referenced this pull request Feb 3, 2026
privacy: Fix privacy lints in RPITITs

Visit RPITITs and report `private_interfaces`, `private_bounds` and `exported_private_dependencies` in them (these are regular, non-deprecation lints).
New hard errors are not reported, rust-lang/rust#146470 is for hard errors.
So this PR doesn't contain any breakage or language changes.
@petrochenkov
Copy link
Copy Markdown
Contributor

I'll close this in favor of #152543.

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 12, 2026
@rust-rfcbot rust-rfcbot removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Feb 12, 2026
@mladedav mladedav deleted the dm/reenable-private-in-public-rpitit-errors branch February 12, 2026 16:48
makai410 pushed a commit to makai410/rustc_public that referenced this pull request Mar 19, 2026
privacy: Fix privacy lints in RPITITs

Visit RPITITs and report `private_interfaces`, `private_bounds` and `exported_private_dependencies` in them (these are regular, non-deprecation lints).
New hard errors are not reported, rust-lang/rust#146470 is for hard errors.
So this PR doesn't contain any breakage or language changes.
makai410 pushed a commit to makai410/rustc_public that referenced this pull request Mar 19, 2026
privacy: Fix privacy lints in RPITITs

Visit RPITITs and report `private_interfaces`, `private_bounds` and `exported_private_dependencies` in them (these are regular, non-deprecation lints).
New hard errors are not reported, rust-lang/rust#146470 is for hard errors.
So this PR doesn't contain any breakage or language changes.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 22, 2026
privacy: Fix type privacy holes in RPITITs

A subset of rust-lang#146470.

Private types in RPITITs now report hard errors.
Private types in bounds of associated types still only report the `private_bounds` lint due to unacceptable amount of breakage (rust-lang#146470 (comment)).

Closes rust-lang#144139
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 22, 2026
privacy: Fix type privacy holes in RPITITs

A subset of rust-lang#146470.

Private types in RPITITs now report hard errors.
Private types in bounds of associated types still only report the `private_bounds` lint due to unacceptable amount of breakage (rust-lang#146470 (comment)).

Closes rust-lang#144139
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 22, 2026
privacy: Fix type privacy holes in RPITITs

A subset of rust-lang#146470.

Private types in RPITITs now report hard errors.
Private types in bounds of associated types still only report the `private_bounds` lint due to unacceptable amount of breakage (rust-lang#146470 (comment)).

Closes rust-lang#144139
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Mar 23, 2026
privacy: Fix type privacy holes in RPITITs

A subset of rust-lang#146470.

Private types in RPITITs now report hard errors.
Private types in bounds of associated types still only report the `private_bounds` lint due to unacceptable amount of breakage (rust-lang#146470 (comment)).

Closes rust-lang#144139
github-actions bot pushed a commit to rust-lang/stdarch that referenced this pull request Mar 26, 2026
privacy: Fix type privacy holes in RPITITs

A subset of rust-lang/rust#146470.

Private types in RPITITs now report hard errors.
Private types in bounds of associated types still only report the `private_bounds` lint due to unacceptable amount of breakage (rust-lang/rust#146470 (comment)).

Closes rust-lang/rust#144139
makai410 pushed a commit to makai410/rustc_public that referenced this pull request Mar 27, 2026
privacy: Fix privacy lints in RPITITs

Visit RPITITs and report `private_interfaces`, `private_bounds` and `exported_private_dependencies` in them (these are regular, non-deprecation lints).
New hard errors are not reported, rust-lang/rust#146470 is for hard errors.
So this PR doesn't contain any breakage or language changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-lang-radar Items that are on lang's radar and will need eventual work or consideration. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team

Projects

None yet