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