Skip to content

Commit 34eb73c

Browse files
committed
privacy: Rename "accessibility levels" to "effective visibilities"
And a couple of other naming tweaks Related to #48054
1 parent 629a414 commit 34eb73c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+524
-516
lines changed

Diff for: compiler/rustc_interface/src/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
927927
sess.time("misc_checking_3", || {
928928
parallel!(
929929
{
930-
tcx.ensure().privacy_access_levels(());
930+
tcx.ensure().effective_visibilities(());
931931

932932
parallel!(
933933
{

Diff for: compiler/rustc_lint/src/builtin.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ impl MissingDoc {
563563
// It's an option so the crate root can also use this function (it doesn't
564564
// have a `NodeId`).
565565
if def_id != CRATE_DEF_ID {
566-
if !cx.access_levels.is_exported(def_id) {
566+
if !cx.effective_visibilities.is_exported(def_id) {
567567
return;
568568
}
569569
}
@@ -721,7 +721,7 @@ declare_lint_pass!(MissingCopyImplementations => [MISSING_COPY_IMPLEMENTATIONS])
721721

722722
impl<'tcx> LateLintPass<'tcx> for MissingCopyImplementations {
723723
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
724-
if !cx.access_levels.is_reachable(item.def_id.def_id) {
724+
if !cx.effective_visibilities.is_reachable(item.def_id.def_id) {
725725
return;
726726
}
727727
let (def, ty) = match item.kind {
@@ -814,7 +814,7 @@ impl_lint_pass!(MissingDebugImplementations => [MISSING_DEBUG_IMPLEMENTATIONS]);
814814

815815
impl<'tcx> LateLintPass<'tcx> for MissingDebugImplementations {
816816
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
817-
if !cx.access_levels.is_reachable(item.def_id.def_id) {
817+
if !cx.effective_visibilities.is_reachable(item.def_id.def_id) {
818818
return;
819819
}
820820

@@ -1385,7 +1385,8 @@ impl UnreachablePub {
13851385
exportable: bool,
13861386
) {
13871387
let mut applicability = Applicability::MachineApplicable;
1388-
if cx.tcx.visibility(def_id).is_public() && !cx.access_levels.is_reachable(def_id) {
1388+
if cx.tcx.visibility(def_id).is_public() && !cx.effective_visibilities.is_reachable(def_id)
1389+
{
13891390
if vis_span.from_expansion() {
13901391
applicability = Applicability::MaybeIncorrect;
13911392
}

Diff for: compiler/rustc_lint/src/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_hir as hir;
3131
use rustc_hir::def::Res;
3232
use rustc_hir::def_id::{CrateNum, DefId};
3333
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
34-
use rustc_middle::middle::privacy::AccessLevels;
34+
use rustc_middle::middle::privacy::EffectiveVisibilities;
3535
use rustc_middle::middle::stability;
3636
use rustc_middle::ty::layout::{LayoutError, LayoutOfHelpers, TyAndLayout};
3737
use rustc_middle::ty::print::with_no_trimmed_paths;
@@ -542,7 +542,7 @@ pub struct LateContext<'tcx> {
542542
pub param_env: ty::ParamEnv<'tcx>,
543543

544544
/// Items accessible from the crate being checked.
545-
pub access_levels: &'tcx AccessLevels,
545+
pub effective_visibilities: &'tcx EffectiveVisibilities,
546546

547547
/// The store of registered lints and the lint levels.
548548
pub lint_store: &'tcx LintStore,

Diff for: compiler/rustc_lint/src/late.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,14 @@ fn late_lint_mod_pass<'tcx, T: LateLintPass<'tcx>>(
338338
module_def_id: LocalDefId,
339339
pass: T,
340340
) {
341-
let access_levels = &tcx.privacy_access_levels(());
341+
let effective_visibilities = &tcx.effective_visibilities(());
342342

343343
let context = LateContext {
344344
tcx,
345345
enclosing_body: None,
346346
cached_typeck_results: Cell::new(None),
347347
param_env: ty::ParamEnv::empty(),
348-
access_levels,
348+
effective_visibilities,
349349
lint_store: unerased_lint_store(tcx),
350350
last_node_with_lint_attrs: tcx.hir().local_def_id_to_hir_id(module_def_id),
351351
generics: None,
@@ -386,14 +386,14 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx>>(
386386
}
387387

388388
fn late_lint_pass_crate<'tcx, T: LateLintPass<'tcx>>(tcx: TyCtxt<'tcx>, pass: T) {
389-
let access_levels = &tcx.privacy_access_levels(());
389+
let effective_visibilities = &tcx.effective_visibilities(());
390390

391391
let context = LateContext {
392392
tcx,
393393
enclosing_body: None,
394394
cached_typeck_results: Cell::new(None),
395395
param_env: ty::ParamEnv::empty(),
396-
access_levels,
396+
effective_visibilities,
397397
lint_store: unerased_lint_store(tcx),
398398
last_node_with_lint_attrs: hir::CRATE_HIR_ID,
399399
generics: None,

Diff for: compiler/rustc_lint/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ macro_rules! late_lint_mod_passes {
212212
TypeLimits: TypeLimits::new(),
213213
NonSnakeCase: NonSnakeCase,
214214
InvalidNoMangleItems: InvalidNoMangleItems,
215-
// Depends on access levels
215+
// Depends on effective visibilities
216216
UnreachablePub: UnreachablePub,
217217
ExplicitOutlivesRequirements: ExplicitOutlivesRequirements,
218218
InvalidValue: InvalidValue,

Diff for: compiler/rustc_metadata/src/rmeta/encoder.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -787,9 +787,8 @@ fn should_encode_attr(
787787
} else if attr.doc_str().is_some() {
788788
// We keep all public doc comments because they might be "imported" into downstream crates
789789
// if they use `#[doc(inline)]` to copy an item's documentation into their own.
790-
*is_def_id_public.get_or_insert_with(|| {
791-
tcx.privacy_access_levels(()).get_effective_vis(def_id).is_some()
792-
})
790+
*is_def_id_public
791+
.get_or_insert_with(|| tcx.effective_visibilities(()).effective_vis(def_id).is_some())
793792
} else if attr.has_name(sym::doc) {
794793
// If this is a `doc` attribute, and it's marked `inline` (as in `#[doc(inline)]`), we can
795794
// remove it. It won't be inlinable in downstream crates.

Diff for: compiler/rustc_middle/src/arena.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ macro_rules! arena_types {
7777
rustc_middle::infer::canonical::QueryResponse<'tcx, rustc_middle::ty::Ty<'tcx>>
7878
>,
7979
[] all_traits: Vec<rustc_hir::def_id::DefId>,
80-
[] privacy_access_levels: rustc_middle::middle::privacy::AccessLevels,
80+
[] effective_visibilities: rustc_middle::middle::privacy::EffectiveVisibilities,
8181
[] foreign_module: rustc_session::cstore::ForeignModule,
8282
[] foreign_modules: Vec<rustc_session::cstore::ForeignModule>,
8383
[] upvars_mentioned: rustc_data_structures::fx::FxIndexMap<rustc_hir::HirId, rustc_hir::Upvar>,

Diff for: compiler/rustc_middle/src/middle/privacy.rs

+79-80
Original file line numberDiff line numberDiff line change
@@ -9,106 +9,101 @@ use rustc_query_system::ich::StableHashingContext;
99
use rustc_span::def_id::{DefId, LocalDefId};
1010
use std::hash::Hash;
1111

12-
/// Represents the levels of accessibility an item can have.
12+
/// Represents the levels of effective visibility an item can have.
1313
///
14-
/// The variants are sorted in ascending order of accessibility.
14+
/// The variants are sorted in ascending order of directness.
1515
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, HashStable)]
16-
pub enum AccessLevel {
17-
/// Superset of `AccessLevel::Reachable` used to mark impl Trait items.
18-
ReachableFromImplTrait,
19-
/// Exported items + items participating in various kinds of public interfaces,
20-
/// but not directly nameable. For example, if function `fn f() -> T {...}` is
21-
/// public, then type `T` is reachable. Its values can be obtained by other crates
22-
/// even if the type itself is not nameable.
16+
pub enum Level {
17+
/// Superset of `Reachable` including items leaked through return position `impl Trait`.
18+
ReachableThroughImplTrait,
19+
/// Item is either reexported, or leaked through any kind of interface.
20+
/// For example, if function `fn f() -> T {...}` is directly public, then type `T` is publicly
21+
/// reachable and its values can be obtained by other crates even if the type itself is not
22+
/// nameable.
2323
Reachable,
24-
/// Public items + items accessible to other crates with the help of `pub use` re-exports.
25-
Exported,
26-
/// Items accessible to other crates directly, without the help of re-exports.
27-
Public,
24+
/// Item is accessible either directly, or with help of `use` reexports.
25+
Reexported,
26+
/// Item is directly accessible, without help of reexports.
27+
Direct,
2828
}
2929

30-
impl AccessLevel {
31-
pub fn all_levels() -> [AccessLevel; 4] {
32-
[
33-
AccessLevel::Public,
34-
AccessLevel::Exported,
35-
AccessLevel::Reachable,
36-
AccessLevel::ReachableFromImplTrait,
37-
]
30+
impl Level {
31+
pub fn all_levels() -> [Level; 4] {
32+
[Level::Direct, Level::Reexported, Level::Reachable, Level::ReachableThroughImplTrait]
3833
}
3934
}
4035

4136
#[derive(Clone, Copy, PartialEq, Eq, Debug, HashStable)]
4237
pub struct EffectiveVisibility {
43-
public: Visibility,
44-
exported: Visibility,
38+
direct: Visibility,
39+
reexported: Visibility,
4540
reachable: Visibility,
46-
reachable_from_impl_trait: Visibility,
41+
reachable_through_impl_trait: Visibility,
4742
}
4843

4944
impl EffectiveVisibility {
50-
pub fn get(&self, tag: AccessLevel) -> &Visibility {
51-
match tag {
52-
AccessLevel::Public => &self.public,
53-
AccessLevel::Exported => &self.exported,
54-
AccessLevel::Reachable => &self.reachable,
55-
AccessLevel::ReachableFromImplTrait => &self.reachable_from_impl_trait,
45+
pub fn at_level(&self, level: Level) -> &Visibility {
46+
match level {
47+
Level::Direct => &self.direct,
48+
Level::Reexported => &self.reexported,
49+
Level::Reachable => &self.reachable,
50+
Level::ReachableThroughImplTrait => &self.reachable_through_impl_trait,
5651
}
5752
}
5853

59-
fn get_mut(&mut self, tag: AccessLevel) -> &mut Visibility {
60-
match tag {
61-
AccessLevel::Public => &mut self.public,
62-
AccessLevel::Exported => &mut self.exported,
63-
AccessLevel::Reachable => &mut self.reachable,
64-
AccessLevel::ReachableFromImplTrait => &mut self.reachable_from_impl_trait,
54+
fn at_level_mut(&mut self, level: Level) -> &mut Visibility {
55+
match level {
56+
Level::Direct => &mut self.direct,
57+
Level::Reexported => &mut self.reexported,
58+
Level::Reachable => &mut self.reachable,
59+
Level::ReachableThroughImplTrait => &mut self.reachable_through_impl_trait,
6560
}
6661
}
6762

68-
pub fn is_public_at_level(&self, tag: AccessLevel) -> bool {
69-
self.get(tag).is_public()
63+
pub fn is_public_at_level(&self, level: Level) -> bool {
64+
self.at_level(level).is_public()
7065
}
7166

7267
pub fn from_vis(vis: Visibility) -> EffectiveVisibility {
7368
EffectiveVisibility {
74-
public: vis,
75-
exported: vis,
69+
direct: vis,
70+
reexported: vis,
7671
reachable: vis,
77-
reachable_from_impl_trait: vis,
72+
reachable_through_impl_trait: vis,
7873
}
7974
}
8075
}
8176

82-
/// Holds a map of accessibility levels for reachable HIR nodes.
77+
/// Holds a map of effective visibilities for reachable HIR nodes.
8378
#[derive(Debug, Clone)]
84-
pub struct AccessLevels<Id = LocalDefId> {
79+
pub struct EffectiveVisibilities<Id = LocalDefId> {
8580
map: FxHashMap<Id, EffectiveVisibility>,
8681
}
8782

88-
impl<Id: Hash + Eq + Copy> AccessLevels<Id> {
89-
pub fn is_public_at_level(&self, id: Id, tag: AccessLevel) -> bool {
90-
self.get_effective_vis(id)
91-
.map_or(false, |effective_vis| effective_vis.is_public_at_level(tag))
83+
impl<Id: Hash + Eq + Copy> EffectiveVisibilities<Id> {
84+
pub fn is_public_at_level(&self, id: Id, level: Level) -> bool {
85+
self.effective_vis(id)
86+
.map_or(false, |effective_vis| effective_vis.is_public_at_level(level))
9287
}
9388

94-
/// See `AccessLevel::Reachable`.
89+
/// See `Level::Reachable`.
9590
pub fn is_reachable(&self, id: Id) -> bool {
96-
self.is_public_at_level(id, AccessLevel::Reachable)
91+
self.is_public_at_level(id, Level::Reachable)
9792
}
9893

99-
/// See `AccessLevel::Exported`.
94+
/// See `Level::Reexported`.
10095
pub fn is_exported(&self, id: Id) -> bool {
101-
self.is_public_at_level(id, AccessLevel::Exported)
96+
self.is_public_at_level(id, Level::Reexported)
10297
}
10398

104-
/// See `AccessLevel::Public`.
105-
pub fn is_public(&self, id: Id) -> bool {
106-
self.is_public_at_level(id, AccessLevel::Public)
99+
/// See `Level::Direct`.
100+
pub fn is_directly_public(&self, id: Id) -> bool {
101+
self.is_public_at_level(id, Level::Direct)
107102
}
108103

109-
pub fn get_access_level(&self, id: Id) -> Option<AccessLevel> {
110-
self.get_effective_vis(id).and_then(|effective_vis| {
111-
for level in AccessLevel::all_levels() {
104+
pub fn public_at_level(&self, id: Id) -> Option<Level> {
105+
self.effective_vis(id).and_then(|effective_vis| {
106+
for level in Level::all_levels() {
112107
if effective_vis.is_public_at_level(level) {
113108
return Some(level);
114109
}
@@ -117,38 +112,41 @@ impl<Id: Hash + Eq + Copy> AccessLevels<Id> {
117112
})
118113
}
119114

120-
pub fn get_effective_vis(&self, id: Id) -> Option<&EffectiveVisibility> {
115+
pub fn effective_vis(&self, id: Id) -> Option<&EffectiveVisibility> {
121116
self.map.get(&id)
122117
}
123118

124119
pub fn iter(&self) -> impl Iterator<Item = (&Id, &EffectiveVisibility)> {
125120
self.map.iter()
126121
}
127122

128-
pub fn map_id<OutId: Hash + Eq + Copy>(&self, f: impl Fn(Id) -> OutId) -> AccessLevels<OutId> {
129-
AccessLevels { map: self.map.iter().map(|(k, v)| (f(*k), *v)).collect() }
123+
pub fn map_id<OutId: Hash + Eq + Copy>(
124+
&self,
125+
f: impl Fn(Id) -> OutId,
126+
) -> EffectiveVisibilities<OutId> {
127+
EffectiveVisibilities { map: self.map.iter().map(|(k, v)| (f(*k), *v)).collect() }
130128
}
131129

132-
pub fn set_access_level(
130+
pub fn set_public_at_level(
133131
&mut self,
134132
id: Id,
135133
default_vis: impl FnOnce() -> Visibility,
136-
tag: AccessLevel,
134+
level: Level,
137135
) {
138136
let mut effective_vis = self
139-
.get_effective_vis(id)
137+
.effective_vis(id)
140138
.copied()
141139
.unwrap_or_else(|| EffectiveVisibility::from_vis(default_vis()));
142-
for level in AccessLevel::all_levels() {
143-
if level <= tag {
144-
*effective_vis.get_mut(level) = Visibility::Public;
140+
for l in Level::all_levels() {
141+
if l <= level {
142+
*effective_vis.at_level_mut(l) = Visibility::Public;
145143
}
146144
}
147145
self.map.insert(id, effective_vis);
148146
}
149147
}
150148

151-
impl<Id: Hash + Eq + Copy + Into<DefId>> AccessLevels<Id> {
149+
impl<Id: Hash + Eq + Copy + Into<DefId>> EffectiveVisibilities<Id> {
152150
// `parent_id` is not necessarily a parent in source code tree,
153151
// it is the node from which the maximum effective visibility is inherited.
154152
pub fn update(
@@ -157,28 +155,29 @@ impl<Id: Hash + Eq + Copy + Into<DefId>> AccessLevels<Id> {
157155
nominal_vis: Visibility,
158156
default_vis: impl FnOnce() -> Visibility,
159157
parent_id: Id,
160-
tag: AccessLevel,
158+
level: Level,
161159
tree: impl DefIdTree,
162160
) -> bool {
163161
let mut changed = false;
164-
let mut current_effective_vis = self.get_effective_vis(id).copied().unwrap_or_else(|| {
162+
let mut current_effective_vis = self.effective_vis(id).copied().unwrap_or_else(|| {
165163
if id.into().is_crate_root() {
166164
EffectiveVisibility::from_vis(Visibility::Public)
167165
} else {
168166
EffectiveVisibility::from_vis(default_vis())
169167
}
170168
});
171-
if let Some(inherited_effective_vis) = self.get_effective_vis(parent_id) {
172-
let mut inherited_effective_vis_at_prev_level = *inherited_effective_vis.get(tag);
169+
if let Some(inherited_effective_vis) = self.effective_vis(parent_id) {
170+
let mut inherited_effective_vis_at_prev_level =
171+
*inherited_effective_vis.at_level(level);
173172
let mut calculated_effective_vis = inherited_effective_vis_at_prev_level;
174-
for level in AccessLevel::all_levels() {
175-
if tag >= level {
176-
let inherited_effective_vis_at_level = *inherited_effective_vis.get(level);
177-
let current_effective_vis_at_level = current_effective_vis.get_mut(level);
173+
for l in Level::all_levels() {
174+
if level >= l {
175+
let inherited_effective_vis_at_level = *inherited_effective_vis.at_level(l);
176+
let current_effective_vis_at_level = current_effective_vis.at_level_mut(l);
178177
// effective visibility for id shouldn't be recalculated if
179178
// inherited from parent_id effective visibility isn't changed at next level
180179
if !(inherited_effective_vis_at_prev_level == inherited_effective_vis_at_level
181-
&& tag != level)
180+
&& level != l)
182181
{
183182
calculated_effective_vis =
184183
if nominal_vis.is_at_least(inherited_effective_vis_at_level, tree) {
@@ -205,15 +204,15 @@ impl<Id: Hash + Eq + Copy + Into<DefId>> AccessLevels<Id> {
205204
}
206205
}
207206

208-
impl<Id> Default for AccessLevels<Id> {
207+
impl<Id> Default for EffectiveVisibilities<Id> {
209208
fn default() -> Self {
210-
AccessLevels { map: Default::default() }
209+
EffectiveVisibilities { map: Default::default() }
211210
}
212211
}
213212

214-
impl<'a> HashStable<StableHashingContext<'a>> for AccessLevels {
213+
impl<'a> HashStable<StableHashingContext<'a>> for EffectiveVisibilities {
215214
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
216-
let AccessLevels { ref map } = *self;
215+
let EffectiveVisibilities { ref map } = *self;
217216
map.hash_stable(hcx, hasher);
218217
}
219218
}

0 commit comments

Comments
 (0)