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;
12 /// Represents the levels of accessibility an item can have.
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.
24 /// Public items + items accessible to other crates with the help of `pub use` re-exports.
26 /// Items accessible to other crates directly, without the help of re-exports.
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>,
38 impl EffectiveVisibility {
39 pub fn get(&self, tag: AccessLevel) -> Option<&Visibility> {
41 AccessLevel::Public => &self.public,
42 AccessLevel::Exported => &self.exported,
43 AccessLevel::Reachable => &self.reachable,
44 AccessLevel::ReachableFromImplTrait => &self.reachable_from_impl_trait,
49 fn get_mut(&mut self, tag: AccessLevel) -> &mut Option<Visibility> {
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,
58 pub fn is_public_at_level(&self, tag: AccessLevel) -> bool {
59 self.get(tag).map_or(false, |vis| vis.is_public())
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>,
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))
75 /// See `AccessLevel::Reachable`.
76 pub fn is_reachable(&self, id: Id) -> bool {
77 self.is_public_at_level(id, AccessLevel::Reachable)
80 /// See `AccessLevel::Exported`.
81 pub fn is_exported(&self, id: Id) -> bool {
82 self.is_public_at_level(id, AccessLevel::Exported)
85 /// See `AccessLevel::Public`.
86 pub fn is_public(&self, id: Id) -> bool {
87 self.is_public_at_level(id, AccessLevel::Public)
90 pub fn get_access_level(&self, id: Id) -> Option<AccessLevel> {
91 self.get_effective_vis(id).and_then(|effective_vis| {
94 AccessLevel::Exported,
95 AccessLevel::Reachable,
96 AccessLevel::ReachableFromImplTrait,
98 if effective_vis.is_public_at_level(level) {
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();
110 AccessLevel::Exported,
111 AccessLevel::Reachable,
112 AccessLevel::ReachableFromImplTrait,
115 *effective_vis.get_mut(level) = Some(Visibility::Public);
118 self.map.insert(id, effective_vis);
121 pub fn get_effective_vis(&self, id: Id) -> Option<&EffectiveVisibility> {
125 pub fn iter(&self) -> impl Iterator<Item = (&Id, &EffectiveVisibility)> {
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() }
134 impl<Id> Default for AccessLevels<Id> {
135 fn default() -> Self {
136 AccessLevels { map: Default::default() }
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);