1 use crate::imports::ImportKind;
2 use crate::NameBinding;
3 use crate::NameBindingKind;
7 use rustc_ast::visit::Visitor;
9 use rustc_ast::EnumDef;
10 use rustc_ast::NodeId;
11 use rustc_hir::def_id::LocalDefId;
12 use rustc_hir::def_id::CRATE_DEF_ID;
13 use rustc_middle::middle::privacy::AccessLevel;
14 use rustc_middle::ty::DefIdTree;
17 pub struct AccessLevelsVisitor<'r, 'a> {
18 r: &'r mut Resolver<'a>,
22 impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
23 /// Fills the `Resolver::access_levels` table with public & exported items
24 /// For now, this doesn't resolve macros (FIXME) and cannot resolve Impl, as we
25 /// need access to a TyCtxt for that.
26 pub fn compute_access_levels<'c>(r: &'r mut Resolver<'a>, krate: &'c Crate) {
27 let mut visitor = AccessLevelsVisitor { r, changed: false };
29 visitor.set_access_level_def_id(CRATE_DEF_ID, Some(AccessLevel::Public));
30 visitor.set_bindings_access_level(CRATE_DEF_ID);
32 while visitor.changed {
34 visit::walk_crate(&mut visitor, krate);
37 info!("resolve::access_levels: {:#?}", r.access_levels);
44 /// Update the access level of the bindings in the given module accordingly. The module access
45 /// level has to be Exported or Public.
46 /// This will also follow `use` chains (see PrivacyVisitor::set_import_binding_access_level).
47 fn set_bindings_access_level(&mut self, module_id: LocalDefId) {
48 assert!(self.r.module_map.contains_key(&&module_id.to_def_id()));
49 let module_level = self.r.access_levels.map.get(&module_id).copied();
50 if !module_level.is_some() {
53 // Set the given binding access level to `AccessLevel::Public` and
54 // sets the rest of the `use` chain to `AccessLevel::Exported` until
55 // we hit the actual exported item.
56 let set_import_binding_access_level =
57 |this: &mut Self, mut binding: &NameBinding<'a>, mut access_level| {
58 while let NameBindingKind::Import { binding: nested_binding, import, .. } =
61 this.set_access_level(import.id, access_level);
62 if let ImportKind::Single { additional_ids, .. } = import.kind {
63 this.set_access_level(additional_ids.0, access_level);
64 this.set_access_level(additional_ids.1, access_level);
67 access_level = Some(AccessLevel::Exported);
68 binding = nested_binding;
72 let module = self.r.get_module(module_id.to_def_id()).unwrap();
73 let resolutions = self.r.resolutions(module);
75 for (.., name_resolution) in resolutions.borrow().iter() {
76 if let Some(binding) = name_resolution.borrow().binding() && binding.vis.is_public() && !binding.is_ambiguity() {
77 let access_level = match binding.is_import() {
79 set_import_binding_access_level(self, binding, module_level);
80 Some(AccessLevel::Exported)
82 false => module_level,
84 if let Some(def_id) = binding.res().opt_def_id().and_then(|id| id.as_local()) {
85 self.set_access_level_def_id(def_id, access_level);
91 /// Sets the access level of the `LocalDefId` corresponding to the given `NodeId`.
92 /// This function will panic if the `NodeId` does not have a `LocalDefId`
96 access_level: Option<AccessLevel>,
97 ) -> Option<AccessLevel> {
98 self.set_access_level_def_id(self.r.local_def_id(node_id), access_level)
101 fn set_access_level_def_id(
104 access_level: Option<AccessLevel>,
105 ) -> Option<AccessLevel> {
106 let old_level = self.r.access_levels.map.get(&def_id).copied();
107 if old_level < access_level {
108 self.r.access_levels.map.insert(def_id, access_level.unwrap());
117 impl<'r, 'ast> Visitor<'ast> for AccessLevelsVisitor<'ast, 'r> {
118 fn visit_item(&mut self, item: &'ast ast::Item) {
119 let def_id = self.r.local_def_id(item.id);
120 // Set access level of nested items.
121 // If it's a mod, also make the visitor walk all of its items
123 // Resolved in rustc_privacy when types are available
124 ast::ItemKind::Impl(..) => return,
126 // Should be unreachable at this stage
127 ast::ItemKind::MacCall(..) => panic!(
128 "ast::ItemKind::MacCall encountered, this should not anymore appear at this stage"
131 // Foreign modules inherit level from parents.
132 ast::ItemKind::ForeignMod(..) => {
134 self.r.access_levels.map.get(&self.r.local_parent(def_id)).copied();
135 self.set_access_level(item.id, parent_level);
138 // Only exported `macro_rules!` items are public, but they always are
139 ast::ItemKind::MacroDef(ref macro_def) if macro_def.macro_rules => {
140 if item.attrs.iter().any(|attr| attr.has_name(sym::macro_export)) {
141 self.set_access_level(item.id, Some(AccessLevel::Public));
145 ast::ItemKind::Mod(..) => {
146 self.set_bindings_access_level(def_id);
147 visit::walk_item(self, item);
150 ast::ItemKind::Enum(EnumDef { ref variants }, _) => {
151 self.set_bindings_access_level(def_id);
152 for variant in variants {
153 let variant_def_id = self.r.local_def_id(variant.id);
154 let variant_level = self.r.access_levels.map.get(&variant_def_id).copied();
155 for field in variant.data.fields() {
156 self.set_access_level(field.id, variant_level);
161 ast::ItemKind::Struct(ref def, _) | ast::ItemKind::Union(ref def, _) => {
162 let inherited_level = self.r.access_levels.map.get(&def_id).copied();
163 for field in def.fields() {
164 if field.vis.kind.is_pub() {
165 self.set_access_level(field.id, inherited_level);
170 ast::ItemKind::Trait(..) => {
171 self.set_bindings_access_level(def_id);
174 ast::ItemKind::ExternCrate(..)
175 | ast::ItemKind::Use(..)
176 | ast::ItemKind::Static(..)
177 | ast::ItemKind::Const(..)
178 | ast::ItemKind::GlobalAsm(..)
179 | ast::ItemKind::TyAlias(..)
180 | ast::ItemKind::TraitAlias(..)
181 | ast::ItemKind::MacroDef(..)
182 | ast::ItemKind::Fn(..) => return,