Skip to content

Commit

Permalink
Demonstrate why we need to handle the trait of a projection
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Sep 5, 2023
1 parent c2bcced commit 8ce14f0
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 113 deletions.
18 changes: 12 additions & 6 deletions compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,21 @@ where
return ControlFlow::Continue(());
}

let (def_id, kind) = match kind {
ty::Inherent => (data.def_id, "associated type"),
ty::Weak => (data.def_id, "type alias"),
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 => (tcx.parent(data.def_id), "trait"),
ty::Projection => None,
_ => unreachable!(),
};
self.def_id_visitor.visit_def_id(def_id, kind, &LazyDefPathStr { def_id, tcx })?;
if let Some(kind) = kind {
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 Expand Up @@ -1364,7 +1370,7 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> {
match item.kind {
hir::ItemKind::Macro(_, _) => intravisit::walk_item(self, item),
hir::ItemKind::Static(_, _, body_id)
| hir::ItemKind::Const(_, body_id)
| hir::ItemKind::Const(_, _, body_id)
| hir::ItemKind::Fn(_, _, body_id) => self.visit_nested_body(body_id),
hir::ItemKind::Impl(hir::Impl { of_trait: Some(tr), .. }) => {
self.span = tr.path.span;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

fn ice() -> impl AsRef<Fn(&())> {
//~^ WARN trait objects without an explicit `dyn` are deprecated
//~| WARN trait objects without an explicit `dyn` are deprecated
//~| WARN trait objects without an explicit `dyn` are deprecated
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
Foo
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,5 @@ help: use `dyn`
LL | fn ice() -> impl AsRef<dyn Fn(&())> {
| +++

warning: trait objects without an explicit `dyn` are deprecated
--> $DIR/fresh-lifetime-from-bare-trait-obj-114664.rs:5:24
|
LL | fn ice() -> impl AsRef<Fn(&())> {
| ^^^^^^^
|
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
|
LL | fn ice() -> impl AsRef<dyn Fn(&())> {
| +++

warning: trait objects without an explicit `dyn` are deprecated
--> $DIR/fresh-lifetime-from-bare-trait-obj-114664.rs:5:24
|
LL | fn ice() -> impl AsRef<Fn(&())> {
| ^^^^^^^
|
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
|
LL | fn ice() -> impl AsRef<dyn Fn(&())> {
| +++

warning: 3 warnings emitted
warning: 1 warning emitted

1 change: 0 additions & 1 deletion tests/ui/privacy/associated-item-privacy-trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ mod priv_trait {
let _: <Pub as PrivTr>::AssocTy;
//~^ ERROR associated type `PrivTr::AssocTy` is private
pub type InSignatureTy = <Pub as PrivTr>::AssocTy;
//~^ ERROR trait `PrivTr` is private
pub trait InSignatureTr: PrivTr {}
//~^ ERROR trait `PrivTr` is private
impl PrivTr for u8 {}
Expand Down
61 changes: 25 additions & 36 deletions tests/ui/privacy/associated-item-privacy-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,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:25:34
|
LL | pub type InSignatureTy = <Pub as PrivTr>::AssocTy;
| ^^^^^^^^^^^^^^^^^^^^^^^^ private trait
...
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:27:34
--> $DIR/associated-item-privacy-trait.rs:26:34
|
LL | pub trait InSignatureTr: PrivTr {}
| ^^^^^^ private trait
Expand All @@ -76,7 +65,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:29:14
--> $DIR/associated-item-privacy-trait.rs:28:14
|
LL | impl PrivTr for u8 {}
| ^^^^^^ private trait
Expand All @@ -87,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: type `priv_signature::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:46:21
--> $DIR/associated-item-privacy-trait.rs:45:21
|
LL | let value = <Pub as PubTr>::method;
| ^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -98,7 +87,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:48:9
--> $DIR/associated-item-privacy-trait.rs:47:9
|
LL | value;
| ^^^^^ private type
Expand All @@ -109,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:50:13
--> $DIR/associated-item-privacy-trait.rs:49:13
|
LL | Pub.method(loop {});
| ^^^^^^ private type
Expand All @@ -120,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_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:67:21
--> $DIR/associated-item-privacy-trait.rs:66:21
|
LL | let value = <Pub as PubTr>::method::<Priv>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -131,7 +120,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:69:9
--> $DIR/associated-item-privacy-trait.rs:68:9
|
LL | value;
| ^^^^^ private type
Expand All @@ -142,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:71:9
--> $DIR/associated-item-privacy-trait.rs:70:9
|
LL | Pub.method::<Priv>();
| ^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -153,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_parent_substs::Priv` is private
--> $DIR/associated-item-privacy-trait.rs:91:21
--> $DIR/associated-item-privacy-trait.rs:90:21
|
LL | let value = <Pub as PubTr>::method;
| ^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -164,7 +153,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:93:9
--> $DIR/associated-item-privacy-trait.rs:92:9
|
LL | value;
| ^^^^^ private type
Expand All @@ -175,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:95:21
--> $DIR/associated-item-privacy-trait.rs:94:21
|
LL | let value = <Pub as PubTr<_>>::method;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -186,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:97:9
--> $DIR/associated-item-privacy-trait.rs:96:9
|
LL | value;
| ^^^^^ private type
Expand All @@ -197,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:99:9
--> $DIR/associated-item-privacy-trait.rs:98:9
|
LL | Pub.method();
| ^^^^^^^^^^^^ private type
Expand All @@ -208,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:102:21
--> $DIR/associated-item-privacy-trait.rs:101:21
|
LL | let value = <Priv as PubTr<_>>::method;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -219,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:104:9
--> $DIR/associated-item-privacy-trait.rs:103:9
|
LL | value;
| ^^^^^ private type
Expand All @@ -230,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:106:9
--> $DIR/associated-item-privacy-trait.rs:105:9
|
LL | Priv.method();
| ^^^^^^^^^^^^^ private type
Expand All @@ -241,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:109:9
--> $DIR/associated-item-privacy-trait.rs:108:9
|
LL | <Pub as PubTr>::CONST;
| ^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -252,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:111:9
--> $DIR/associated-item-privacy-trait.rs:110:9
|
LL | <Pub as PubTr<_>>::CONST;
| ^^^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -263,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:113:9
--> $DIR/associated-item-privacy-trait.rs:112:9
|
LL | <Priv as PubTr<_>>::CONST;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -274,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:117:30
--> $DIR/associated-item-privacy-trait.rs:116:30
|
LL | let _: <Pub as PubTr<_>>::AssocTy;
| ^ private type
Expand All @@ -285,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:119:17
--> $DIR/associated-item-privacy-trait.rs:118:17
|
LL | let _: <Priv as PubTr<_>>::AssocTy;
| ^^^^ private type
Expand All @@ -296,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:122:35
--> $DIR/associated-item-privacy-trait.rs:121:35
|
LL | pub type InSignatureTy1 = <Pub as PubTr>::AssocTy;
| ^^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -307,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:124:35
--> $DIR/associated-item-privacy-trait.rs:123:35
|
LL | pub type InSignatureTy2 = <Priv as PubTr<Pub>>::AssocTy;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private type
Expand All @@ -318,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:126:14
--> $DIR/associated-item-privacy-trait.rs:125:14
|
LL | impl PubTr for u8 {}
| ^^^^^ private type
Expand All @@ -328,5 +317,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 30 previous errors
error: aborting due to 29 previous errors

6 changes: 2 additions & 4 deletions tests/ui/privacy/private-in-public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,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 trait `aliases_pub::PrivTr` is more private than the item `aliases_pub::f3`
//~| WARNING type `aliases_pub::Priv` is more private than the item `aliases_pub::f3`
//~^ WARNING type `aliases_pub::Priv` is more private than the item `aliases_pub::f3`

impl PrivUseAlias {
pub fn f(arg: Priv) {}
Expand Down Expand Up @@ -133,8 +132,7 @@ mod aliases_priv {
pub fn f1(arg: PrivUseAlias) {} //~ WARNING type `Priv1` is more private than the item `aliases_priv::f1`
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 trait `aliases_priv::PrivTr` is more private than the item `aliases_priv::f3`
//~| WARNING type `aliases_priv::Priv` is more private than the item `aliases_priv::f3`
//~^ WARNING type `aliases_priv::Priv` is more private than the item `aliases_priv::f3`
}

mod aliases_params {
Expand Down
Loading

0 comments on commit 8ce14f0

Please sign in to comment.