]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/middle/privacy.rs
Re-use std::sealed::Sealed in os/linux/process.
[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
5 use rustc_data_structures::fx::FxHashMap;
6 use rustc_hir::HirId;
7 use rustc_macros::HashStable;
8 use std::fmt;
9 use std::hash::Hash;
10
11 /// Represents the levels of accessibility an item can have.
12 ///
13 /// The variants are sorted in ascending order of accessibility.
14 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, HashStable)]
15 pub enum AccessLevel {
16     /// Superset of `AccessLevel::Reachable` used to mark impl Trait items.
17     ReachableFromImplTrait,
18     /// Exported items + items participating in various kinds of public interfaces,
19     /// but not directly nameable. For example, if function `fn f() -> T {...}` is
20     /// public, then type `T` is reachable. Its values can be obtained by other crates
21     /// even if the type itself is not nameable.
22     Reachable,
23     /// Public items + items accessible to other crates with the help of `pub use` re-exports.
24     Exported,
25     /// Items accessible to other crates directly, without the help of re-exports.
26     Public,
27 }
28
29 /// Holds a map of accessibility levels for reachable HIR nodes.
30 #[derive(Clone)]
31 pub struct AccessLevels<Id = HirId> {
32     pub map: FxHashMap<Id, AccessLevel>,
33 }
34
35 impl<Id: Hash + Eq> AccessLevels<Id> {
36     /// See `AccessLevel::Reachable`.
37     pub fn is_reachable(&self, id: Id) -> bool {
38         self.map.get(&id) >= Some(&AccessLevel::Reachable)
39     }
40
41     /// See `AccessLevel::Exported`.
42     pub fn is_exported(&self, id: Id) -> bool {
43         self.map.get(&id) >= Some(&AccessLevel::Exported)
44     }
45
46     /// See `AccessLevel::Public`.
47     pub fn is_public(&self, id: Id) -> bool {
48         self.map.get(&id) >= Some(&AccessLevel::Public)
49     }
50 }
51
52 impl<Id: Hash + Eq> Default for AccessLevels<Id> {
53     fn default() -> Self {
54         AccessLevels { map: Default::default() }
55     }
56 }
57
58 impl<Id: Hash + Eq + fmt::Debug> fmt::Debug for AccessLevels<Id> {
59     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60         fmt::Debug::fmt(&self.map, f)
61     }
62 }