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.
5 use rustc_data_structures::fx::FxHashMap;
7 use rustc_macros::HashStable;
11 /// Represents the levels of accessibility an item can have.
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.
23 /// Public items + items accessible to other crates with the help of `pub use` re-exports.
25 /// Items accessible to other crates directly, without the help of re-exports.
29 /// Holds a map of accessibility levels for reachable HIR nodes.
31 pub struct AccessLevels<Id = HirId> {
32 pub map: FxHashMap<Id, AccessLevel>,
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)
41 /// See `AccessLevel::Exported`.
42 pub fn is_exported(&self, id: Id) -> bool {
43 self.map.get(&id) >= Some(&AccessLevel::Exported)
46 /// See `AccessLevel::Public`.
47 pub fn is_public(&self, id: Id) -> bool {
48 self.map.get(&id) >= Some(&AccessLevel::Public)
52 impl<Id: Hash + Eq> Default for AccessLevels<Id> {
53 fn default() -> Self {
54 AccessLevels { map: Default::default() }
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)