@@ -9,106 +9,101 @@ use rustc_query_system::ich::StableHashingContext;
99use rustc_span:: def_id:: { DefId , LocalDefId } ;
1010use std:: hash:: Hash ;
1111
12- /// Represents the levels of accessibility an item can have.
12+ /// Represents the levels of effective visibility an item can have.
1313///
14- /// The variants are sorted in ascending order of accessibility .
14+ /// The variants are sorted in ascending order of directness .
1515#[ derive( Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord , HashStable ) ]
16- pub enum AccessLevel {
17- /// Superset of `AccessLevel:: Reachable` used to mark impl Trait items .
18- ReachableFromImplTrait ,
19- /// Exported items + items participating in various kinds of public interfaces,
20- /// but not directly nameable. For example, if function `fn f() -> T {...}` is
21- /// public, then type `T` is reachable. Its values can be obtained by other crates
22- /// even if the type itself is not nameable.
16+ pub enum Level {
17+ /// Superset of `Reachable` including items leaked through return position ` impl Trait` .
18+ ReachableThroughImplTrait ,
19+ /// Item is either reexported, or leaked through any kind of interface.
20+ /// For example, if function `fn f() -> T {...}` is directly public, then type `T` is publicly
21+ /// reachable and its values can be obtained by other crates even if the type itself is not
22+ /// nameable.
2323 Reachable ,
24- /// Public items + items accessible to other crates with the help of `pub use` re-exports .
25- Exported ,
26- /// Items accessible to other crates directly, without the help of re-exports .
27- Public ,
24+ /// Item is accessible either directly, or with help of `use` reexports .
25+ Reexported ,
26+ /// Item is directly accessible , without help of reexports .
27+ Direct ,
2828}
2929
30- impl AccessLevel {
31- pub fn all_levels ( ) -> [ AccessLevel ; 4 ] {
32- [
33- AccessLevel :: Public ,
34- AccessLevel :: Exported ,
35- AccessLevel :: Reachable ,
36- AccessLevel :: ReachableFromImplTrait ,
37- ]
30+ impl Level {
31+ pub fn all_levels ( ) -> [ Level ; 4 ] {
32+ [ Level :: Direct , Level :: Reexported , Level :: Reachable , Level :: ReachableThroughImplTrait ]
3833 }
3934}
4035
4136#[ derive( Clone , Copy , PartialEq , Eq , Debug , HashStable ) ]
4237pub struct EffectiveVisibility {
43- public : Visibility ,
44- exported : Visibility ,
38+ direct : Visibility ,
39+ reexported : Visibility ,
4540 reachable : Visibility ,
46- reachable_from_impl_trait : Visibility ,
41+ reachable_through_impl_trait : Visibility ,
4742}
4843
4944impl EffectiveVisibility {
50- pub fn get ( & self , tag : AccessLevel ) -> & Visibility {
51- match tag {
52- AccessLevel :: Public => & self . public ,
53- AccessLevel :: Exported => & self . exported ,
54- AccessLevel :: Reachable => & self . reachable ,
55- AccessLevel :: ReachableFromImplTrait => & self . reachable_from_impl_trait ,
45+ pub fn at_level ( & self , level : Level ) -> & Visibility {
46+ match level {
47+ Level :: Direct => & self . direct ,
48+ Level :: Reexported => & self . reexported ,
49+ Level :: Reachable => & self . reachable ,
50+ Level :: ReachableThroughImplTrait => & self . reachable_through_impl_trait ,
5651 }
5752 }
5853
59- fn get_mut ( & mut self , tag : AccessLevel ) -> & mut Visibility {
60- match tag {
61- AccessLevel :: Public => & mut self . public ,
62- AccessLevel :: Exported => & mut self . exported ,
63- AccessLevel :: Reachable => & mut self . reachable ,
64- AccessLevel :: ReachableFromImplTrait => & mut self . reachable_from_impl_trait ,
54+ fn at_level_mut ( & mut self , level : Level ) -> & mut Visibility {
55+ match level {
56+ Level :: Direct => & mut self . direct ,
57+ Level :: Reexported => & mut self . reexported ,
58+ Level :: Reachable => & mut self . reachable ,
59+ Level :: ReachableThroughImplTrait => & mut self . reachable_through_impl_trait ,
6560 }
6661 }
6762
68- pub fn is_public_at_level ( & self , tag : AccessLevel ) -> bool {
69- self . get ( tag ) . is_public ( )
63+ pub fn is_public_at_level ( & self , level : Level ) -> bool {
64+ self . at_level ( level ) . is_public ( )
7065 }
7166
7267 pub fn from_vis ( vis : Visibility ) -> EffectiveVisibility {
7368 EffectiveVisibility {
74- public : vis,
75- exported : vis,
69+ direct : vis,
70+ reexported : vis,
7671 reachable : vis,
77- reachable_from_impl_trait : vis,
72+ reachable_through_impl_trait : vis,
7873 }
7974 }
8075}
8176
82- /// Holds a map of accessibility levels for reachable HIR nodes.
77+ /// Holds a map of effective visibilities for reachable HIR nodes.
8378#[ derive( Debug , Clone ) ]
84- pub struct AccessLevels < Id = LocalDefId > {
79+ pub struct EffectiveVisibilities < Id = LocalDefId > {
8580 map : FxHashMap < Id , EffectiveVisibility > ,
8681}
8782
88- impl < Id : Hash + Eq + Copy > AccessLevels < Id > {
89- pub fn is_public_at_level ( & self , id : Id , tag : AccessLevel ) -> bool {
90- self . get_effective_vis ( id)
91- . map_or ( false , |effective_vis| effective_vis. is_public_at_level ( tag ) )
83+ impl < Id : Hash + Eq + Copy > EffectiveVisibilities < Id > {
84+ pub fn is_public_at_level ( & self , id : Id , level : Level ) -> bool {
85+ self . effective_vis ( id)
86+ . map_or ( false , |effective_vis| effective_vis. is_public_at_level ( level ) )
9287 }
9388
94- /// See `AccessLevel ::Reachable`.
89+ /// See `Level ::Reachable`.
9590 pub fn is_reachable ( & self , id : Id ) -> bool {
96- self . is_public_at_level ( id, AccessLevel :: Reachable )
91+ self . is_public_at_level ( id, Level :: Reachable )
9792 }
9893
99- /// See `AccessLevel::Exported `.
94+ /// See `Level::Reexported `.
10095 pub fn is_exported ( & self , id : Id ) -> bool {
101- self . is_public_at_level ( id, AccessLevel :: Exported )
96+ self . is_public_at_level ( id, Level :: Reexported )
10297 }
10398
104- /// See `AccessLevel::Public `.
105- pub fn is_public ( & self , id : Id ) -> bool {
106- self . is_public_at_level ( id, AccessLevel :: Public )
99+ /// See `Level::Direct `.
100+ pub fn is_directly_public ( & self , id : Id ) -> bool {
101+ self . is_public_at_level ( id, Level :: Direct )
107102 }
108103
109- pub fn get_access_level ( & self , id : Id ) -> Option < AccessLevel > {
110- self . get_effective_vis ( id) . and_then ( |effective_vis| {
111- for level in AccessLevel :: all_levels ( ) {
104+ pub fn public_at_level ( & self , id : Id ) -> Option < Level > {
105+ self . effective_vis ( id) . and_then ( |effective_vis| {
106+ for level in Level :: all_levels ( ) {
112107 if effective_vis. is_public_at_level ( level) {
113108 return Some ( level) ;
114109 }
@@ -117,38 +112,41 @@ impl<Id: Hash + Eq + Copy> AccessLevels<Id> {
117112 } )
118113 }
119114
120- pub fn get_effective_vis ( & self , id : Id ) -> Option < & EffectiveVisibility > {
115+ pub fn effective_vis ( & self , id : Id ) -> Option < & EffectiveVisibility > {
121116 self . map . get ( & id)
122117 }
123118
124119 pub fn iter ( & self ) -> impl Iterator < Item = ( & Id , & EffectiveVisibility ) > {
125120 self . map . iter ( )
126121 }
127122
128- pub fn map_id < OutId : Hash + Eq + Copy > ( & self , f : impl Fn ( Id ) -> OutId ) -> AccessLevels < OutId > {
129- AccessLevels { map : self . map . iter ( ) . map ( |( k, v) | ( f ( * k) , * v) ) . collect ( ) }
123+ pub fn map_id < OutId : Hash + Eq + Copy > (
124+ & self ,
125+ f : impl Fn ( Id ) -> OutId ,
126+ ) -> EffectiveVisibilities < OutId > {
127+ EffectiveVisibilities { map : self . map . iter ( ) . map ( |( k, v) | ( f ( * k) , * v) ) . collect ( ) }
130128 }
131129
132- pub fn set_access_level (
130+ pub fn set_public_at_level (
133131 & mut self ,
134132 id : Id ,
135133 default_vis : impl FnOnce ( ) -> Visibility ,
136- tag : AccessLevel ,
134+ level : Level ,
137135 ) {
138136 let mut effective_vis = self
139- . get_effective_vis ( id)
137+ . effective_vis ( id)
140138 . copied ( )
141139 . unwrap_or_else ( || EffectiveVisibility :: from_vis ( default_vis ( ) ) ) ;
142- for level in AccessLevel :: all_levels ( ) {
143- if level <= tag {
144- * effective_vis. get_mut ( level ) = Visibility :: Public ;
140+ for l in Level :: all_levels ( ) {
141+ if l <= level {
142+ * effective_vis. at_level_mut ( l ) = Visibility :: Public ;
145143 }
146144 }
147145 self . map . insert ( id, effective_vis) ;
148146 }
149147}
150148
151- impl < Id : Hash + Eq + Copy + Into < DefId > > AccessLevels < Id > {
149+ impl < Id : Hash + Eq + Copy + Into < DefId > > EffectiveVisibilities < Id > {
152150 // `parent_id` is not necessarily a parent in source code tree,
153151 // it is the node from which the maximum effective visibility is inherited.
154152 pub fn update (
@@ -157,28 +155,29 @@ impl<Id: Hash + Eq + Copy + Into<DefId>> AccessLevels<Id> {
157155 nominal_vis : Visibility ,
158156 default_vis : impl FnOnce ( ) -> Visibility ,
159157 parent_id : Id ,
160- tag : AccessLevel ,
158+ level : Level ,
161159 tree : impl DefIdTree ,
162160 ) -> bool {
163161 let mut changed = false ;
164- let mut current_effective_vis = self . get_effective_vis ( id) . copied ( ) . unwrap_or_else ( || {
162+ let mut current_effective_vis = self . effective_vis ( id) . copied ( ) . unwrap_or_else ( || {
165163 if id. into ( ) . is_crate_root ( ) {
166164 EffectiveVisibility :: from_vis ( Visibility :: Public )
167165 } else {
168166 EffectiveVisibility :: from_vis ( default_vis ( ) )
169167 }
170168 } ) ;
171- if let Some ( inherited_effective_vis) = self . get_effective_vis ( parent_id) {
172- let mut inherited_effective_vis_at_prev_level = * inherited_effective_vis. get ( tag) ;
169+ if let Some ( inherited_effective_vis) = self . effective_vis ( parent_id) {
170+ let mut inherited_effective_vis_at_prev_level =
171+ * inherited_effective_vis. at_level ( level) ;
173172 let mut calculated_effective_vis = inherited_effective_vis_at_prev_level;
174- for level in AccessLevel :: all_levels ( ) {
175- if tag >= level {
176- let inherited_effective_vis_at_level = * inherited_effective_vis. get ( level ) ;
177- let current_effective_vis_at_level = current_effective_vis. get_mut ( level ) ;
173+ for l in Level :: all_levels ( ) {
174+ if level >= l {
175+ let inherited_effective_vis_at_level = * inherited_effective_vis. at_level ( l ) ;
176+ let current_effective_vis_at_level = current_effective_vis. at_level_mut ( l ) ;
178177 // effective visibility for id shouldn't be recalculated if
179178 // inherited from parent_id effective visibility isn't changed at next level
180179 if !( inherited_effective_vis_at_prev_level == inherited_effective_vis_at_level
181- && tag != level )
180+ && level != l )
182181 {
183182 calculated_effective_vis =
184183 if nominal_vis. is_at_least ( inherited_effective_vis_at_level, tree) {
@@ -205,15 +204,15 @@ impl<Id: Hash + Eq + Copy + Into<DefId>> AccessLevels<Id> {
205204 }
206205}
207206
208- impl < Id > Default for AccessLevels < Id > {
207+ impl < Id > Default for EffectiveVisibilities < Id > {
209208 fn default ( ) -> Self {
210- AccessLevels { map : Default :: default ( ) }
209+ EffectiveVisibilities { map : Default :: default ( ) }
211210 }
212211}
213212
214- impl < ' a > HashStable < StableHashingContext < ' a > > for AccessLevels {
213+ impl < ' a > HashStable < StableHashingContext < ' a > > for EffectiveVisibilities {
215214 fn hash_stable ( & self , hcx : & mut StableHashingContext < ' a > , hasher : & mut StableHasher ) {
216- let AccessLevels { ref map } = * self ;
215+ let EffectiveVisibilities { ref map } = * self ;
217216 map. hash_stable ( hcx, hasher) ;
218217 }
219218}
0 commit comments