10
10
11
11
use std:: mem;
12
12
use rustc_data_structures:: small_vec:: SmallVec ;
13
- use syntax:: ast:: { CRATE_NODE_ID , NodeId } ;
13
+ use syntax:: ast:: CRATE_NODE_ID ;
14
14
use util:: nodemap:: FxHashSet ;
15
15
use ty:: context:: TyCtxt ;
16
16
use ty:: { AdtDef , VariantDef , FieldDef , TyS } ;
17
17
use ty:: { DefId , Substs } ;
18
- use ty:: { AdtKind , Visibility , NodeIdTree } ;
18
+ use ty:: { AdtKind , Visibility , DefIdTree } ;
19
19
use ty:: TypeVariants :: * ;
20
20
21
- /// Represents a set of nodes closed under the ancestor relation. That is, if a
22
- /// node is in this set then so are all its descendants.
21
+ /// Represents a set of DefIds closed under the ancestor relation. That is, if
22
+ /// a DefId is in this set then so are all its descendants.
23
23
#[ derive( Clone ) ]
24
- pub struct NodeForrest {
25
- /// The minimal set of nodes required to represent the whole set.
26
- /// If A and B are nodes in the NodeForrest , and A is a desecendant
27
- /// of B, then only B will be in root_nodes .
24
+ pub struct DefIdForrest {
25
+ /// The minimal set of DefIds required to represent the whole set.
26
+ /// If A and B are DefIds in the DefIdForrest , and A is a desecendant
27
+ /// of B, then only B will be in root_ids .
28
28
/// We use a SmallVec here because (for its use in this module) its rare
29
- /// that this will contain more than one or two nodes .
30
- root_nodes : SmallVec < [ NodeId ; 1 ] > ,
29
+ /// that this will contain even two ids .
30
+ root_ids : SmallVec < [ DefId ; 1 ] > ,
31
31
}
32
32
33
- impl < ' a , ' gcx , ' tcx > NodeForrest {
34
- /// Create an empty set .
35
- pub fn empty ( ) -> NodeForrest {
36
- NodeForrest {
37
- root_nodes : SmallVec :: new ( ) ,
33
+ impl < ' a , ' gcx , ' tcx > DefIdForrest {
34
+ /// Create an empty forrest .
35
+ pub fn empty ( ) -> DefIdForrest {
36
+ DefIdForrest {
37
+ root_ids : SmallVec :: new ( ) ,
38
38
}
39
39
}
40
40
41
- /// Create a set containing every node.
41
+ /// Create a forrest consisting of a single tree representing the entire
42
+ /// crate.
42
43
#[ inline]
43
- pub fn full ( ) -> NodeForrest {
44
- NodeForrest :: from_node ( CRATE_NODE_ID )
44
+ pub fn full ( tcx : TyCtxt < ' a , ' gcx , ' tcx > ) -> DefIdForrest {
45
+ let crate_id = tcx. map . local_def_id ( CRATE_NODE_ID ) ;
46
+ DefIdForrest :: from_id ( crate_id)
45
47
}
46
48
47
- /// Create a set containing a node and all its descendants.
48
- pub fn from_node ( node : NodeId ) -> NodeForrest {
49
- let mut root_nodes = SmallVec :: new ( ) ;
50
- root_nodes . push ( node ) ;
51
- NodeForrest {
52
- root_nodes : root_nodes ,
49
+ /// Create a forrest containing a DefId and all its descendants.
50
+ pub fn from_id ( id : DefId ) -> DefIdForrest {
51
+ let mut root_ids = SmallVec :: new ( ) ;
52
+ root_ids . push ( id ) ;
53
+ DefIdForrest {
54
+ root_ids : root_ids ,
53
55
}
54
56
}
55
57
56
- /// Test whether the set is empty.
58
+ /// Test whether the forrest is empty.
57
59
pub fn is_empty ( & self ) -> bool {
58
- self . root_nodes . is_empty ( )
60
+ self . root_ids . is_empty ( )
59
61
}
60
62
61
- /// Test whether the set conains a node .
63
+ /// Test whether the forrest conains a given DefId .
62
64
pub fn contains ( & self ,
63
65
tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
64
- node : NodeId ) -> bool
66
+ id : DefId ) -> bool
65
67
{
66
- for root_node in self . root_nodes . iter ( ) {
67
- if tcx. map . is_descendant_of ( node , * root_node ) {
68
+ for root_id in self . root_ids . iter ( ) {
69
+ if tcx. is_descendant_of ( id , * root_id ) {
68
70
return true ;
69
71
}
70
72
}
71
73
false
72
74
}
73
75
74
- /// Calculate the intersection of a collection of sets .
76
+ /// Calculate the intersection of a collection of forrests .
75
77
pub fn intersection < I > ( tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
76
- iter : I ) -> NodeForrest
77
- where I : IntoIterator < Item =NodeForrest >
78
+ iter : I ) -> DefIdForrest
79
+ where I : IntoIterator < Item =DefIdForrest >
78
80
{
79
- let mut ret = NodeForrest :: full ( ) ;
81
+ let mut ret = DefIdForrest :: full ( tcx ) ;
80
82
let mut next_ret = SmallVec :: new ( ) ;
81
- let mut old_ret: SmallVec < [ NodeId ; 1 ] > = SmallVec :: new ( ) ;
82
- for next_set in iter {
83
- for node in ret. root_nodes . drain ( ..) {
84
- if next_set . contains ( tcx, node ) {
85
- next_ret. push ( node ) ;
83
+ let mut old_ret: SmallVec < [ DefId ; 1 ] > = SmallVec :: new ( ) ;
84
+ for next_forrest in iter {
85
+ for id in ret. root_ids . drain ( ..) {
86
+ if next_forrest . contains ( tcx, id ) {
87
+ next_ret. push ( id ) ;
86
88
} else {
87
- old_ret. push ( node ) ;
89
+ old_ret. push ( id ) ;
88
90
}
89
91
}
90
- ret. root_nodes . extend ( old_ret. drain ( ..) ) ;
92
+ ret. root_ids . extend ( old_ret. drain ( ..) ) ;
91
93
92
- for node in next_set . root_nodes {
93
- if ret. contains ( tcx, node ) {
94
- next_ret. push ( node ) ;
94
+ for id in next_forrest . root_ids {
95
+ if ret. contains ( tcx, id ) {
96
+ next_ret. push ( id ) ;
95
97
}
96
98
}
97
99
98
- mem:: swap ( & mut next_ret, & mut ret. root_nodes ) ;
100
+ mem:: swap ( & mut next_ret, & mut ret. root_ids ) ;
99
101
next_ret. drain ( ..) ;
100
102
}
101
103
ret
102
104
}
103
105
104
- /// Calculate the union of a collection of sets .
106
+ /// Calculate the union of a collection of forrests .
105
107
pub fn union < I > ( tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
106
- iter : I ) -> NodeForrest
107
- where I : IntoIterator < Item =NodeForrest >
108
+ iter : I ) -> DefIdForrest
109
+ where I : IntoIterator < Item =DefIdForrest >
108
110
{
109
- let mut ret = NodeForrest :: empty ( ) ;
111
+ let mut ret = DefIdForrest :: empty ( ) ;
110
112
let mut next_ret = SmallVec :: new ( ) ;
111
- for next_set in iter {
112
- for node in ret. root_nodes . drain ( ..) {
113
- if !next_set . contains ( tcx, node ) {
114
- next_ret. push ( node ) ;
113
+ for next_forrest in iter {
114
+ for id in ret. root_ids . drain ( ..) {
115
+ if !next_forrest . contains ( tcx, id ) {
116
+ next_ret. push ( id ) ;
115
117
}
116
118
}
117
119
118
- for node in next_set . root_nodes {
119
- if !next_ret. contains ( & node ) {
120
- next_ret. push ( node ) ;
120
+ for id in next_forrest . root_ids {
121
+ if !next_ret. contains ( & id ) {
122
+ next_ret. push ( id ) ;
121
123
}
122
124
}
123
125
124
- mem:: swap ( & mut next_ret, & mut ret. root_nodes ) ;
126
+ mem:: swap ( & mut next_ret, & mut ret. root_ids ) ;
125
127
next_ret. drain ( ..) ;
126
128
}
127
129
ret
128
130
}
129
131
}
130
132
131
133
impl < ' a , ' gcx , ' tcx > AdtDef {
132
- /// Calculate the set of nodes from which this adt is visibly uninhabited.
134
+ /// Calculate the forrest of DefIds from which this adt is visibly uninhabited.
133
135
pub fn uninhabited_from (
134
136
& self ,
135
137
visited : & mut FxHashSet < ( DefId , & ' tcx Substs < ' tcx > ) > ,
136
138
tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
137
- substs : & ' tcx Substs < ' tcx > ) -> NodeForrest
139
+ substs : & ' tcx Substs < ' tcx > ) -> DefIdForrest
138
140
{
139
141
if !visited. insert ( ( self . did , substs) ) {
140
- return NodeForrest :: empty ( ) ;
142
+ return DefIdForrest :: empty ( ) ;
141
143
}
142
144
143
- let ret = NodeForrest :: intersection ( tcx, self . variants . iter ( ) . map ( |v| {
145
+ let ret = DefIdForrest :: intersection ( tcx, self . variants . iter ( ) . map ( |v| {
144
146
v. uninhabited_from ( visited, tcx, substs, self . adt_kind ( ) )
145
147
} ) ) ;
146
148
visited. remove ( & ( self . did , substs) ) ;
@@ -149,27 +151,27 @@ impl<'a, 'gcx, 'tcx> AdtDef {
149
151
}
150
152
151
153
impl < ' a , ' gcx , ' tcx > VariantDef {
152
- /// Calculate the set of nodes from which this variant is visibly uninhabited.
154
+ /// Calculate the forrest of DefIds from which this variant is visibly uninhabited.
153
155
pub fn uninhabited_from (
154
156
& self ,
155
157
visited : & mut FxHashSet < ( DefId , & ' tcx Substs < ' tcx > ) > ,
156
158
tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
157
159
substs : & ' tcx Substs < ' tcx > ,
158
- adt_kind : AdtKind ) -> NodeForrest
160
+ adt_kind : AdtKind ) -> DefIdForrest
159
161
{
160
162
match adt_kind {
161
163
AdtKind :: Union => {
162
- NodeForrest :: intersection ( tcx, self . fields . iter ( ) . map ( |f| {
164
+ DefIdForrest :: intersection ( tcx, self . fields . iter ( ) . map ( |f| {
163
165
f. uninhabited_from ( visited, tcx, substs, false )
164
166
} ) )
165
167
} ,
166
168
AdtKind :: Struct => {
167
- NodeForrest :: union ( tcx, self . fields . iter ( ) . map ( |f| {
169
+ DefIdForrest :: union ( tcx, self . fields . iter ( ) . map ( |f| {
168
170
f. uninhabited_from ( visited, tcx, substs, false )
169
171
} ) )
170
172
} ,
171
173
AdtKind :: Enum => {
172
- NodeForrest :: union ( tcx, self . fields . iter ( ) . map ( |f| {
174
+ DefIdForrest :: union ( tcx, self . fields . iter ( ) . map ( |f| {
173
175
f. uninhabited_from ( visited, tcx, substs, true )
174
176
} ) )
175
177
} ,
@@ -178,24 +180,24 @@ impl<'a, 'gcx, 'tcx> VariantDef {
178
180
}
179
181
180
182
impl < ' a , ' gcx , ' tcx > FieldDef {
181
- /// Calculate the set of nodes from which this field is visibly uninhabited.
183
+ /// Calculate the forrest of DefIds from which this field is visibly uninhabited.
182
184
pub fn uninhabited_from (
183
185
& self ,
184
186
visited : & mut FxHashSet < ( DefId , & ' tcx Substs < ' tcx > ) > ,
185
187
tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
186
188
substs : & ' tcx Substs < ' tcx > ,
187
- is_enum : bool ) -> NodeForrest
189
+ is_enum : bool ) -> DefIdForrest
188
190
{
189
191
let mut data_uninhabitedness = move || self . ty ( tcx, substs) . uninhabited_from ( visited, tcx) ;
190
192
if is_enum {
191
193
data_uninhabitedness ( )
192
194
} else {
193
195
match self . vis {
194
- Visibility :: PrivateExternal => NodeForrest :: empty ( ) ,
196
+ Visibility :: Invisible => DefIdForrest :: empty ( ) ,
195
197
Visibility :: Restricted ( from) => {
196
- let node_set = NodeForrest :: from_node ( from) ;
197
- let iter = Some ( node_set ) . into_iter ( ) . chain ( Some ( data_uninhabitedness ( ) ) ) ;
198
- NodeForrest :: intersection ( tcx, iter)
198
+ let forrest = DefIdForrest :: from_id ( from) ;
199
+ let iter = Some ( forrest ) . into_iter ( ) . chain ( Some ( data_uninhabitedness ( ) ) ) ;
200
+ DefIdForrest :: intersection ( tcx, iter)
199
201
} ,
200
202
Visibility :: Public => data_uninhabitedness ( ) ,
201
203
}
@@ -204,58 +206,58 @@ impl<'a, 'gcx, 'tcx> FieldDef {
204
206
}
205
207
206
208
impl < ' a , ' gcx , ' tcx > TyS < ' tcx > {
207
- /// Calculate the set of nodes from which this type is visibly uninhabited.
209
+ /// Calculate the forrest of DefIds from which this type is visibly uninhabited.
208
210
pub fn uninhabited_from (
209
211
& self ,
210
212
visited : & mut FxHashSet < ( DefId , & ' tcx Substs < ' tcx > ) > ,
211
- tcx : TyCtxt < ' a , ' gcx , ' tcx > ) -> NodeForrest
213
+ tcx : TyCtxt < ' a , ' gcx , ' tcx > ) -> DefIdForrest
212
214
{
213
215
match tcx. lift_to_global ( & self ) {
214
216
Some ( global_ty) => {
215
217
{
216
218
let cache = tcx. inhabitedness_cache . borrow ( ) ;
217
- if let Some ( closed_node_set ) = cache. get ( & global_ty) {
218
- return closed_node_set . clone ( ) ;
219
+ if let Some ( forrest ) = cache. get ( & global_ty) {
220
+ return forrest . clone ( ) ;
219
221
}
220
222
}
221
- let node_set = global_ty. uninhabited_from_inner ( visited, tcx) ;
223
+ let forrest = global_ty. uninhabited_from_inner ( visited, tcx) ;
222
224
let mut cache = tcx. inhabitedness_cache . borrow_mut ( ) ;
223
- cache. insert ( global_ty, node_set . clone ( ) ) ;
224
- node_set
225
+ cache. insert ( global_ty, forrest . clone ( ) ) ;
226
+ forrest
225
227
} ,
226
228
None => {
227
- let node_set = self . uninhabited_from_inner ( visited, tcx) ;
228
- node_set
229
+ let forrest = self . uninhabited_from_inner ( visited, tcx) ;
230
+ forrest
229
231
} ,
230
232
}
231
233
}
232
234
233
235
fn uninhabited_from_inner (
234
236
& self ,
235
237
visited : & mut FxHashSet < ( DefId , & ' tcx Substs < ' tcx > ) > ,
236
- tcx : TyCtxt < ' a , ' gcx , ' tcx > ) -> NodeForrest
238
+ tcx : TyCtxt < ' a , ' gcx , ' tcx > ) -> DefIdForrest
237
239
{
238
240
match self . sty {
239
241
TyAdt ( def, substs) => {
240
242
def. uninhabited_from ( visited, tcx, substs)
241
243
} ,
242
244
243
- TyNever => NodeForrest :: full ( ) ,
245
+ TyNever => DefIdForrest :: full ( tcx ) ,
244
246
TyTuple ( ref tys) => {
245
- NodeForrest :: union ( tcx, tys. iter ( ) . map ( |ty| {
247
+ DefIdForrest :: union ( tcx, tys. iter ( ) . map ( |ty| {
246
248
ty. uninhabited_from ( visited, tcx)
247
249
} ) )
248
250
} ,
249
251
TyArray ( ty, len) => {
250
252
if len == 0 {
251
- NodeForrest :: empty ( )
253
+ DefIdForrest :: empty ( )
252
254
} else {
253
255
ty. uninhabited_from ( visited, tcx)
254
256
}
255
257
}
256
258
TyRef ( _, ref tm) => tm. ty . uninhabited_from ( visited, tcx) ,
257
259
258
- _ => NodeForrest :: empty ( ) ,
260
+ _ => DefIdForrest :: empty ( ) ,
259
261
}
260
262
}
261
263
}
0 commit comments