]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/privacy.rs
Rollup merge of #58306 - GuillaumeGomez:crate-browser-history, r=QuietMisdreavus
[rust.git] / src / librustc / 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 crate::util::nodemap::{DefIdSet, FxHashMap};
6
7 use std::hash::Hash;
8 use std::fmt;
9 use syntax::ast::NodeId;
10
11 // Accessibility levels, sorted in ascending order
12 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
13 pub enum AccessLevel {
14     /// Superset of `AccessLevel::Reachable` used to mark impl Trait items.
15     ReachableFromImplTrait,
16     /// Exported items + items participating in various kinds of public interfaces,
17     /// but not directly nameable. For example, if function `fn f() -> T {...}` is
18     /// public, then type `T` is reachable. Its values can be obtained by other crates
19     /// even if the type itself is not nameable.
20     Reachable,
21     /// Public items + items accessible to other crates with help of `pub use` re-exports
22     Exported,
23     /// Items accessible to other crates directly, without help of re-exports
24     Public,
25 }
26
27 // Accessibility levels for reachable HIR nodes
28 #[derive(Clone)]
29 pub struct AccessLevels<Id = NodeId> {
30     pub map: FxHashMap<Id, AccessLevel>
31 }
32
33 impl<Id: Hash + Eq> AccessLevels<Id> {
34     /// See `AccessLevel::Reachable`.
35     pub fn is_reachable(&self, id: Id) -> bool {
36         self.map.get(&id) >= Some(&AccessLevel::Reachable)
37     }
38
39     /// See `AccessLevel::Exported`.
40     pub fn is_exported(&self, id: Id) -> bool {
41         self.map.get(&id) >= Some(&AccessLevel::Exported)
42     }
43
44     /// See `AccessLevel::Public`.
45     pub fn is_public(&self, id: Id) -> bool {
46         self.map.get(&id) >= Some(&AccessLevel::Public)
47     }
48 }
49
50 impl<Id: Hash + Eq> Default for AccessLevels<Id> {
51     fn default() -> Self {
52         AccessLevels { map: Default::default() }
53     }
54 }
55
56 impl<Id: Hash + Eq + fmt::Debug> fmt::Debug for AccessLevels<Id> {
57     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58         fmt::Debug::fmt(&self.map, f)
59     }
60 }
61
62 /// A set containing all exported definitions from external crates.
63 /// The set does not contain any entries from local crates.
64 pub type ExternalExports = DefIdSet;