]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_resolve/src/effective_visibilities.rs
Rollup merge of #104304 - uweigand:s390x-profiler, r=Mark-Simulacrum
[rust.git] / compiler / rustc_resolve / src / effective_visibilities.rs
1 use crate::{ImportKind, NameBinding, NameBindingKind, Resolver, ResolverTree};
2 use rustc_ast::ast;
3 use rustc_ast::visit;
4 use rustc_ast::visit::Visitor;
5 use rustc_ast::Crate;
6 use rustc_ast::EnumDef;
7 use rustc_data_structures::intern::Interned;
8 use rustc_hir::def_id::LocalDefId;
9 use rustc_hir::def_id::CRATE_DEF_ID;
10 use rustc_middle::middle::privacy::{EffectiveVisibilities, EffectiveVisibility, Level};
11 use rustc_middle::ty::Visibility;
12
13 type ImportId<'a> = Interned<'a, NameBinding<'a>>;
14
15 #[derive(Clone, Copy)]
16 enum ParentId<'a> {
17     Def(LocalDefId),
18     Import(ImportId<'a>),
19 }
20
21 impl ParentId<'_> {
22     fn level(self) -> Level {
23         match self {
24             ParentId::Def(_) => Level::Direct,
25             ParentId::Import(_) => Level::Reexported,
26         }
27     }
28 }
29
30 pub struct EffectiveVisibilitiesVisitor<'r, 'a> {
31     r: &'r mut Resolver<'a>,
32     /// While walking import chains we need to track effective visibilities per-binding, and def id
33     /// keys in `Resolver::effective_visibilities` are not enough for that, because multiple
34     /// bindings can correspond to a single def id in imports. So we keep a separate table.
35     import_effective_visibilities: EffectiveVisibilities<ImportId<'a>>,
36     changed: bool,
37 }
38
39 impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> {
40     /// Fills the `Resolver::effective_visibilities` table with public & exported items
41     /// For now, this doesn't resolve macros (FIXME) and cannot resolve Impl, as we
42     /// need access to a TyCtxt for that.
43     pub fn compute_effective_visibilities<'c>(r: &'r mut Resolver<'a>, krate: &'c Crate) {
44         let mut visitor = EffectiveVisibilitiesVisitor {
45             r,
46             import_effective_visibilities: Default::default(),
47             changed: false,
48         };
49
50         visitor.update(CRATE_DEF_ID, CRATE_DEF_ID);
51         visitor.set_bindings_effective_visibilities(CRATE_DEF_ID);
52
53         while visitor.changed {
54             visitor.changed = false;
55             visit::walk_crate(&mut visitor, krate);
56         }
57
58         // Update visibilities for import def ids. These are not used during the
59         // `EffectiveVisibilitiesVisitor` pass, because we have more detailed binding-based
60         // information, but are used by later passes. Effective visibility of an import def id
61         // is the maximum value among visibilities of bindings corresponding to that def id.
62         for (binding, eff_vis) in visitor.import_effective_visibilities.iter() {
63             let NameBindingKind::Import { import, .. } = binding.kind else { unreachable!() };
64             if let Some(node_id) = import.id() {
65                 let mut update = |node_id| {
66                     r.effective_visibilities.update_eff_vis(
67                         r.local_def_id(node_id),
68                         eff_vis,
69                         ResolverTree(&r.definitions, &r.crate_loader),
70                     )
71                 };
72                 update(node_id);
73                 if let ImportKind::Single { additional_ids: (id1, id2), .. } = import.kind {
74                     // In theory all the single import IDs have individual visibilities and
75                     // effective visibilities, but in practice these IDs go straigth to HIR
76                     // where all their few uses assume that their (effective) visibility
77                     // applies to the whole syntactic `use` item. So they all get the same
78                     // value which is the maximum of all bindings. Maybe HIR for imports
79                     // shouldn't use three IDs at all.
80                     if id1 != ast::DUMMY_NODE_ID {
81                         update(id1);
82                     }
83                     if id2 != ast::DUMMY_NODE_ID {
84                         update(id2);
85                     }
86                 }
87             }
88         }
89
90         info!("resolve::effective_visibilities: {:#?}", r.effective_visibilities);
91     }
92
93     fn nearest_normal_mod(&mut self, def_id: LocalDefId) -> LocalDefId {
94         self.r.get_nearest_non_block_module(def_id.to_def_id()).nearest_parent_mod().expect_local()
95     }
96
97     /// Update effective visibilities of bindings in the given module,
98     /// including their whole reexport chains.
99     fn set_bindings_effective_visibilities(&mut self, module_id: LocalDefId) {
100         assert!(self.r.module_map.contains_key(&&module_id.to_def_id()));
101         let module = self.r.get_module(module_id.to_def_id()).unwrap();
102         let resolutions = self.r.resolutions(module);
103
104         for (_, name_resolution) in resolutions.borrow().iter() {
105             if let Some(mut binding) = name_resolution.borrow().binding() && !binding.is_ambiguity() {
106                 // Set the given effective visibility level to `Level::Direct` and
107                 // sets the rest of the `use` chain to `Level::Reexported` until
108                 // we hit the actual exported item.
109                 let mut parent_id = ParentId::Def(module_id);
110                 while let NameBindingKind::Import { binding: nested_binding, .. } = binding.kind {
111                     let binding_id = ImportId::new_unchecked(binding);
112                     self.update_import(binding_id, parent_id);
113
114                     parent_id = ParentId::Import(binding_id);
115                     binding = nested_binding;
116                 }
117
118                 if let Some(def_id) = binding.res().opt_def_id().and_then(|id| id.as_local()) {
119                     self.update_def(def_id, binding.vis.expect_local(), parent_id);
120                 }
121             }
122         }
123     }
124
125     fn effective_vis(&self, parent_id: ParentId<'a>) -> Option<EffectiveVisibility> {
126         match parent_id {
127             ParentId::Def(def_id) => self.r.effective_visibilities.effective_vis(def_id),
128             ParentId::Import(binding) => self.import_effective_visibilities.effective_vis(binding),
129         }
130         .copied()
131     }
132
133     /// The update is guaranteed to not change the table and we can skip it.
134     fn is_noop_update(
135         &self,
136         parent_id: ParentId<'a>,
137         nominal_vis: Visibility,
138         default_vis: Visibility,
139     ) -> bool {
140         nominal_vis == default_vis
141             || match parent_id {
142                 ParentId::Def(def_id) => self.r.visibilities[&def_id],
143                 ParentId::Import(binding) => binding.vis.expect_local(),
144             } == default_vis
145     }
146
147     fn update_import(&mut self, binding: ImportId<'a>, parent_id: ParentId<'a>) {
148         let NameBindingKind::Import { import, .. } = binding.kind else { unreachable!() };
149         let nominal_vis = binding.vis.expect_local();
150         let default_vis = Visibility::Restricted(
151             import
152                 .id()
153                 .map(|id| self.nearest_normal_mod(self.r.local_def_id(id)))
154                 .unwrap_or(CRATE_DEF_ID),
155         );
156         if self.is_noop_update(parent_id, nominal_vis, default_vis) {
157             return;
158         }
159         self.changed |= self.import_effective_visibilities.update(
160             binding,
161             nominal_vis,
162             default_vis,
163             self.effective_vis(parent_id),
164             parent_id.level(),
165             ResolverTree(&self.r.definitions, &self.r.crate_loader),
166         );
167     }
168
169     fn update_def(&mut self, def_id: LocalDefId, nominal_vis: Visibility, parent_id: ParentId<'a>) {
170         let default_vis = Visibility::Restricted(self.nearest_normal_mod(def_id));
171         if self.is_noop_update(parent_id, nominal_vis, default_vis) {
172             return;
173         }
174         self.changed |= self.r.effective_visibilities.update(
175             def_id,
176             nominal_vis,
177             if def_id == CRATE_DEF_ID { Visibility::Public } else { default_vis },
178             self.effective_vis(parent_id),
179             parent_id.level(),
180             ResolverTree(&self.r.definitions, &self.r.crate_loader),
181         );
182     }
183
184     fn update(&mut self, def_id: LocalDefId, parent_id: LocalDefId) {
185         self.update_def(def_id, self.r.visibilities[&def_id], ParentId::Def(parent_id));
186     }
187 }
188
189 impl<'r, 'ast> Visitor<'ast> for EffectiveVisibilitiesVisitor<'ast, 'r> {
190     fn visit_item(&mut self, item: &'ast ast::Item) {
191         let def_id = self.r.local_def_id(item.id);
192         // Update effective visibilities of nested items.
193         // If it's a mod, also make the visitor walk all of its items
194         match item.kind {
195             // Resolved in rustc_privacy when types are available
196             ast::ItemKind::Impl(..) => return,
197
198             // Should be unreachable at this stage
199             ast::ItemKind::MacCall(..) => panic!(
200                 "ast::ItemKind::MacCall encountered, this should not anymore appear at this stage"
201             ),
202
203             ast::ItemKind::Mod(..) => {
204                 self.set_bindings_effective_visibilities(def_id);
205                 visit::walk_item(self, item);
206             }
207
208             ast::ItemKind::Enum(EnumDef { ref variants }, _) => {
209                 self.set_bindings_effective_visibilities(def_id);
210                 for variant in variants {
211                     let variant_def_id = self.r.local_def_id(variant.id);
212                     for field in variant.data.fields() {
213                         self.update(self.r.local_def_id(field.id), variant_def_id);
214                     }
215                 }
216             }
217
218             ast::ItemKind::Struct(ref def, _) | ast::ItemKind::Union(ref def, _) => {
219                 for field in def.fields() {
220                     self.update(self.r.local_def_id(field.id), def_id);
221                 }
222             }
223
224             ast::ItemKind::Trait(..) => {
225                 self.set_bindings_effective_visibilities(def_id);
226             }
227
228             ast::ItemKind::ExternCrate(..)
229             | ast::ItemKind::Use(..)
230             | ast::ItemKind::Static(..)
231             | ast::ItemKind::Const(..)
232             | ast::ItemKind::GlobalAsm(..)
233             | ast::ItemKind::TyAlias(..)
234             | ast::ItemKind::TraitAlias(..)
235             | ast::ItemKind::MacroDef(..)
236             | ast::ItemKind::ForeignMod(..)
237             | ast::ItemKind::Fn(..) => return,
238         }
239     }
240 }