privacy: Rename "accessibility levels" to "effective visibilities"
And a couple of other naming tweaks Related to https://github.com/rust-lang/rust/issues/48054
This commit is contained in:
parent
629a414d7b
commit
34eb73c72d
53 changed files with 524 additions and 516 deletions
|
@ -6,55 +6,54 @@ use rustc_ast::Crate;
|
|||
use rustc_ast::EnumDef;
|
||||
use rustc_hir::def_id::LocalDefId;
|
||||
use rustc_hir::def_id::CRATE_DEF_ID;
|
||||
use rustc_middle::middle::privacy::AccessLevel;
|
||||
use rustc_middle::middle::privacy::Level;
|
||||
use rustc_middle::ty::{DefIdTree, Visibility};
|
||||
|
||||
pub struct AccessLevelsVisitor<'r, 'a> {
|
||||
pub struct EffectiveVisibilitiesVisitor<'r, 'a> {
|
||||
r: &'r mut Resolver<'a>,
|
||||
changed: bool,
|
||||
}
|
||||
|
||||
impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
|
||||
/// Fills the `Resolver::access_levels` table with public & exported items
|
||||
impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> {
|
||||
/// Fills the `Resolver::effective_visibilities` table with public & exported items
|
||||
/// For now, this doesn't resolve macros (FIXME) and cannot resolve Impl, as we
|
||||
/// need access to a TyCtxt for that.
|
||||
pub fn compute_access_levels<'c>(r: &'r mut Resolver<'a>, krate: &'c Crate) {
|
||||
let mut visitor = AccessLevelsVisitor { r, changed: false };
|
||||
pub fn compute_effective_visibilities<'c>(r: &'r mut Resolver<'a>, krate: &'c Crate) {
|
||||
let mut visitor = EffectiveVisibilitiesVisitor { r, changed: false };
|
||||
|
||||
visitor.update(CRATE_DEF_ID, Visibility::Public, CRATE_DEF_ID, AccessLevel::Public);
|
||||
visitor.set_bindings_access_level(CRATE_DEF_ID);
|
||||
visitor.update(CRATE_DEF_ID, Visibility::Public, CRATE_DEF_ID, Level::Direct);
|
||||
visitor.set_bindings_effective_visibilities(CRATE_DEF_ID);
|
||||
|
||||
while visitor.changed {
|
||||
visitor.reset();
|
||||
visit::walk_crate(&mut visitor, krate);
|
||||
}
|
||||
|
||||
info!("resolve::access_levels: {:#?}", r.access_levels);
|
||||
info!("resolve::effective_visibilities: {:#?}", r.effective_visibilities);
|
||||
}
|
||||
|
||||
fn reset(&mut self) {
|
||||
self.changed = false;
|
||||
}
|
||||
|
||||
/// Update the access level of the bindings in the given module accordingly. The module access
|
||||
/// level has to be Exported or Public.
|
||||
/// This will also follow `use` chains (see PrivacyVisitor::set_import_binding_access_level).
|
||||
fn set_bindings_access_level(&mut self, module_id: LocalDefId) {
|
||||
/// Update effective visibilities of bindings in the given module,
|
||||
/// including their whole reexport chains.
|
||||
fn set_bindings_effective_visibilities(&mut self, module_id: LocalDefId) {
|
||||
assert!(self.r.module_map.contains_key(&&module_id.to_def_id()));
|
||||
let module = self.r.get_module(module_id.to_def_id()).unwrap();
|
||||
let resolutions = self.r.resolutions(module);
|
||||
|
||||
for (_, name_resolution) in resolutions.borrow().iter() {
|
||||
if let Some(mut binding) = name_resolution.borrow().binding() && !binding.is_ambiguity() {
|
||||
// Set the given binding access level to `AccessLevel::Public` and
|
||||
// sets the rest of the `use` chain to `AccessLevel::Exported` until
|
||||
// Set the given effective visibility level to `Level::Direct` and
|
||||
// sets the rest of the `use` chain to `Level::Reexported` until
|
||||
// we hit the actual exported item.
|
||||
|
||||
// FIXME: tag and is_public() condition should be removed, but assertions occur.
|
||||
let tag = if binding.is_import() { AccessLevel::Exported } else { AccessLevel::Public };
|
||||
let tag = if binding.is_import() { Level::Reexported } else { Level::Direct };
|
||||
if binding.vis.is_public() {
|
||||
let mut prev_parent_id = module_id;
|
||||
let mut level = AccessLevel::Public;
|
||||
let mut level = Level::Direct;
|
||||
while let NameBindingKind::Import { binding: nested_binding, import, .. } =
|
||||
binding.kind
|
||||
{
|
||||
|
@ -76,7 +75,7 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
|
|||
update(additional_ids.1);
|
||||
}
|
||||
|
||||
level = AccessLevel::Exported;
|
||||
level = Level::Reexported;
|
||||
prev_parent_id = self.r.local_def_id(import.id);
|
||||
binding = nested_binding;
|
||||
}
|
||||
|
@ -94,7 +93,7 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
|
|||
def_id: LocalDefId,
|
||||
nominal_vis: Visibility,
|
||||
parent_id: LocalDefId,
|
||||
tag: AccessLevel,
|
||||
tag: Level,
|
||||
) {
|
||||
let module_id = self
|
||||
.r
|
||||
|
@ -106,8 +105,8 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
|
|||
{
|
||||
return;
|
||||
}
|
||||
let mut access_levels = std::mem::take(&mut self.r.access_levels);
|
||||
self.changed |= access_levels.update(
|
||||
let mut effective_visibilities = std::mem::take(&mut self.r.effective_visibilities);
|
||||
self.changed |= effective_visibilities.update(
|
||||
def_id,
|
||||
nominal_vis,
|
||||
|| Visibility::Restricted(module_id),
|
||||
|
@ -115,14 +114,14 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
|
|||
tag,
|
||||
&*self.r,
|
||||
);
|
||||
self.r.access_levels = access_levels;
|
||||
self.r.effective_visibilities = effective_visibilities;
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r, 'ast> Visitor<'ast> for AccessLevelsVisitor<'ast, 'r> {
|
||||
impl<'r, 'ast> Visitor<'ast> for EffectiveVisibilitiesVisitor<'ast, 'r> {
|
||||
fn visit_item(&mut self, item: &'ast ast::Item) {
|
||||
let def_id = self.r.local_def_id(item.id);
|
||||
// Set access level of nested items.
|
||||
// Update effective visibilities of nested items.
|
||||
// If it's a mod, also make the visitor walk all of its items
|
||||
match item.kind {
|
||||
// Resolved in rustc_privacy when types are available
|
||||
|
@ -136,29 +135,29 @@ impl<'r, 'ast> Visitor<'ast> for AccessLevelsVisitor<'ast, 'r> {
|
|||
// Foreign modules inherit level from parents.
|
||||
ast::ItemKind::ForeignMod(..) => {
|
||||
let parent_id = self.r.local_parent(def_id);
|
||||
self.update(def_id, Visibility::Public, parent_id, AccessLevel::Public);
|
||||
self.update(def_id, Visibility::Public, parent_id, Level::Direct);
|
||||
}
|
||||
|
||||
// Only exported `macro_rules!` items are public, but they always are
|
||||
ast::ItemKind::MacroDef(ref macro_def) if macro_def.macro_rules => {
|
||||
let parent_id = self.r.local_parent(def_id);
|
||||
let vis = self.r.visibilities[&def_id];
|
||||
self.update(def_id, vis, parent_id, AccessLevel::Public);
|
||||
self.update(def_id, vis, parent_id, Level::Direct);
|
||||
}
|
||||
|
||||
ast::ItemKind::Mod(..) => {
|
||||
self.set_bindings_access_level(def_id);
|
||||
self.set_bindings_effective_visibilities(def_id);
|
||||
visit::walk_item(self, item);
|
||||
}
|
||||
|
||||
ast::ItemKind::Enum(EnumDef { ref variants }, _) => {
|
||||
self.set_bindings_access_level(def_id);
|
||||
self.set_bindings_effective_visibilities(def_id);
|
||||
for variant in variants {
|
||||
let variant_def_id = self.r.local_def_id(variant.id);
|
||||
for field in variant.data.fields() {
|
||||
let field_def_id = self.r.local_def_id(field.id);
|
||||
let vis = self.r.visibilities[&field_def_id];
|
||||
self.update(field_def_id, vis, variant_def_id, AccessLevel::Public);
|
||||
self.update(field_def_id, vis, variant_def_id, Level::Direct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -167,12 +166,12 @@ impl<'r, 'ast> Visitor<'ast> for AccessLevelsVisitor<'ast, 'r> {
|
|||
for field in def.fields() {
|
||||
let field_def_id = self.r.local_def_id(field.id);
|
||||
let vis = self.r.visibilities[&field_def_id];
|
||||
self.update(field_def_id, vis, def_id, AccessLevel::Public);
|
||||
self.update(field_def_id, vis, def_id, Level::Direct);
|
||||
}
|
||||
}
|
||||
|
||||
ast::ItemKind::Trait(..) => {
|
||||
self.set_bindings_access_level(def_id);
|
||||
self.set_bindings_effective_visibilities(def_id);
|
||||
}
|
||||
|
||||
ast::ItemKind::ExternCrate(..)
|
|
@ -7,6 +7,7 @@
|
|||
//! Type-relative name resolution (methods, fields, associated items) happens in `rustc_hir_analysis`.
|
||||
|
||||
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
|
||||
#![feature(assert_matches)]
|
||||
#![feature(box_patterns)]
|
||||
#![feature(drain_filter)]
|
||||
#![feature(if_let_guard)]
|
||||
|
@ -40,7 +41,7 @@ use rustc_hir::TraitCandidate;
|
|||
use rustc_index::vec::IndexVec;
|
||||
use rustc_metadata::creader::{CStore, CrateLoader};
|
||||
use rustc_middle::metadata::ModChild;
|
||||
use rustc_middle::middle::privacy::AccessLevels;
|
||||
use rustc_middle::middle::privacy::EffectiveVisibilities;
|
||||
use rustc_middle::span_bug;
|
||||
use rustc_middle::ty::{self, DefIdTree, MainDefinition, RegisteredTools};
|
||||
use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs};
|
||||
|
@ -63,15 +64,15 @@ use imports::{Import, ImportKind, ImportResolver, NameResolution};
|
|||
use late::{HasGenericParams, PathSource, PatternSource};
|
||||
use macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
|
||||
|
||||
use crate::access_levels::AccessLevelsVisitor;
|
||||
use crate::effective_visibilities::EffectiveVisibilitiesVisitor;
|
||||
|
||||
type Res = def::Res<NodeId>;
|
||||
|
||||
mod access_levels;
|
||||
mod build_reduced_graph;
|
||||
mod check_unused;
|
||||
mod def_collector;
|
||||
mod diagnostics;
|
||||
mod effective_visibilities;
|
||||
mod ident;
|
||||
mod imports;
|
||||
mod late;
|
||||
|
@ -1030,7 +1031,7 @@ pub struct Resolver<'a> {
|
|||
proc_macros: Vec<NodeId>,
|
||||
confused_type_with_std_module: FxHashMap<Span, Span>,
|
||||
|
||||
access_levels: AccessLevels,
|
||||
effective_visibilities: EffectiveVisibilities,
|
||||
}
|
||||
|
||||
/// Nothing really interesting here; it just provides memory for the rest of the crate.
|
||||
|
@ -1334,7 +1335,7 @@ impl<'a> Resolver<'a> {
|
|||
trait_impls: Default::default(),
|
||||
proc_macros: Default::default(),
|
||||
confused_type_with_std_module: Default::default(),
|
||||
access_levels: Default::default(),
|
||||
effective_visibilities: Default::default(),
|
||||
};
|
||||
|
||||
let root_parent_scope = ParentScope::module(graph_root, &resolver);
|
||||
|
@ -1392,14 +1393,14 @@ impl<'a> Resolver<'a> {
|
|||
let glob_map = self.glob_map;
|
||||
let main_def = self.main_def;
|
||||
let confused_type_with_std_module = self.confused_type_with_std_module;
|
||||
let access_levels = self.access_levels;
|
||||
let effective_visibilities = self.effective_visibilities;
|
||||
let global_ctxt = ResolverGlobalCtxt {
|
||||
cstore,
|
||||
source_span,
|
||||
expn_that_defined,
|
||||
visibilities,
|
||||
has_pub_restricted,
|
||||
access_levels,
|
||||
effective_visibilities,
|
||||
extern_crate_map,
|
||||
reexport_map,
|
||||
glob_map,
|
||||
|
@ -1457,7 +1458,7 @@ impl<'a> Resolver<'a> {
|
|||
proc_macros,
|
||||
confused_type_with_std_module: self.confused_type_with_std_module.clone(),
|
||||
registered_tools: self.registered_tools.clone(),
|
||||
access_levels: self.access_levels.clone(),
|
||||
effective_visibilities: self.effective_visibilities.clone(),
|
||||
};
|
||||
let ast_lowering = ty::ResolverAstLowering {
|
||||
legacy_const_generic_args: self.legacy_const_generic_args.clone(),
|
||||
|
@ -1520,8 +1521,8 @@ impl<'a> Resolver<'a> {
|
|||
pub fn resolve_crate(&mut self, krate: &Crate) {
|
||||
self.session.time("resolve_crate", || {
|
||||
self.session.time("finalize_imports", || ImportResolver { r: self }.finalize_imports());
|
||||
self.session.time("resolve_access_levels", || {
|
||||
AccessLevelsVisitor::compute_access_levels(self, krate)
|
||||
self.session.time("compute_effective_visibilities", || {
|
||||
EffectiveVisibilitiesVisitor::compute_effective_visibilities(self, krate)
|
||||
});
|
||||
self.session.time("finalize_macro_resolutions", || self.finalize_macro_resolutions());
|
||||
self.session.time("late_resolve_crate", || self.late_resolve_crate(krate));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue