1 use crate::NameBinding;
2 use crate::NameBindingKind;
6 use rustc_ast::visit::Visitor;
8 use rustc_ast::EnumDef;
10 use rustc_hir::def_id::LocalDefId;
11 use rustc_hir::def_id::CRATE_DEF_ID;
12 use rustc_middle::middle::privacy::AccessLevel;
13 use rustc_middle::ty::DefIdTree;
16 pub struct AccessLevelsVisitor<'r, 'a> {
17 r: &'r mut Resolver<'a>,
21 impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
22 /// Fills the `Resolver::access_levels` table with public & exported items
23 /// For now, this doesn't resolve macros (FIXME) and cannot resolve Impl, as we
24 /// need access to a TyCtxt for that.
25 pub fn compute_access_levels<'c>(r: &'r mut Resolver<'a>, krate: &'c Crate) {
26 let mut visitor = AccessLevelsVisitor { r, changed: false };
28 visitor.set_access_level_def_id(CRATE_DEF_ID, Some(AccessLevel::Public));
29 visitor.set_bindings_access_level(CRATE_DEF_ID);
31 while visitor.changed {
33 visit::walk_crate(&mut visitor, krate);
36 info!("resolve::access_levels: {:#?}", r.access_levels);
43 /// Update the access level of the bindings in the given module accordingly. The module access
44 /// level has to be Exported or Public.
45 /// This will also follow `use` chains (see PrivacyVisitor::set_import_binding_access_level).
46 fn set_bindings_access_level(&mut self, module_id: LocalDefId) {
47 assert!(self.r.module_map.contains_key(&&module_id.to_def_id()));
48 let module_level = self.r.access_levels.get_access_level(module_id);
49 if !module_level.is_some() {
52 // Set the given binding access level to `AccessLevel::Public` and
53 // sets the rest of the `use` chain to `AccessLevel::Exported` until
54 // we hit the actual exported item.
55 let set_import_binding_access_level =
56 |this: &mut Self, mut binding: &NameBinding<'a>, mut access_level, ns| {
57 while let NameBindingKind::Import { binding: nested_binding, import, .. } =
60 this.set_access_level(this.r.import_id_for_ns(import, ns), access_level);
62 access_level = Some(AccessLevel::Exported);
63 binding = nested_binding;
67 let module = self.r.get_module(module_id.to_def_id()).unwrap();
68 let resolutions = self.r.resolutions(module);
70 for (key, name_resolution) in resolutions.borrow().iter() {
71 if let Some(binding) = name_resolution.borrow().binding() && binding.vis.is_public() && !binding.is_ambiguity() {
72 let access_level = match binding.is_import() {
74 set_import_binding_access_level(self, binding, module_level, key.ns);
75 Some(AccessLevel::Exported)
77 false => module_level,
79 if let Some(def_id) = binding.res().opt_def_id().and_then(|id| id.as_local()) {
80 self.set_access_level_def_id(def_id, access_level);
86 /// Sets the access level of the `LocalDefId` corresponding to the given `NodeId`.
87 /// This function will panic if the `NodeId` does not have a `LocalDefId`
91 access_level: Option<AccessLevel>,
92 ) -> Option<AccessLevel> {
93 self.set_access_level_def_id(self.r.local_def_id(node_id), access_level)
96 fn set_access_level_def_id(
99 access_level: Option<AccessLevel>,
100 ) -> Option<AccessLevel> {
101 let old_level = self.r.access_levels.get_access_level(def_id);
102 if old_level < access_level {
103 self.r.access_levels.set_access_level(def_id, access_level.unwrap());
112 impl<'r, 'ast> Visitor<'ast> for AccessLevelsVisitor<'ast, 'r> {
113 fn visit_item(&mut self, item: &'ast ast::Item) {
114 let def_id = self.r.local_def_id(item.id);
115 // Set access level of nested items.
116 // If it's a mod, also make the visitor walk all of its items
118 // Resolved in rustc_privacy when types are available
119 ast::ItemKind::Impl(..) => return,
121 // Should be unreachable at this stage
122 ast::ItemKind::MacCall(..) => panic!(
123 "ast::ItemKind::MacCall encountered, this should not anymore appear at this stage"
126 // Foreign modules inherit level from parents.
127 ast::ItemKind::ForeignMod(..) => {
129 self.r.access_levels.get_access_level(self.r.local_parent(def_id));
130 self.set_access_level(item.id, parent_level);
133 // Only exported `macro_rules!` items are public, but they always are
134 ast::ItemKind::MacroDef(ref macro_def) if macro_def.macro_rules => {
135 if item.attrs.iter().any(|attr| attr.has_name(sym::macro_export)) {
136 self.set_access_level(item.id, Some(AccessLevel::Public));
140 ast::ItemKind::Mod(..) => {
141 self.set_bindings_access_level(def_id);
142 visit::walk_item(self, item);
145 ast::ItemKind::Enum(EnumDef { ref variants }, _) => {
146 self.set_bindings_access_level(def_id);
147 for variant in variants {
148 let variant_def_id = self.r.local_def_id(variant.id);
149 let variant_level = self.r.access_levels.get_access_level(variant_def_id);
150 for field in variant.data.fields() {
151 self.set_access_level(field.id, variant_level);
156 ast::ItemKind::Struct(ref def, _) | ast::ItemKind::Union(ref def, _) => {
157 let inherited_level = self.r.access_levels.get_access_level(def_id);
158 for field in def.fields() {
159 if field.vis.kind.is_pub() {
160 self.set_access_level(field.id, inherited_level);
165 ast::ItemKind::Trait(..) => {
166 self.set_bindings_access_level(def_id);
169 ast::ItemKind::ExternCrate(..)
170 | ast::ItemKind::Use(..)
171 | ast::ItemKind::Static(..)
172 | ast::ItemKind::Const(..)
173 | ast::ItemKind::GlobalAsm(..)
174 | ast::ItemKind::TyAlias(..)
175 | ast::ItemKind::TraitAlias(..)
176 | ast::ItemKind::MacroDef(..)
177 | ast::ItemKind::Fn(..) => return,