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 crate::util::nodemap::{DefIdSet, FxHashMap};
9 use syntax::ast::NodeId;
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.
21 /// Public items + items accessible to other crates with help of `pub use` re-exports
23 /// Items accessible to other crates directly, without help of re-exports
27 // Accessibility levels for reachable HIR nodes
29 pub struct AccessLevels<Id = NodeId> {
30 pub map: FxHashMap<Id, AccessLevel>
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)
39 /// See `AccessLevel::Exported`.
40 pub fn is_exported(&self, id: Id) -> bool {
41 self.map.get(&id) >= Some(&AccessLevel::Exported)
44 /// See `AccessLevel::Public`.
45 pub fn is_public(&self, id: Id) -> bool {
46 self.map.get(&id) >= Some(&AccessLevel::Public)
50 impl<Id: Hash + Eq> Default for AccessLevels<Id> {
51 fn default() -> Self {
52 AccessLevels { map: Default::default() }
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)
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;