]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_resolve/src/access_levels.rs
Rollup merge of #100470 - reitermarkus:patch-1, r=joshtriplett
[rust.git] / compiler / rustc_resolve / src / access_levels.rs
1 use crate::NameBinding;
2 use crate::NameBindingKind;
3 use crate::Resolver;
4 use rustc_ast::ast;
5 use rustc_ast::visit;
6 use rustc_ast::visit::Visitor;
7 use rustc_ast::Crate;
8 use rustc_ast::EnumDef;
9 use rustc_ast::NodeId;
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;
14 use rustc_span::sym;
15
16 pub struct AccessLevelsVisitor<'r, 'a> {
17     r: &'r mut Resolver<'a>,
18     changed: bool,
19 }
20
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 };
27
28         visitor.set_access_level_def_id(CRATE_DEF_ID, Some(AccessLevel::Public));
29         visitor.set_bindings_access_level(CRATE_DEF_ID);
30
31         while visitor.changed {
32             visitor.reset();
33             visit::walk_crate(&mut visitor, krate);
34         }
35
36         info!("resolve::access_levels: {:#?}", r.access_levels);
37     }
38
39     fn reset(&mut self) {
40         self.changed = false;
41     }
42
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() {
50             return;
51         }
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, .. } =
58                     binding.kind
59                 {
60                     this.set_access_level(this.r.import_id_for_ns(import, ns), access_level);
61
62                     access_level = Some(AccessLevel::Exported);
63                     binding = nested_binding;
64                 }
65             };
66
67         let module = self.r.get_module(module_id.to_def_id()).unwrap();
68         let resolutions = self.r.resolutions(module);
69
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() {
73                     true => {
74                         set_import_binding_access_level(self, binding, module_level, key.ns);
75                         Some(AccessLevel::Exported)
76                     },
77                     false => module_level,
78                 };
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);
81                 }
82             }
83         }
84     }
85
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`
88     fn set_access_level(
89         &mut self,
90         node_id: NodeId,
91         access_level: Option<AccessLevel>,
92     ) -> Option<AccessLevel> {
93         self.set_access_level_def_id(self.r.local_def_id(node_id), access_level)
94     }
95
96     fn set_access_level_def_id(
97         &mut self,
98         def_id: LocalDefId,
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());
104             self.changed = true;
105             access_level
106         } else {
107             old_level
108         }
109     }
110 }
111
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
117         match item.kind {
118             // Resolved in rustc_privacy when types are available
119             ast::ItemKind::Impl(..) => return,
120
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"
124             ),
125
126             // Foreign modules inherit level from parents.
127             ast::ItemKind::ForeignMod(..) => {
128                 let parent_level =
129                     self.r.access_levels.get_access_level(self.r.local_parent(def_id));
130                 self.set_access_level(item.id, parent_level);
131             }
132
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));
137                 }
138             }
139
140             ast::ItemKind::Mod(..) => {
141                 self.set_bindings_access_level(def_id);
142                 visit::walk_item(self, item);
143             }
144
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);
152                     }
153                 }
154             }
155
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);
161                     }
162                 }
163             }
164
165             ast::ItemKind::Trait(..) => {
166                 self.set_bindings_access_level(def_id);
167             }
168
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,
178         }
179     }
180 }