2013-08-08 18:38:10 +00:00
|
|
|
//! A pass that checks to make sure private fields and methods aren't used
|
2013-08-07 07:11:34 +00:00
|
|
|
//! outside their scopes. This pass will also generate a set of exported items
|
|
|
|
//! which are available for use externally when compiled as a library.
|
2015-01-15 18:47:17 +00:00
|
|
|
|
2019-12-24 04:02:53 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-11-14 15:35:31 +00:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2018-12-03 00:14:35 +00:00
|
|
|
use rustc_macros::HashStable;
|
2022-04-04 20:19:25 +00:00
|
|
|
use rustc_query_system::ich::StableHashingContext;
|
2021-07-28 15:23:40 +00:00
|
|
|
use rustc_span::def_id::LocalDefId;
|
2015-11-19 11:16:35 +00:00
|
|
|
use std::hash::Hash;
|
|
|
|
|
2020-12-22 04:00:18 +00:00
|
|
|
/// Represents the levels of accessibility an item can have.
|
|
|
|
///
|
|
|
|
/// The variants are sorted in ascending order of accessibility.
|
2018-12-03 00:14:35 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, HashStable)]
|
2015-11-19 11:16:35 +00:00
|
|
|
pub enum AccessLevel {
|
2019-01-26 19:30:52 +00:00
|
|
|
/// Superset of `AccessLevel::Reachable` used to mark impl Trait items.
|
2018-08-20 23:11:59 +00:00
|
|
|
ReachableFromImplTrait,
|
2019-01-26 19:30:52 +00:00
|
|
|
/// Exported items + items participating in various kinds of public interfaces,
|
|
|
|
/// but not directly nameable. For example, if function `fn f() -> T {...}` is
|
|
|
|
/// public, then type `T` is reachable. Its values can be obtained by other crates
|
|
|
|
/// even if the type itself is not nameable.
|
2015-11-19 11:16:35 +00:00
|
|
|
Reachable,
|
2020-12-22 04:00:18 +00:00
|
|
|
/// Public items + items accessible to other crates with the help of `pub use` re-exports.
|
2015-11-19 11:16:35 +00:00
|
|
|
Exported,
|
2020-12-22 04:00:18 +00:00
|
|
|
/// Items accessible to other crates directly, without the help of re-exports.
|
2015-11-19 11:16:35 +00:00
|
|
|
Public,
|
|
|
|
}
|
|
|
|
|
2020-12-22 04:00:18 +00:00
|
|
|
/// Holds a map of accessibility levels for reachable HIR nodes.
|
2021-07-26 03:38:16 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2021-07-28 15:23:40 +00:00
|
|
|
pub struct AccessLevels<Id = LocalDefId> {
|
2016-11-08 03:02:55 +00:00
|
|
|
pub map: FxHashMap<Id, AccessLevel>,
|
2015-11-19 11:16:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<Id: Hash + Eq> AccessLevels<Id> {
|
2019-01-26 19:30:52 +00:00
|
|
|
/// See `AccessLevel::Reachable`.
|
2015-11-19 11:16:35 +00:00
|
|
|
pub fn is_reachable(&self, id: Id) -> bool {
|
2018-08-20 23:11:59 +00:00
|
|
|
self.map.get(&id) >= Some(&AccessLevel::Reachable)
|
2015-11-19 11:16:35 +00:00
|
|
|
}
|
2019-01-26 19:30:52 +00:00
|
|
|
|
|
|
|
/// See `AccessLevel::Exported`.
|
2015-11-19 11:16:35 +00:00
|
|
|
pub fn is_exported(&self, id: Id) -> bool {
|
|
|
|
self.map.get(&id) >= Some(&AccessLevel::Exported)
|
|
|
|
}
|
2019-01-26 19:30:52 +00:00
|
|
|
|
|
|
|
/// See `AccessLevel::Public`.
|
2015-11-19 11:16:35 +00:00
|
|
|
pub fn is_public(&self, id: Id) -> bool {
|
|
|
|
self.map.get(&id) >= Some(&AccessLevel::Public)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:23:40 +00:00
|
|
|
impl<Id> Default for AccessLevels<Id> {
|
2015-11-19 11:16:35 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
AccessLevels { map: Default::default() }
|
|
|
|
}
|
|
|
|
}
|
2020-11-14 15:35:31 +00:00
|
|
|
|
|
|
|
impl<'a> HashStable<StableHashingContext<'a>> for AccessLevels {
|
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2022-04-04 20:19:25 +00:00
|
|
|
let AccessLevels { ref map } = *self;
|
|
|
|
map.hash_stable(hcx, hasher);
|
2020-11-14 15:35:31 +00:00
|
|
|
}
|
|
|
|
}
|