1 use crate::NameBindingKind;
5 use rustc_ast::visit::Visitor;
7 use rustc_ast::EnumDef;
8 use rustc_hir::def_id::LocalDefId;
9 use rustc_hir::def_id::CRATE_DEF_ID;
10 use rustc_middle::middle::privacy::AccessLevel;
11 use rustc_middle::ty::{DefIdTree, Visibility};
13 pub struct AccessLevelsVisitor<'r, 'a> {
14 r: &'r mut Resolver<'a>,
18 impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
19 /// Fills the `Resolver::access_levels` table with public & exported items
20 /// For now, this doesn't resolve macros (FIXME) and cannot resolve Impl, as we
21 /// need access to a TyCtxt for that.
22 pub fn compute_access_levels<'c>(r: &'r mut Resolver<'a>, krate: &'c Crate) {
23 let mut visitor = AccessLevelsVisitor { r, changed: false };
25 visitor.update(CRATE_DEF_ID, Visibility::Public, CRATE_DEF_ID, AccessLevel::Public);
26 visitor.set_bindings_access_level(CRATE_DEF_ID);
28 while visitor.changed {
30 visit::walk_crate(&mut visitor, krate);
33 info!("resolve::access_levels: {:#?}", r.access_levels);
40 /// Update the access level of the bindings in the given module accordingly. The module access
41 /// level has to be Exported or Public.
42 /// This will also follow `use` chains (see PrivacyVisitor::set_import_binding_access_level).
43 fn set_bindings_access_level(&mut self, module_id: LocalDefId) {
44 assert!(self.r.module_map.contains_key(&&module_id.to_def_id()));
45 let module = self.r.get_module(module_id.to_def_id()).unwrap();
46 let resolutions = self.r.resolutions(module);
48 for (key, name_resolution) in resolutions.borrow().iter() {
49 if let Some(mut binding) = name_resolution.borrow().binding() && !binding.is_ambiguity() {
50 // Set the given binding access level to `AccessLevel::Public` and
51 // sets the rest of the `use` chain to `AccessLevel::Exported` until
52 // we hit the actual exported item.
54 // FIXME: tag and is_public() condition must be deleted,
55 // but assertion fail occurs in import_id_for_ns
56 let tag = if binding.is_import() { AccessLevel::Exported } else { AccessLevel::Public };
57 if binding.vis.is_public() {
58 let mut prev_parent_id = module_id;
59 let mut level = AccessLevel::Public;
60 while let NameBindingKind::Import { binding: nested_binding, import, .. } =
63 let id = self.r.local_def_id(self.r.import_id_for_ns(import, key.ns));
66 binding.vis.expect_local(),
71 level = AccessLevel::Exported;
73 binding = nested_binding;
77 if let Some(def_id) = binding.res().opt_def_id().and_then(|id| id.as_local()) {
78 self.update(def_id, binding.vis.expect_local(), module_id, tag);
87 nominal_vis: Visibility,
88 parent_id: LocalDefId,
91 let mut access_levels = std::mem::take(&mut self.r.access_levels);
93 self.r.get_nearest_non_block_module(def_id.to_def_id()).def_id().expect_local();
94 let res = access_levels.update(
97 || Visibility::Restricted(module_id),
102 if let Ok(changed) = res {
103 self.changed |= changed;
105 self.r.session.delay_span_bug(
106 self.r.opt_span(def_id.to_def_id()).unwrap(),
107 "Can't update effective visibility",
110 self.r.access_levels = access_levels;
114 impl<'r, 'ast> Visitor<'ast> for AccessLevelsVisitor<'ast, 'r> {
115 fn visit_item(&mut self, item: &'ast ast::Item) {
116 let def_id = self.r.local_def_id(item.id);
117 // Set access level of nested items.
118 // If it's a mod, also make the visitor walk all of its items
120 // Resolved in rustc_privacy when types are available
121 ast::ItemKind::Impl(..) => return,
123 // Should be unreachable at this stage
124 ast::ItemKind::MacCall(..) => panic!(
125 "ast::ItemKind::MacCall encountered, this should not anymore appear at this stage"
128 // Foreign modules inherit level from parents.
129 ast::ItemKind::ForeignMod(..) => {
130 let parent_id = self.r.local_parent(def_id);
131 self.update(def_id, Visibility::Public, parent_id, AccessLevel::Public);
134 // Only exported `macro_rules!` items are public, but they always are
135 ast::ItemKind::MacroDef(ref macro_def) if macro_def.macro_rules => {
136 let parent_id = self.r.local_parent(def_id);
137 let vis = self.r.visibilities[&def_id];
138 self.update(def_id, vis, parent_id, AccessLevel::Public);
141 ast::ItemKind::Mod(..) => {
142 self.set_bindings_access_level(def_id);
143 visit::walk_item(self, item);
146 ast::ItemKind::Enum(EnumDef { ref variants }, _) => {
147 self.set_bindings_access_level(def_id);
148 for variant in variants {
149 let variant_def_id = self.r.local_def_id(variant.id);
150 for field in variant.data.fields() {
151 let field_def_id = self.r.local_def_id(field.id);
152 let vis = self.r.visibilities[&field_def_id];
153 self.update(field_def_id, vis, variant_def_id, AccessLevel::Public);
158 ast::ItemKind::Struct(ref def, _) | ast::ItemKind::Union(ref def, _) => {
159 for field in def.fields() {
160 let field_def_id = self.r.local_def_id(field.id);
161 let vis = self.r.visibilities[&field_def_id];
162 self.update(field_def_id, vis, def_id, AccessLevel::Public);
166 ast::ItemKind::Trait(..) => {
167 self.set_bindings_access_level(def_id);
170 ast::ItemKind::ExternCrate(..)
171 | ast::ItemKind::Use(..)
172 | ast::ItemKind::Static(..)
173 | ast::ItemKind::Const(..)
174 | ast::ItemKind::GlobalAsm(..)
175 | ast::ItemKind::TyAlias(..)
176 | ast::ItemKind::TraitAlias(..)
177 | ast::ItemKind::MacroDef(..)
178 | ast::ItemKind::Fn(..) => return,