2013-08-08 11:38:10 -07:00
|
|
|
//! A pass that checks to make sure private fields and methods aren't used
|
2013-08-07 00:11:34 -07: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 10:47:17 -08:00
|
|
|
|
2019-12-24 05:02:53 +01:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::HirId;
|
2018-12-03 01:14:35 +01:00
|
|
|
use rustc_macros::HashStable;
|
2019-12-22 17:42:04 -05:00
|
|
|
use std::fmt;
|
|
|
|
|
use std::hash::Hash;
|
2015-11-19 14:16:35 +03:00
|
|
|
|
|
|
|
|
// Accessibility levels, sorted in ascending order
|
2018-12-03 01:14:35 +01:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, HashStable)]
|
2015-11-19 14:16:35 +03:00
|
|
|
pub enum AccessLevel {
|
2019-01-26 20:30:52 +01:00
|
|
|
/// Superset of `AccessLevel::Reachable` used to mark impl Trait items.
|
2018-08-21 00:11:59 +01:00
|
|
|
ReachableFromImplTrait,
|
2019-01-26 20:30:52 +01: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 14:16:35 +03:00
|
|
|
Reachable,
|
2019-01-26 20:30:52 +01:00
|
|
|
/// Public items + items accessible to other crates with help of `pub use` re-exports
|
2015-11-19 14:16:35 +03:00
|
|
|
Exported,
|
2019-01-26 20:30:52 +01:00
|
|
|
/// Items accessible to other crates directly, without help of re-exports
|
2015-11-19 14:16:35 +03:00
|
|
|
Public,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Accessibility levels for reachable HIR nodes
|
|
|
|
|
#[derive(Clone)]
|
2019-03-11 11:03:19 +01:00
|
|
|
pub struct AccessLevels<Id = HirId> {
|
2019-12-22 17:42:04 -05:00
|
|
|
pub map: FxHashMap<Id, AccessLevel>,
|
2015-11-19 14:16:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<Id: Hash + Eq> AccessLevels<Id> {
|
2019-01-26 20:30:52 +01:00
|
|
|
/// See `AccessLevel::Reachable`.
|
2015-11-19 14:16:35 +03:00
|
|
|
pub fn is_reachable(&self, id: Id) -> bool {
|
2018-08-21 00:11:59 +01:00
|
|
|
self.map.get(&id) >= Some(&AccessLevel::Reachable)
|
2015-11-19 14:16:35 +03:00
|
|
|
}
|
2019-01-26 20:30:52 +01:00
|
|
|
|
|
|
|
|
/// See `AccessLevel::Exported`.
|
2015-11-19 14:16:35 +03:00
|
|
|
pub fn is_exported(&self, id: Id) -> bool {
|
|
|
|
|
self.map.get(&id) >= Some(&AccessLevel::Exported)
|
|
|
|
|
}
|
2019-01-26 20:30:52 +01:00
|
|
|
|
|
|
|
|
/// See `AccessLevel::Public`.
|
2015-11-19 14:16:35 +03:00
|
|
|
pub fn is_public(&self, id: Id) -> bool {
|
|
|
|
|
self.map.get(&id) >= Some(&AccessLevel::Public)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<Id: Hash + Eq> Default for AccessLevels<Id> {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
AccessLevels { map: Default::default() }
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-09 21:16:51 -07:00
|
|
|
|
2016-04-07 05:59:02 +02:00
|
|
|
impl<Id: Hash + Eq + fmt::Debug> fmt::Debug for AccessLevels<Id> {
|
2018-08-29 22:02:42 -07:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-04-07 05:59:02 +02:00
|
|
|
fmt::Debug::fmt(&self.map, f)
|
|
|
|
|
}
|
|
|
|
|
}
|