Skip to content

Commit

Permalink
Report associated types of private traits as being private themselves
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Oct 23, 2023
1 parent d849942 commit f6919fa
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 48 deletions.
20 changes: 8 additions & 12 deletions compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,20 +221,16 @@ where
}

let kind = match kind {
ty::Inherent => Some("associated type"),
ty::Weak => Some("type alias"),
// Trait associated types don't have visibility, they are
// visible if their parent trait is.
ty::Projection => None,
ty::Inherent => "associated type",
ty::Weak => "type alias",
ty::Projection => "associated type",
_ => unreachable!(),
};
if let Some(kind) = kind {
self.def_id_visitor.visit_def_id(
data.def_id,
kind,
&LazyDefPathStr { def_id: data.def_id, tcx },
)?;
}
self.def_id_visitor.visit_def_id(
data.def_id,
kind,
&LazyDefPathStr { def_id: data.def_id, tcx },
)?;

// This will also visit args if necessary, so we don't need to recurse.
return if V::SHALLOW {
Expand Down
1 change: 1 addition & 0 deletions tests/ui/privacy/associated-item-privacy-trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod priv_trait {
let _: <Pub as PrivTr>::AssocTy;
//~^ ERROR associated type `PrivTr::AssocTy` is private
pub type InSignatureTy = <Pub as PrivTr>::AssocTy;
//~^ ERROR associated type `PrivTr::AssocTy` is private
pub trait InSignatureTr: PrivTr {}
//~^ ERROR trait `PrivTr` is private
impl PrivTr for u8 {}
Expand Down
61 changes: 36 additions & 25 deletions tests/ui/privacy/associated-item-privacy-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,19 @@ LL | priv_trait::mac!();
|
= note: this error originates in the macro `priv_trait::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: associated type `PrivTr::AssocTy` is private
--> $DIR/associated-item-privacy-trait.rs:25:34
|
LL | pub type InSignatureTy = <Pub as PrivTr>::AssocTy;
| ^^^^^^^^^^^^^^^^^^^^^^^^ private associated type
...
LL | priv_trait::mac!();
| ------------------ in this macro invocation
|
= note: this error originates in the macro `priv_trait::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: trait `PrivTr` is private
--> $DIR/associated-item-privacy-trait.rs:26:34
--> $DIR/associated-item-privacy-trait.rs:27:34
|
LL | pub trait InSignatureTr: PrivTr {}
| ^^^^^^ private trait
Expand All @@ -65,7 +76,7 @@ LL | priv_trait::mac!();
= note: this error originates in the macro `priv_trait::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: trait `PrivTr` is private
--> $DIR/associated-item-privacy-trait.rs:28:14
--> $DIR/associated-item-privacy-trait.rs:29:14
|
LL | impl PrivTr for u8 {}
| ^^^^^^ private trait
Expand All @@ -76,7 +87,7 @@ LL | priv_trait::mac!();
= note: this error originates in the macro `priv_trait::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_signature::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:45:21
--> $DIR/associated-item-privacy-trait.rs:46:21
|
LL | let value = <Pub as PubTr>::method;
| ^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -87,7 +98,7 @@ LL | priv_signature::mac!();
= note: this error originates in the macro `priv_signature::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_signature::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:47:9
--> $DIR/associated-item-privacy-trait.rs:48:9
|
LL | value;
| ^^^^^ private type
Expand All @@ -98,7 +109,7 @@ LL | priv_signature::mac!();
= note: this error originates in the macro `priv_signature::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_signature::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:49:13
--> $DIR/associated-item-privacy-trait.rs:50:13
|
LL | Pub.method(loop {});
| ^^^^^^ private type
Expand All @@ -109,7 +120,7 @@ LL | priv_signature::mac!();
= note: this error originates in the macro `priv_signature::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:66:21
--> $DIR/associated-item-privacy-trait.rs:67:21
|
LL | let value = <Pub as PubTr>::method::<Priv>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -120,7 +131,7 @@ LL | priv_substs::mac!();
= note: this error originates in the macro `priv_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:68:9
--> $DIR/associated-item-privacy-trait.rs:69:9
|
LL | value;
| ^^^^^ private type
Expand All @@ -131,7 +142,7 @@ LL | priv_substs::mac!();
= note: this error originates in the macro `priv_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:70:9
--> $DIR/associated-item-privacy-trait.rs:71:9
|
LL | Pub.method::<Priv>();
| ^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -142,7 +153,7 @@ LL | priv_substs::mac!();
= note: this error originates in the macro `priv_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_parent_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:90:21
--> $DIR/associated-item-privacy-trait.rs:91:21
|
LL | let value = <Pub as PubTr>::method;
| ^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -153,7 +164,7 @@ LL | priv_parent_substs::mac!();
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_parent_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:92:9
--> $DIR/associated-item-privacy-trait.rs:93:9
|
LL | value;
| ^^^^^ private type
Expand All @@ -164,7 +175,7 @@ LL | priv_parent_substs::mac!();
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_parent_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:94:21
--> $DIR/associated-item-privacy-trait.rs:95:21
|
LL | let value = <Pub as PubTr<_>>::method;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -175,7 +186,7 @@ LL | priv_parent_substs::mac!();
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_parent_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:96:9
--> $DIR/associated-item-privacy-trait.rs:97:9
|
LL | value;
| ^^^^^ private type
Expand All @@ -186,7 +197,7 @@ LL | priv_parent_substs::mac!();
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_parent_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:98:9
--> $DIR/associated-item-privacy-trait.rs:99:9
|
LL | Pub.method();
| ^^^^^^^^^^^^ private type
Expand All @@ -197,7 +208,7 @@ LL | priv_parent_substs::mac!();
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_parent_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:101:21
--> $DIR/associated-item-privacy-trait.rs:102:21
|
LL | let value = <Priv as PubTr<_>>::method;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -208,7 +219,7 @@ LL | priv_parent_substs::mac!();
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_parent_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:103:9
--> $DIR/associated-item-privacy-trait.rs:104:9
|
LL | value;
| ^^^^^ private type
Expand All @@ -219,7 +230,7 @@ LL | priv_parent_substs::mac!();
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_parent_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:105:9
--> $DIR/associated-item-privacy-trait.rs:106:9
|
LL | Priv.method();
| ^^^^^^^^^^^^^ private type
Expand All @@ -230,7 +241,7 @@ LL | priv_parent_substs::mac!();
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_parent_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:108:9
--> $DIR/associated-item-privacy-trait.rs:109:9
|
LL | <Pub as PubTr>::CONST;
| ^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -241,7 +252,7 @@ LL | priv_parent_substs::mac!();
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_parent_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:110:9
--> $DIR/associated-item-privacy-trait.rs:111:9
|
LL | <Pub as PubTr<_>>::CONST;
| ^^^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -252,7 +263,7 @@ LL | priv_parent_substs::mac!();
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_parent_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:112:9
--> $DIR/associated-item-privacy-trait.rs:113:9
|
LL | <Priv as PubTr<_>>::CONST;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -263,7 +274,7 @@ LL | priv_parent_substs::mac!();
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_parent_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:116:30
--> $DIR/associated-item-privacy-trait.rs:117:30
|
LL | let _: <Pub as PubTr<_>>::AssocTy;
| ^ private type
Expand All @@ -274,7 +285,7 @@ LL | priv_parent_substs::mac!();
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_parent_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:118:17
--> $DIR/associated-item-privacy-trait.rs:119:17
|
LL | let _: <Priv as PubTr<_>>::AssocTy;
| ^^^^ private type
Expand All @@ -285,7 +296,7 @@ LL | priv_parent_substs::mac!();
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_parent_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:121:35
--> $DIR/associated-item-privacy-trait.rs:122:35
|
LL | pub type InSignatureTy1 = <Pub as PubTr>::AssocTy;
| ^^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -296,7 +307,7 @@ LL | priv_parent_substs::mac!();
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_parent_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:123:35
--> $DIR/associated-item-privacy-trait.rs:124:35
|
LL | pub type InSignatureTy2 = <Priv as PubTr<Pub>>::AssocTy;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -307,7 +318,7 @@ LL | priv_parent_substs::mac!();
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: type `priv_parent_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:125:14
--> $DIR/associated-item-privacy-trait.rs:126:14
|
LL | impl PubTr for u8 {}
| ^^^^^ private type
Expand All @@ -317,5 +328,5 @@ LL | priv_parent_substs::mac!();
|
= note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 29 previous errors
error: aborting due to 30 previous errors

2 changes: 2 additions & 0 deletions tests/ui/privacy/private-in-public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ mod aliases_pub {
// This should be OK, but associated type aliases are not substituted yet
pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
//~^ WARNING type `aliases_pub::Priv` is more private than the item `aliases_pub::f3`
//~| WARNING associated type `aliases_pub::PrivTr::Assoc` is more private than the item `aliases_pub::f3`

impl PrivUseAlias {
pub fn f(arg: Priv) {}
Expand Down Expand Up @@ -133,6 +134,7 @@ mod aliases_priv {
pub fn f2(arg: PrivAlias) {} //~ WARNING type `Priv2` is more private than the item `aliases_priv::f2`
pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
//~^ WARNING type `aliases_priv::Priv` is more private than the item `aliases_priv::f3`
//~| WARNING associated type `aliases_priv::PrivTr::Assoc` is more private than the item `aliases_priv::f3`
}

mod aliases_params {
Expand Down
46 changes: 35 additions & 11 deletions tests/ui/privacy/private-in-public.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,18 @@ note: but type `impls::Priv` is only usable at visibility `pub(self)`
LL | struct Priv;
| ^^^^^^^^^^^

warning: associated type `aliases_pub::PrivTr::Assoc` is more private than the item `aliases_pub::f3`
--> $DIR/private-in-public.rs:106:5
|
LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_pub::f3` is reachable at visibility `pub(crate)`
|
note: but associated type `aliases_pub::PrivTr::Assoc` is only usable at visibility `pub(self)`
--> $DIR/private-in-public.rs:101:9
|
LL | type Assoc = m::Pub3;
| ^^^^^^^^^^

warning: type `aliases_pub::Priv` is more private than the item `aliases_pub::f3`
--> $DIR/private-in-public.rs:106:5
|
Expand All @@ -289,64 +301,76 @@ LL | struct Priv;
| ^^^^^^^^^^^

warning: type `Priv1` is more private than the item `aliases_priv::f1`
--> $DIR/private-in-public.rs:132:5
--> $DIR/private-in-public.rs:133:5
|
LL | pub fn f1(arg: PrivUseAlias) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_priv::f1` is reachable at visibility `pub(crate)`
|
note: but type `Priv1` is only usable at visibility `pub(self)`
--> $DIR/private-in-public.rs:117:5
--> $DIR/private-in-public.rs:118:5
|
LL | struct Priv1;
| ^^^^^^^^^^^^

warning: type `Priv2` is more private than the item `aliases_priv::f2`
--> $DIR/private-in-public.rs:133:5
--> $DIR/private-in-public.rs:134:5
|
LL | pub fn f2(arg: PrivAlias) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_priv::f2` is reachable at visibility `pub(crate)`
|
note: but type `Priv2` is only usable at visibility `pub(self)`
--> $DIR/private-in-public.rs:118:5
--> $DIR/private-in-public.rs:119:5
|
LL | struct Priv2;
| ^^^^^^^^^^^^

warning: associated type `aliases_priv::PrivTr::Assoc` is more private than the item `aliases_priv::f3`
--> $DIR/private-in-public.rs:135:5
|
LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_priv::f3` is reachable at visibility `pub(crate)`
|
note: but associated type `aliases_priv::PrivTr::Assoc` is only usable at visibility `pub(self)`
--> $DIR/private-in-public.rs:129:9
|
LL | type Assoc = Priv3;
| ^^^^^^^^^^

warning: type `aliases_priv::Priv` is more private than the item `aliases_priv::f3`
--> $DIR/private-in-public.rs:134:5
--> $DIR/private-in-public.rs:135:5
|
LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_priv::f3` is reachable at visibility `pub(crate)`
|
note: but type `aliases_priv::Priv` is only usable at visibility `pub(self)`
--> $DIR/private-in-public.rs:115:5
--> $DIR/private-in-public.rs:116:5
|
LL | struct Priv;
| ^^^^^^^^^^^

warning: type `aliases_params::Priv` is more private than the item `aliases_params::f2`
--> $DIR/private-in-public.rs:143:5
--> $DIR/private-in-public.rs:145:5
|
LL | pub fn f2(arg: PrivAliasGeneric) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_params::f2` is reachable at visibility `pub(crate)`
|
note: but type `aliases_params::Priv` is only usable at visibility `pub(self)`
--> $DIR/private-in-public.rs:139:5
--> $DIR/private-in-public.rs:141:5
|
LL | struct Priv;
| ^^^^^^^^^^^

warning: type `aliases_params::Priv` is more private than the item `aliases_params::f3`
--> $DIR/private-in-public.rs:145:5
--> $DIR/private-in-public.rs:147:5
|
LL | pub fn f3(arg: Result<u8>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_params::f3` is reachable at visibility `pub(crate)`
|
note: but type `aliases_params::Priv` is only usable at visibility `pub(self)`
--> $DIR/private-in-public.rs:139:5
--> $DIR/private-in-public.rs:141:5
|
LL | struct Priv;
| ^^^^^^^^^^^

warning: 29 warnings emitted
warning: 31 warnings emitted

0 comments on commit f6919fa

Please sign in to comment.