@@ -9,106 +9,101 @@ use rustc_query_system::ich::StableHashingContext;
9
9
use rustc_span:: def_id:: { DefId , LocalDefId } ;
10
10
use std:: hash:: Hash ;
11
11
12
- /// Represents the levels of accessibility an item can have.
12
+ /// Represents the levels of effective visibility an item can have.
13
13
///
14
- /// The variants are sorted in ascending order of accessibility .
14
+ /// The variants are sorted in ascending order of directness .
15
15
#[ 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.
23
23
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 ,
28
28
}
29
29
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 ]
38
33
}
39
34
}
40
35
41
36
#[ derive( Clone , Copy , PartialEq , Eq , Debug , HashStable ) ]
42
37
pub struct EffectiveVisibility {
43
- public : Visibility ,
44
- exported : Visibility ,
38
+ direct : Visibility ,
39
+ reexported : Visibility ,
45
40
reachable : Visibility ,
46
- reachable_from_impl_trait : Visibility ,
41
+ reachable_through_impl_trait : Visibility ,
47
42
}
48
43
49
44
impl 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 ,
56
51
}
57
52
}
58
53
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 ,
65
60
}
66
61
}
67
62
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 ( )
70
65
}
71
66
72
67
pub fn from_vis ( vis : Visibility ) -> EffectiveVisibility {
73
68
EffectiveVisibility {
74
- public : vis,
75
- exported : vis,
69
+ direct : vis,
70
+ reexported : vis,
76
71
reachable : vis,
77
- reachable_from_impl_trait : vis,
72
+ reachable_through_impl_trait : vis,
78
73
}
79
74
}
80
75
}
81
76
82
- /// Holds a map of accessibility levels for reachable HIR nodes.
77
+ /// Holds a map of effective visibilities for reachable HIR nodes.
83
78
#[ derive( Debug , Clone ) ]
84
- pub struct AccessLevels < Id = LocalDefId > {
79
+ pub struct EffectiveVisibilities < Id = LocalDefId > {
85
80
map : FxHashMap < Id , EffectiveVisibility > ,
86
81
}
87
82
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 ) )
92
87
}
93
88
94
- /// See `AccessLevel ::Reachable`.
89
+ /// See `Level ::Reachable`.
95
90
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 )
97
92
}
98
93
99
- /// See `AccessLevel::Exported `.
94
+ /// See `Level::Reexported `.
100
95
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 )
102
97
}
103
98
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 )
107
102
}
108
103
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 ( ) {
112
107
if effective_vis. is_public_at_level ( level) {
113
108
return Some ( level) ;
114
109
}
@@ -117,38 +112,41 @@ impl<Id: Hash + Eq + Copy> AccessLevels<Id> {
117
112
} )
118
113
}
119
114
120
- pub fn get_effective_vis ( & self , id : Id ) -> Option < & EffectiveVisibility > {
115
+ pub fn effective_vis ( & self , id : Id ) -> Option < & EffectiveVisibility > {
121
116
self . map . get ( & id)
122
117
}
123
118
124
119
pub fn iter ( & self ) -> impl Iterator < Item = ( & Id , & EffectiveVisibility ) > {
125
120
self . map . iter ( )
126
121
}
127
122
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 ( ) }
130
128
}
131
129
132
- pub fn set_access_level (
130
+ pub fn set_public_at_level (
133
131
& mut self ,
134
132
id : Id ,
135
133
default_vis : impl FnOnce ( ) -> Visibility ,
136
- tag : AccessLevel ,
134
+ level : Level ,
137
135
) {
138
136
let mut effective_vis = self
139
- . get_effective_vis ( id)
137
+ . effective_vis ( id)
140
138
. copied ( )
141
139
. 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 ;
145
143
}
146
144
}
147
145
self . map . insert ( id, effective_vis) ;
148
146
}
149
147
}
150
148
151
- impl < Id : Hash + Eq + Copy + Into < DefId > > AccessLevels < Id > {
149
+ impl < Id : Hash + Eq + Copy + Into < DefId > > EffectiveVisibilities < Id > {
152
150
// `parent_id` is not necessarily a parent in source code tree,
153
151
// it is the node from which the maximum effective visibility is inherited.
154
152
pub fn update (
@@ -157,28 +155,29 @@ impl<Id: Hash + Eq + Copy + Into<DefId>> AccessLevels<Id> {
157
155
nominal_vis : Visibility ,
158
156
default_vis : impl FnOnce ( ) -> Visibility ,
159
157
parent_id : Id ,
160
- tag : AccessLevel ,
158
+ level : Level ,
161
159
tree : impl DefIdTree ,
162
160
) -> bool {
163
161
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 ( || {
165
163
if id. into ( ) . is_crate_root ( ) {
166
164
EffectiveVisibility :: from_vis ( Visibility :: Public )
167
165
} else {
168
166
EffectiveVisibility :: from_vis ( default_vis ( ) )
169
167
}
170
168
} ) ;
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) ;
173
172
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 ) ;
178
177
// effective visibility for id shouldn't be recalculated if
179
178
// inherited from parent_id effective visibility isn't changed at next level
180
179
if !( inherited_effective_vis_at_prev_level == inherited_effective_vis_at_level
181
- && tag != level )
180
+ && level != l )
182
181
{
183
182
calculated_effective_vis =
184
183
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> {
205
204
}
206
205
}
207
206
208
- impl < Id > Default for AccessLevels < Id > {
207
+ impl < Id > Default for EffectiveVisibilities < Id > {
209
208
fn default ( ) -> Self {
210
- AccessLevels { map : Default :: default ( ) }
209
+ EffectiveVisibilities { map : Default :: default ( ) }
211
210
}
212
211
}
213
212
214
- impl < ' a > HashStable < StableHashingContext < ' a > > for AccessLevels {
213
+ impl < ' a > HashStable < StableHashingContext < ' a > > for EffectiveVisibilities {
215
214
fn hash_stable ( & self , hcx : & mut StableHashingContext < ' a > , hasher : & mut StableHasher ) {
216
- let AccessLevels { ref map } = * self ;
215
+ let EffectiveVisibilities { ref map } = * self ;
217
216
map. hash_stable ( hcx, hasher) ;
218
217
}
219
218
}
0 commit comments