]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_resolve/src/access_levels.rs
Auto merge of #100574 - Urgau:check-cfg-warn-cfg, r=petrochenkov
[rust.git] / compiler / rustc_resolve / src / access_levels.rs
1 use crate::imports::ImportKind;
2 use crate::NameBinding;
3 use crate::NameBindingKind;
4 use crate::Resolver;
5 use rustc_ast::ast;
6 use rustc_ast::visit;
7 use rustc_ast::visit::Visitor;
8 use rustc_ast::Crate;
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;
15 use rustc_span::sym;
16
17 pub struct AccessLevelsVisitor<'r, 'a> {
18     r: &'r mut Resolver<'a>,
19     changed: bool,
20 }
21
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 };
28
29         visitor.set_access_level_def_id(CRATE_DEF_ID, Some(AccessLevel::Public));
30         visitor.set_bindings_access_level(CRATE_DEF_ID);
31
32         while visitor.changed {
33             visitor.reset();
34             visit::walk_crate(&mut visitor, krate);
35         }
36
37         info!("resolve::access_levels: {:#?}", r.access_levels);
38     }
39
40     fn reset(&mut self) {
41         self.changed = false;
42     }
43
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() {
51             return;
52         }
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, .. } =
59                     binding.kind
60                 {
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);
65                     }
66
67                     access_level = Some(AccessLevel::Exported);
68                     binding = nested_binding;
69                 }
70             };
71
72         let module = self.r.get_module(module_id.to_def_id()).unwrap();
73         let resolutions = self.r.resolutions(module);
74
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() {
78                     true => {
79                         set_import_binding_access_level(self, binding, module_level);
80                         Some(AccessLevel::Exported)
81                     },
82                     false => module_level,
83                 };
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);
86                 }
87             }
88         }
89     }
90
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`
93     fn set_access_level(
94         &mut self,
95         node_id: NodeId,
96         access_level: Option<AccessLevel>,
97     ) -> Option<AccessLevel> {
98         self.set_access_level_def_id(self.r.local_def_id(node_id), access_level)
99     }
100
101     fn set_access_level_def_id(
102         &mut self,
103         def_id: LocalDefId,
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());
109             self.changed = true;
110             access_level
111         } else {
112             old_level
113         }
114     }
115 }
116
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
122         match item.kind {
123             // Resolved in rustc_privacy when types are available
124             ast::ItemKind::Impl(..) => return,
125
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"
129             ),
130
131             // Foreign modules inherit level from parents.
132             ast::ItemKind::ForeignMod(..) => {
133                 let parent_level =
134                     self.r.access_levels.map.get(&self.r.local_parent(def_id)).copied();
135                 self.set_access_level(item.id, parent_level);
136             }
137
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));
142                 }
143             }
144
145             ast::ItemKind::Mod(..) => {
146                 self.set_bindings_access_level(def_id);
147                 visit::walk_item(self, item);
148             }
149
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);
157                     }
158                 }
159             }
160
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);
166                     }
167                 }
168             }
169
170             ast::ItemKind::Trait(..) => {
171                 self.set_bindings_access_level(def_id);
172             }
173
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,
183         }
184     }
185 }