Skip to content

Commit c5221eb

Browse files
committed
privacy: Report priv-in-pub as errors in associated type *bounds*
1 parent d682af8 commit c5221eb

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
@@ -29,8 +29,8 @@ use rustc_middle::middle::privacy::{EffectiveVisibilities, EffectiveVisibility,
2929
use rustc_middle::query::Providers;
3030
use rustc_middle::ty::print::PrintTraitRefExt as _;
3131
use rustc_middle::ty::{
32-
self, Const, GenericParamDefKind, TraitRef, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable,
33-
TypeVisitor,
32+
self, AssocContainer, Const, GenericParamDefKind, TraitRef, Ty, TyCtxt, TypeSuperVisitable,
33+
TypeVisitable, TypeVisitor,
3434
};
3535
use rustc_middle::{bug, span_bug};
3636
use rustc_session::lint;
@@ -1594,6 +1594,9 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> {
15941594
if check_ty {
15951595
check.ty();
15961596
}
1597+
if is_assoc_ty && item.container == AssocContainer::Trait {
1598+
check.bounds();
1599+
}
15971600
}
15981601

15991602
fn get(&self, def_id: LocalDefId) -> Option<EffectiveVisibility> {
@@ -1630,15 +1633,6 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> {
16301633
}
16311634

16321635
self.check_assoc_item(assoc_item, item_visibility, effective_vis);
1633-
1634-
if assoc_item.is_type() {
1635-
self.check(
1636-
assoc_item.def_id.expect_local(),
1637-
item_visibility,
1638-
effective_vis,
1639-
)
1640-
.bounds();
1641-
}
16421636
}
16431637
}
16441638
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)