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
;
6 use rustc_hir
::def_id
::DefIdSet
;
8 use rustc_macros
::HashStable
;
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.
22 /// Public items + items accessible to other crates with help of `pub use` re-exports
24 /// Items accessible to other crates directly, without help of re-exports
28 // Accessibility levels for reachable HIR nodes
30 pub struct AccessLevels
<Id
= HirId
> {
31 pub map
: FxHashMap
<Id
, AccessLevel
>,
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
)
40 /// See `AccessLevel::Exported`.
41 pub fn is_exported(&self, id
: Id
) -> bool
{
42 self.map
.get(&id
) >= Some(&AccessLevel
::Exported
)
45 /// See `AccessLevel::Public`.
46 pub fn is_public(&self, id
: Id
) -> bool
{
47 self.map
.get(&id
) >= Some(&AccessLevel
::Public
)
51 impl<Id
: Hash
+ Eq
> Default
for AccessLevels
<Id
> {
52 fn default() -> Self {
53 AccessLevels { map: Default::default() }
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
)
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
;