]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_resolve/src/access_levels.rs
Rollup merge of #103280 - finnbear:impl_string_leak_2, r=joshtriplett
[rust.git] / compiler / rustc_resolve / src / access_levels.rs
1 use crate::NameBindingKind;
2 use crate::Resolver;
3 use rustc_ast::ast;
4 use rustc_ast::visit;
5 use rustc_ast::visit::Visitor;
6 use rustc_ast::Crate;
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};
12
13 pub struct AccessLevelsVisitor<'r, 'a> {
14     r: &'r mut Resolver<'a>,
15     changed: bool,
16 }
17
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 };
24
25         visitor.update(CRATE_DEF_ID, Visibility::Public, CRATE_DEF_ID, AccessLevel::Public);
26         visitor.set_bindings_access_level(CRATE_DEF_ID);
27
28         while visitor.changed {
29             visitor.reset();
30             visit::walk_crate(&mut visitor, krate);
31         }
32
33         info!("resolve::access_levels: {:#?}", r.access_levels);
34     }
35
36     fn reset(&mut self) {
37         self.changed = false;
38     }
39
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);
47
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.
53
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, .. } =
61                         binding.kind
62                     {
63                         let id = self.r.local_def_id(self.r.import_id_for_ns(import, key.ns));
64                         self.update(
65                             id,
66                             binding.vis.expect_local(),
67                             prev_parent_id,
68                             level,
69                         );
70
71                         level = AccessLevel::Exported;
72                         prev_parent_id = id;
73                         binding = nested_binding;
74                     }
75                 }
76
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);
79                 }
80             }
81         }
82     }
83
84     fn update(
85         &mut self,
86         def_id: LocalDefId,
87         nominal_vis: Visibility,
88         parent_id: LocalDefId,
89         tag: AccessLevel,
90     ) {
91         let mut access_levels = std::mem::take(&mut self.r.access_levels);
92         let module_id =
93             self.r.get_nearest_non_block_module(def_id.to_def_id()).def_id().expect_local();
94         let res = access_levels.update(
95             def_id,
96             nominal_vis,
97             || Visibility::Restricted(module_id),
98             parent_id,
99             tag,
100             &*self.r,
101         );
102         if let Ok(changed) = res {
103             self.changed |= changed;
104         } else {
105             self.r.session.delay_span_bug(
106                 self.r.opt_span(def_id.to_def_id()).unwrap(),
107                 "Can't update effective visibility",
108             );
109         }
110         self.r.access_levels = access_levels;
111     }
112 }
113
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
119         match item.kind {
120             // Resolved in rustc_privacy when types are available
121             ast::ItemKind::Impl(..) => return,
122
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"
126             ),
127
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);
132             }
133
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);
139             }
140
141             ast::ItemKind::Mod(..) => {
142                 self.set_bindings_access_level(def_id);
143                 visit::walk_item(self, item);
144             }
145
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);
154                     }
155                 }
156             }
157
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);
163                 }
164             }
165
166             ast::ItemKind::Trait(..) => {
167                 self.set_bindings_access_level(def_id);
168             }
169
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,
179         }
180     }
181 }