From 5c9a74d88b6e585e49b62b0a8f45fe95abc70f7e Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 12 Jul 2023 10:18:30 +0000 Subject: [PATCH] Merge associated types with the other alias types --- compiler/rustc_privacy/src/lib.rs | 25 ++++++------------- .../privacy/associated-item-privacy-trait.rs | 2 +- .../associated-item-privacy-trait.stderr | 4 +-- tests/ui/privacy/private-in-public.rs | 8 +++--- tests/ui/privacy/private-in-public.stderr | 20 +++++++-------- 5 files changed, 24 insertions(+), 35 deletions(-) diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index 1fb54df60f7..4bb7e65747f 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -210,19 +210,7 @@ where } } } - ty::Alias(ty::Projection, proj) => { - if V::SKIP_ASSOC_TYS { - // Visitors searching for minimal visibility/reachability want to - // conservatively approximate associated types like `::Alias` - // as visible/reachable even if both `Type` and `Trait` are private. - // Ideally, associated types should be substituted in the same way as - // free type aliases, but this isn't done yet. - return ControlFlow::Continue(()); - } - // This will also visit args if necessary, so we don't need to recurse. - return self.visit_projection_ty(proj); - } - ty::Alias(kind @ (ty::Inherent | ty::Weak), data) => { + ty::Alias(kind @ (ty::Inherent | ty::Weak | ty::Projection), data) => { if V::SKIP_ASSOC_TYS { // Visitors searching for minimal visibility/reachability want to // conservatively approximate associated types like `Type::Alias` @@ -232,13 +220,14 @@ where return ControlFlow::Continue(()); } + let kind = match kind { + ty::Inherent | ty::Projection => "associated type", + ty::Weak => "type alias", + ty::Opaque => unreachable!(), + }; self.def_id_visitor.visit_def_id( data.def_id, - match kind { - ty::Inherent => "associated type", - ty::Weak => "type alias", - _ => unreachable!(), - }, + kind, &LazyDefPathStr { def_id: data.def_id, tcx }, )?; diff --git a/tests/ui/privacy/associated-item-privacy-trait.rs b/tests/ui/privacy/associated-item-privacy-trait.rs index db77a6a7258..f038ae9e261 100644 --- a/tests/ui/privacy/associated-item-privacy-trait.rs +++ b/tests/ui/privacy/associated-item-privacy-trait.rs @@ -23,7 +23,7 @@ mod priv_trait { let _: ::AssocTy; //~^ ERROR associated type `PrivTr::AssocTy` is private pub type InSignatureTy = ::AssocTy; - //~^ ERROR trait `PrivTr` is private + //~^ ERROR associated type `PrivTr::AssocTy` is private pub trait InSignatureTr: PrivTr {} //~^ ERROR trait `PrivTr` is private impl PrivTr for u8 {} diff --git a/tests/ui/privacy/associated-item-privacy-trait.stderr b/tests/ui/privacy/associated-item-privacy-trait.stderr index eb905bf7ef8..4e9dfa4a835 100644 --- a/tests/ui/privacy/associated-item-privacy-trait.stderr +++ b/tests/ui/privacy/associated-item-privacy-trait.stderr @@ -53,11 +53,11 @@ 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 +error: associated type `PrivTr::AssocTy` is private --> $DIR/associated-item-privacy-trait.rs:25:34 | LL | pub type InSignatureTy = ::AssocTy; - | ^^^^^^^^^^^^^^^^^^^^^^^^ private trait + | ^^^^^^^^^^^^^^^^^^^^^^^^ private associated type ... LL | priv_trait::mac!(); | ------------------ in this macro invocation diff --git a/tests/ui/privacy/private-in-public.rs b/tests/ui/privacy/private-in-public.rs index f54f9e38faa..3fff2d51710 100644 --- a/tests/ui/privacy/private-in-public.rs +++ b/tests/ui/privacy/private-in-public.rs @@ -104,8 +104,8 @@ mod aliases_pub { // This should be OK, but associated type aliases are not substituted yet pub fn f3(arg: ::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` + //~| WARNING associated type `aliases_pub::PrivTr::Assoc` is more private than the item `aliases_pub::f3` impl PrivUseAlias { pub fn f(arg: Priv) {} @@ -133,8 +133,8 @@ 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: ::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` + //~| WARNING associated type `aliases_priv::PrivTr::Assoc` is more private than the item `aliases_priv::f3` } mod aliases_params { diff --git a/tests/ui/privacy/private-in-public.stderr b/tests/ui/privacy/private-in-public.stderr index d3f7f0f637f..49cc2e19bf0 100644 --- a/tests/ui/privacy/private-in-public.stderr +++ b/tests/ui/privacy/private-in-public.stderr @@ -276,17 +276,17 @@ note: but type `impls::Priv` is only usable at visibility `pub(self)` LL | struct Priv; | ^^^^^^^^^^^ -warning: trait `aliases_pub::PrivTr` 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` --> $DIR/private-in-public.rs:106:5 | LL | pub fn f3(arg: ::Assoc) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_pub::f3` is reachable at visibility `pub(crate)` | -note: but trait `aliases_pub::PrivTr` is only usable at visibility `pub(self)` - --> $DIR/private-in-public.rs:100:5 +note: but associated type `aliases_pub::PrivTr::Assoc` is only usable at visibility `pub(self)` + --> $DIR/private-in-public.rs:101:9 | -LL | trait PrivTr { - | ^^^^^^^^^^^^ +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 @@ -324,17 +324,17 @@ note: but type `Priv2` is only usable at visibility `pub(self)` LL | struct Priv2; | ^^^^^^^^^^^^ -warning: trait `aliases_priv::PrivTr` 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` --> $DIR/private-in-public.rs:135:5 | LL | pub fn f3(arg: ::Assoc) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `aliases_priv::f3` is reachable at visibility `pub(crate)` | -note: but trait `aliases_priv::PrivTr` is only usable at visibility `pub(self)` - --> $DIR/private-in-public.rs:128:5 +note: but associated type `aliases_priv::PrivTr::Assoc` is only usable at visibility `pub(self)` + --> $DIR/private-in-public.rs:129:9 | -LL | trait PrivTr { - | ^^^^^^^^^^^^ +LL | type Assoc = Priv3; + | ^^^^^^^^^^ warning: type `aliases_priv::Priv` is more private than the item `aliases_priv::f3` --> $DIR/private-in-public.rs:135:5