Skip to content

Commit 48f6655

Browse files
petrochenkovmladedav
authored andcommitted
privacy: Report priv-in-pub as errors in associated type *bounds*
1 parent 598d6ad commit 48f6655

File tree

5 files changed

+30
-49
lines changed

5 files changed

+30
-49
lines changed

compiler/rustc_privacy/src/lib.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ use rustc_middle::middle::privacy::{EffectiveVisibilities, EffectiveVisibility,
3333
use rustc_middle::query::Providers;
3434
use rustc_middle::ty::print::PrintTraitRefExt as _;
3535
use rustc_middle::ty::{
36-
self, Const, GenericParamDefKind, TraitRef, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable,
37-
TypeVisitor,
36+
self, AssocContainer, Const, GenericParamDefKind, TraitRef, Ty, TyCtxt, TypeSuperVisitable,
37+
TypeVisitable, TypeVisitor,
3838
};
3939
use rustc_middle::{bug, span_bug};
4040
use rustc_session::lint;
@@ -1601,6 +1601,9 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> {
16011601
if check_ty {
16021602
check.ty();
16031603
}
1604+
if is_assoc_ty && item.container == AssocContainer::Trait {
1605+
check.bounds();
1606+
}
16041607
}
16051608

16061609
fn get(&self, def_id: LocalDefId) -> Option<EffectiveVisibility> {
@@ -1633,15 +1636,6 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> {
16331636

16341637
for assoc_item in tcx.associated_items(id.owner_id).in_definition_order() {
16351638
self.check_assoc_item(assoc_item, item_visibility, effective_vis);
1636-
1637-
if assoc_item.is_type() {
1638-
self.check(
1639-
assoc_item.def_id.expect_local(),
1640-
item_visibility,
1641-
effective_vis,
1642-
)
1643-
.bounds();
1644-
}
16451639
}
16461640
}
16471641
DefKind::TraitAlias => {

tests/ui/privacy/private-in-public-assoc-ty.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ mod m {
2222
// applies only to the aliased types, not bounds.
2323
pub trait PubTr {
2424
type Alias1: PrivTr;
25-
//~^ WARN trait `PrivTr` is more private than the item `PubTr::Alias1`
25+
//~^ ERROR private trait `PrivTr` in public interface
2626
type Alias2: PubTrAux1<Priv> = u8;
27-
//~^ WARN type `Priv` is more private than the item `PubTr::Alias2`
27+
//~^ ERROR private type `Priv` in public interface
2828
type Alias3: PubTrAux2<A = Priv> = u8;
29-
//~^ WARN type `Priv` is more private than the item `PubTr::Alias3`
29+
//~^ ERROR private type `Priv` in public interface
3030

3131
type Alias4 = Priv;
3232
//~^ ERROR private type `Priv` in public interface

tests/ui/privacy/private-in-public-assoc-ty.stderr

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,32 @@ LL | struct Priv;
77
LL | type A = Priv;
88
| ^^^^^^ can't leak private type
99

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

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

35-
warning: type `Priv` is more private than the item `PubTr::Alias3`
28+
error[E0446]: private type `Priv` in public interface
3629
--> $DIR/private-in-public-assoc-ty.rs:28:9
3730
|
38-
LL | type Alias3: PubTrAux2<A = Priv> = u8;
39-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ associated type `PubTr::Alias3` is reachable at visibility `pub(crate)`
40-
|
41-
note: but type `Priv` is only usable at visibility `pub(self)`
42-
--> $DIR/private-in-public-assoc-ty.rs:8:5
43-
|
4431
LL | struct Priv;
45-
| ^^^^^^^^^^^
32+
| ----------- `Priv` declared as private
33+
...
34+
LL | type Alias3: PubTrAux2<A = Priv> = u8;
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type
4636

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

74-
error: aborting due to 4 previous errors; 3 warnings emitted
64+
error: aborting due to 7 previous errors
7565

7666
For more information about this error, try `rustc --explain E0446`.

tests/ui/privacy/private-in-public-warn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ mod traits {
4545
pub trait Tr2<T: PrivTr> {} //~ ERROR trait `traits::PrivTr` is more private than the item `traits::Tr2`
4646
pub trait Tr3 {
4747
type Alias: PrivTr;
48-
//~^ ERROR trait `traits::PrivTr` is more private than the item `traits::Tr3::Alias`
48+
//~^ ERROR private trait `traits::PrivTr` in public interface
4949
fn f<T: PrivTr>(arg: T) {}
5050
//~^ ERROR trait `traits::PrivTr` is more private than the item `traits::Tr3::f`
5151
fn g() -> impl PrivTr;

tests/ui/privacy/private-in-public-warn.stderr

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,14 @@ note: but trait `traits::PrivTr` is only usable at visibility `pub(self)`
170170
LL | trait PrivTr {}
171171
| ^^^^^^^^^^^^
172172

173-
error: trait `traits::PrivTr` is more private than the item `traits::Tr3::Alias`
173+
error[E0446]: private trait `traits::PrivTr` in public interface
174174
--> $DIR/private-in-public-warn.rs:47:9
175175
|
176-
LL | type Alias: PrivTr;
177-
| ^^^^^^^^^^^^^^^^^^ associated type `traits::Tr3::Alias` is reachable at visibility `pub(crate)`
178-
|
179-
note: but trait `traits::PrivTr` is only usable at visibility `pub(self)`
180-
--> $DIR/private-in-public-warn.rs:37:5
181-
|
182176
LL | trait PrivTr {}
183-
| ^^^^^^^^^^^^
177+
| ------------ `traits::PrivTr` declared as private
178+
...
179+
LL | type Alias: PrivTr;
180+
| ^^^^^^^^^^^^^^^^^^ can't leak private trait
184181

185182
error: trait `traits::PrivTr` is more private than the item `traits::Tr3::f`
186183
--> $DIR/private-in-public-warn.rs:49:9

0 commit comments

Comments
 (0)