]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/middle/privacy.rs
Auto merge of #102655 - joboet:windows_tls_opt, r=ChrisDenton
[rust.git] / compiler / rustc_middle / src / middle / privacy.rs
1 //! A pass that checks to make sure private fields and methods aren't used
2 //! outside their scopes. This pass will also generate a set of exported items
3 //! which are available for use externally when compiled as a library.
4 use crate::ty::Visibility;
5 use rustc_data_structures::fx::FxHashMap;
6 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
7 use rustc_macros::HashStable;
8 use rustc_query_system::ich::StableHashingContext;
9 use rustc_span::def_id::LocalDefId;
10 use std::hash::Hash;
11
12 /// Represents the levels of accessibility an item can have.
13 ///
14 /// The variants are sorted in ascending order of accessibility.
15 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, HashStable)]
16 pub enum AccessLevel {
17     /// Superset of `AccessLevel::Reachable` used to mark impl Trait items.
18     ReachableFromImplTrait,
19     /// Exported items + items participating in various kinds of public interfaces,
20     /// but not directly nameable. For example, if function `fn f() -> T {...}` is
21     /// public, then type `T` is reachable. Its values can be obtained by other crates
22     /// even if the type itself is not nameable.
23     Reachable,
24     /// Public items + items accessible to other crates with the help of `pub use` re-exports.
25     Exported,
26     /// Items accessible to other crates directly, without the help of re-exports.
27     Public,
28 }
29
30 #[derive(Clone, Copy, PartialEq, Eq, Debug, HashStable, Default)]
31 pub struct EffectiveVisibility {
32     public: Option<Visibility>,
33     exported: Option<Visibility>,
34     reachable: Option<Visibility>,
35     reachable_from_impl_trait: Option<Visibility>,
36 }
37
38 impl EffectiveVisibility {
39     pub fn get(&self, tag: AccessLevel) -> Option<&Visibility> {
40         match tag {
41             AccessLevel::Public => &self.public,
42             AccessLevel::Exported => &self.exported,
43             AccessLevel::Reachable => &self.reachable,
44             AccessLevel::ReachableFromImplTrait => &self.reachable_from_impl_trait,
45         }
46         .as_ref()
47     }
48
49     fn get_mut(&mut self, tag: AccessLevel) -> &mut Option<Visibility> {
50         match tag {
51             AccessLevel::Public => &mut self.public,
52             AccessLevel::Exported => &mut self.exported,
53             AccessLevel::Reachable => &mut self.reachable,
54             AccessLevel::ReachableFromImplTrait => &mut self.reachable_from_impl_trait,
55         }
56     }
57
58     pub fn is_public_at_level(&self, tag: AccessLevel) -> bool {
59         self.get(tag).map_or(false, |vis| vis.is_public())
60     }
61 }
62
63 /// Holds a map of accessibility levels for reachable HIR nodes.
64 #[derive(Debug, Clone)]
65 pub struct AccessLevels<Id = LocalDefId> {
66     map: FxHashMap<Id, EffectiveVisibility>,
67 }
68
69 impl<Id: Hash + Eq + Copy> AccessLevels<Id> {
70     pub fn is_public_at_level(&self, id: Id, tag: AccessLevel) -> bool {
71         self.get_effective_vis(id)
72             .map_or(false, |effective_vis| effective_vis.is_public_at_level(tag))
73     }
74
75     /// See `AccessLevel::Reachable`.
76     pub fn is_reachable(&self, id: Id) -> bool {
77         self.is_public_at_level(id, AccessLevel::Reachable)
78     }
79
80     /// See `AccessLevel::Exported`.
81     pub fn is_exported(&self, id: Id) -> bool {
82         self.is_public_at_level(id, AccessLevel::Exported)
83     }
84
85     /// See `AccessLevel::Public`.
86     pub fn is_public(&self, id: Id) -> bool {
87         self.is_public_at_level(id, AccessLevel::Public)
88     }
89
90     pub fn get_access_level(&self, id: Id) -> Option<AccessLevel> {
91         self.get_effective_vis(id).and_then(|effective_vis| {
92             for level in [
93                 AccessLevel::Public,
94                 AccessLevel::Exported,
95                 AccessLevel::Reachable,
96                 AccessLevel::ReachableFromImplTrait,
97             ] {
98                 if effective_vis.is_public_at_level(level) {
99                     return Some(level);
100                 }
101             }
102             None
103         })
104     }
105
106     pub fn set_access_level(&mut self, id: Id, tag: AccessLevel) {
107         let mut effective_vis = self.get_effective_vis(id).copied().unwrap_or_default();
108         for level in [
109             AccessLevel::Public,
110             AccessLevel::Exported,
111             AccessLevel::Reachable,
112             AccessLevel::ReachableFromImplTrait,
113         ] {
114             if level <= tag {
115                 *effective_vis.get_mut(level) = Some(Visibility::Public);
116             }
117         }
118         self.map.insert(id, effective_vis);
119     }
120
121     pub fn get_effective_vis(&self, id: Id) -> Option<&EffectiveVisibility> {
122         self.map.get(&id)
123     }
124
125     pub fn iter(&self) -> impl Iterator<Item = (&Id, &EffectiveVisibility)> {
126         self.map.iter()
127     }
128
129     pub fn map_id<OutId: Hash + Eq + Copy>(&self, f: impl Fn(Id) -> OutId) -> AccessLevels<OutId> {
130         AccessLevels { map: self.map.iter().map(|(k, v)| (f(*k), *v)).collect() }
131     }
132 }
133
134 impl<Id> Default for AccessLevels<Id> {
135     fn default() -> Self {
136         AccessLevels { map: Default::default() }
137     }
138 }
139
140 impl<'a> HashStable<StableHashingContext<'a>> for AccessLevels {
141     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
142         let AccessLevels { ref map } = *self;
143         map.hash_stable(hcx, hasher);
144     }
145 }