Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 5 additions & 19 deletions compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use rustc_middle::middle::privacy::{EffectiveVisibilities, EffectiveVisibility,
use rustc_middle::query::Providers;
use rustc_middle::ty::print::PrintTraitRefExt as _;
use rustc_middle::ty::{
self, Const, GenericParamDefKind, TraitRef, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable,
TypeVisitor,
self, AssocContainer, Const, GenericParamDefKind, TraitRef, Ty, TyCtxt, TypeSuperVisitable,
TypeVisitable, TypeVisitor,
};
use rustc_middle::{bug, span_bug};
use rustc_session::lint;
Expand Down Expand Up @@ -1601,6 +1601,9 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> {
if check_ty {
check.ty();
}
if is_assoc_ty && item.container == AssocContainer::Trait {
check.bounds();
}
}

fn get(&self, def_id: LocalDefId) -> Option<EffectiveVisibility> {
Expand Down Expand Up @@ -1632,20 +1635,7 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> {
self.check(def_id, item_visibility, effective_vis).generics().predicates();

for assoc_item in tcx.associated_items(id.owner_id).in_definition_order() {
if assoc_item.is_impl_trait_in_trait() {
continue;
}

self.check_assoc_item(assoc_item, item_visibility, effective_vis);

if assoc_item.is_type() {
self.check(
assoc_item.def_id.expect_local(),
item_visibility,
effective_vis,
)
.bounds();
}
}
}
DefKind::TraitAlias => {
Expand Down Expand Up @@ -1711,10 +1701,6 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> {
check.ty().trait_ref();

for assoc_item in tcx.associated_items(id.owner_id).in_definition_order() {
if assoc_item.is_impl_trait_in_trait() {
continue;
}

let impl_item_vis = if !of_trait {
min(tcx.local_visibility(assoc_item.def_id.expect_local()), impl_vis, tcx)
} else {
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/privacy/private-in-public-assoc-ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ mod m {
// applies only to the aliased types, not bounds.
pub trait PubTr {
type Alias1: PrivTr;
//~^ WARN trait `PrivTr` is more private than the item `PubTr::Alias1`
//~^ ERROR private trait `PrivTr` in public interface
type Alias2: PubTrAux1<Priv> = u8;
//~^ WARN type `Priv` is more private than the item `PubTr::Alias2`
//~^ ERROR private type `Priv` in public interface
type Alias3: PubTrAux2<A = Priv> = u8;
//~^ WARN type `Priv` is more private than the item `PubTr::Alias3`
//~^ ERROR private type `Priv` in public interface

type Alias4 = Priv;
//~^ ERROR private type `Priv` in public interface
Expand Down
42 changes: 16 additions & 26 deletions tests/ui/privacy/private-in-public-assoc-ty.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,32 @@ LL | struct Priv;
LL | type A = Priv;
| ^^^^^^ can't leak private type

warning: trait `PrivTr` is more private than the item `PubTr::Alias1`
error[E0446]: private trait `PrivTr` in public interface
--> $DIR/private-in-public-assoc-ty.rs:24:9
|
LL | type Alias1: PrivTr;
| ^^^^^^^^^^^^^^^^^^^ associated type `PubTr::Alias1` is reachable at visibility `pub(crate)`
|
note: but trait `PrivTr` is only usable at visibility `pub(self)`
--> $DIR/private-in-public-assoc-ty.rs:9:5
|
LL | trait PrivTr {}
| ^^^^^^^^^^^^
= note: `#[warn(private_bounds)]` on by default
| ------------ `PrivTr` declared as private
...
LL | type Alias1: PrivTr;
| ^^^^^^^^^^^^^^^^^^^ can't leak private trait

warning: type `Priv` is more private than the item `PubTr::Alias2`
error[E0446]: private type `Priv` in public interface
--> $DIR/private-in-public-assoc-ty.rs:26:9
|
LL | type Alias2: PubTrAux1<Priv> = u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ associated type `PubTr::Alias2` is reachable at visibility `pub(crate)`
|
note: but type `Priv` is only usable at visibility `pub(self)`
--> $DIR/private-in-public-assoc-ty.rs:8:5
|
LL | struct Priv;
| ^^^^^^^^^^^
| ----------- `Priv` declared as private
...
LL | type Alias2: PubTrAux1<Priv> = u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type

warning: type `Priv` is more private than the item `PubTr::Alias3`
error[E0446]: private type `Priv` in public interface
--> $DIR/private-in-public-assoc-ty.rs:28:9
|
LL | type Alias3: PubTrAux2<A = Priv> = u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ associated type `PubTr::Alias3` is reachable at visibility `pub(crate)`
|
note: but type `Priv` is only usable at visibility `pub(self)`
--> $DIR/private-in-public-assoc-ty.rs:8:5
|
LL | struct Priv;
| ^^^^^^^^^^^
| ----------- `Priv` declared as private
...
LL | type Alias3: PubTrAux2<A = Priv> = u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type

error[E0446]: private type `Priv` in public interface
--> $DIR/private-in-public-assoc-ty.rs:31:9
Expand Down Expand Up @@ -71,6 +61,6 @@ LL | trait PrivTr {}
LL | type Exist = impl PrivTr;
| ^^^^^^^^^^ can't leak private trait

error: aborting due to 4 previous errors; 3 warnings emitted
error: aborting due to 7 previous errors

For more information about this error, try `rustc --explain E0446`.
11 changes: 10 additions & 1 deletion tests/ui/privacy/private-in-public-warn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ mod traits {
pub trait Tr2<T: PrivTr> {} //~ ERROR trait `traits::PrivTr` is more private than the item `traits::Tr2`
pub trait Tr3 {
type Alias: PrivTr;
//~^ ERROR trait `traits::PrivTr` is more private than the item `traits::Tr3::Alias`
//~^ ERROR private trait `traits::PrivTr` in public interface
fn f<T: PrivTr>(arg: T) {}
//~^ ERROR trait `traits::PrivTr` is more private than the item `traits::Tr3::f`
fn g() -> impl PrivTr;
//~^ ERROR trait `traits::PrivTr` is more private than the item `traits::Tr3::g::{anon_assoc#0}`
fn h() -> impl PrivTr {}
//~^ ERROR private trait `traits::PrivTr` in public interface
//~| ERROR trait `traits::PrivTr` is more private than the item `traits::Tr3::h::{anon_assoc#0}`
}
impl<T: PrivTr> Pub<T> {} //~ ERROR trait `traits::PrivTr` is more private than the item `traits::Pub<T>`
impl<T: PrivTr> PubTr for Pub<T> {} // OK, trait impl predicates
Expand Down Expand Up @@ -89,7 +92,13 @@ mod generics {

pub trait Tr5 {
fn required() -> impl PrivTr<Priv<()>>;
//~^ ERROR trait `generics::PrivTr<generics::Priv<()>>` is more private than the item `Tr5::required::{anon_assoc#0}`
//~| ERROR type `generics::Priv<()>` is more private than the item `Tr5::required::{anon_assoc#0}`
fn provided() -> impl PrivTr<Priv<()>> {}
//~^ ERROR private trait `generics::PrivTr<generics::Priv<()>>` in public interface
//~| ERROR private type `generics::Priv<()>` in public interface
//~| ERROR trait `generics::PrivTr<generics::Priv<()>>` is more private than the item `Tr5::provided::{anon_assoc#0}`
//~| ERROR type `generics::Priv<()>` is more private than the item `Tr5::provided::{anon_assoc#0}`
}
}

Expand Down
Loading
Loading