@@ -20,7 +20,6 @@ use ich::{StableHashingContext, NodeIdHashingMode};
20
20
use util:: nodemap:: { FxHashMap , FxHashSet } ;
21
21
use ty;
22
22
23
- use std:: fmt;
24
23
use std:: mem;
25
24
use rustc_data_structures:: sync:: Lrc ;
26
25
use syntax:: source_map;
@@ -100,39 +99,29 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
100
99
/// placate the same deriving in `ty::FreeRegion`, but we may want to
101
100
/// actually attach a more meaningful ordering to scopes than the one
102
101
/// generated via deriving here.
103
- ///
104
- /// Scope is a bit-packed to save space - if `code` is SCOPE_DATA_REMAINDER_MAX
105
- /// or less, it is a `ScopeData::Remainder`, otherwise it is a type specified
106
- /// by the bitpacking.
107
- #[ derive( Clone , PartialEq , PartialOrd , Eq , Ord , Hash , Copy , RustcEncodable , RustcDecodable ) ]
102
+ #[ derive( Clone , PartialEq , PartialOrd , Eq , Ord , Hash , Debug , Copy , RustcEncodable , RustcDecodable ) ]
108
103
pub struct Scope {
109
104
pub ( crate ) id : hir:: ItemLocalId ,
110
- pub ( crate ) code : u32
105
+ pub ( crate ) data : ScopeData ,
111
106
}
112
107
113
- const SCOPE_DATA_NODE : u32 = 0xFFFF_FFFF ;
114
- const SCOPE_DATA_CALLSITE : u32 = 0xFFFF_FFFE ;
115
- const SCOPE_DATA_ARGUMENTS : u32 = 0xFFFF_FFFD ;
116
- const SCOPE_DATA_DESTRUCTION : u32 = 0xFFFF_FFFC ;
117
- // be sure to add the MAX of FirstStatementIndex if you add more constants here
118
-
119
108
#[ derive( Clone , PartialEq , PartialOrd , Eq , Ord , Hash , Debug , Copy , RustcEncodable , RustcDecodable ) ]
120
109
pub enum ScopeData {
121
- Node ( hir :: ItemLocalId ) ,
110
+ Node ,
122
111
123
112
// Scope of the call-site for a function or closure
124
113
// (outlives the arguments as well as the body).
125
- CallSite ( hir :: ItemLocalId ) ,
114
+ CallSite ,
126
115
127
116
// Scope of arguments passed to a function or closure
128
117
// (they outlive its body).
129
- Arguments ( hir :: ItemLocalId ) ,
118
+ Arguments ,
130
119
131
120
// Scope of destructors for temporaries of node-id.
132
- Destruction ( hir :: ItemLocalId ) ,
121
+ Destruction ,
133
122
134
123
// Scope following a `let id = expr;` binding in a block.
135
- Remainder ( BlockRemainder )
124
+ Remainder ( FirstStatementIndex )
136
125
}
137
126
138
127
/// Represents a subscope of `block` for a binding that is introduced
@@ -152,81 +141,61 @@ pub enum ScopeData {
152
141
///
153
142
/// * the subscope with `first_statement_index == 1` is scope of `c`,
154
143
/// and thus does not include EXPR_2, but covers the `...`.
155
- #[ derive( Clone , PartialEq , PartialOrd , Eq , Ord , Hash , RustcEncodable ,
156
- RustcDecodable , Debug , Copy ) ]
157
- pub struct BlockRemainder {
158
- pub block : hir:: ItemLocalId ,
159
- pub first_statement_index : FirstStatementIndex ,
160
- }
161
144
162
145
newtype_index ! {
163
146
pub struct FirstStatementIndex ;
164
147
}
165
148
166
149
impl_stable_hash_for ! ( struct :: middle:: region:: FirstStatementIndex { private } ) ;
167
150
168
- impl From < ScopeData > for Scope {
169
- #[ inline]
170
- fn from ( scope_data : ScopeData ) -> Self {
171
- let ( id, code) = match scope_data {
172
- ScopeData :: Node ( id) => ( id, SCOPE_DATA_NODE ) ,
173
- ScopeData :: CallSite ( id) => ( id, SCOPE_DATA_CALLSITE ) ,
174
- ScopeData :: Arguments ( id) => ( id, SCOPE_DATA_ARGUMENTS ) ,
175
- ScopeData :: Destruction ( id) => ( id, SCOPE_DATA_DESTRUCTION ) ,
176
- ScopeData :: Remainder ( r) => ( r. block , r. first_statement_index . index ( ) as u32 )
177
- } ;
178
- Self { id, code }
179
- }
180
- }
181
-
182
- impl fmt:: Debug for Scope {
183
- fn fmt ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
184
- fmt:: Debug :: fmt ( & self . data ( ) , formatter)
185
- }
186
- }
151
+ // compilation error if size of `ScopeData` is not the same as a `u32`
152
+ #[ allow( dead_code) ]
153
+ // only works on stage 1 when the rustc_layout_scalar_valid_range attribute actually exists
154
+ #[ cfg( not( stage0) ) ]
155
+ static ASSERT : ( ) = [ ( ) ] [ ( mem:: size_of :: < ScopeData > ( ) != 4 ) as usize ] ;
187
156
188
157
#[ allow( non_snake_case) ]
189
158
impl Scope {
190
159
#[ inline]
191
160
pub fn data ( self ) -> ScopeData {
192
- match self . code {
193
- SCOPE_DATA_NODE => ScopeData :: Node ( self . id ) ,
194
- SCOPE_DATA_CALLSITE => ScopeData :: CallSite ( self . id ) ,
195
- SCOPE_DATA_ARGUMENTS => ScopeData :: Arguments ( self . id ) ,
196
- SCOPE_DATA_DESTRUCTION => ScopeData :: Destruction ( self . id ) ,
197
- idx => ScopeData :: Remainder ( BlockRemainder {
198
- block : self . id ,
199
- first_statement_index : FirstStatementIndex :: new ( idx as usize )
200
- } )
161
+ self . data
201
162
}
163
+
164
+ #[ inline]
165
+ pub fn new ( id : hir:: ItemLocalId , data : ScopeData ) -> Self {
166
+ Scope { id, data }
202
167
}
203
168
204
169
#[ inline]
205
170
pub fn Node ( id : hir:: ItemLocalId ) -> Self {
206
- Self :: from ( ScopeData :: Node ( id ) )
171
+ Self :: new ( id , ScopeData :: Node )
207
172
}
208
173
209
174
#[ inline]
210
175
pub fn CallSite ( id : hir:: ItemLocalId ) -> Self {
211
- Self :: from ( ScopeData :: CallSite ( id ) )
176
+ Self :: new ( id , ScopeData :: CallSite )
212
177
}
213
178
214
179
#[ inline]
215
180
pub fn Arguments ( id : hir:: ItemLocalId ) -> Self {
216
- Self :: from ( ScopeData :: Arguments ( id ) )
181
+ Self :: new ( id , ScopeData :: Arguments )
217
182
}
218
183
219
184
#[ inline]
220
185
pub fn Destruction ( id : hir:: ItemLocalId ) -> Self {
221
- Self :: from ( ScopeData :: Destruction ( id ) )
186
+ Self :: new ( id , ScopeData :: Destruction )
222
187
}
223
188
224
189
#[ inline]
225
- pub fn Remainder ( r : BlockRemainder ) -> Self {
226
- Self :: from ( ScopeData :: Remainder ( r) )
190
+ pub fn Remainder (
191
+ id : hir:: ItemLocalId ,
192
+ first : FirstStatementIndex ,
193
+ ) -> Self {
194
+ Self :: new ( id, ScopeData :: Remainder ( first) )
227
195
}
228
196
}
229
197
198
+
230
199
impl Scope {
231
200
/// Returns a item-local id associated with this scope.
232
201
///
@@ -257,7 +226,7 @@ impl Scope {
257
226
return DUMMY_SP ;
258
227
}
259
228
let span = tcx. hir . span ( node_id) ;
260
- if let ScopeData :: Remainder ( r ) = self . data ( ) {
229
+ if let ScopeData :: Remainder ( first_statement_index ) = self . data ( ) {
261
230
if let Node :: Block ( ref blk) = tcx. hir . get ( node_id) {
262
231
// Want span for scope starting after the
263
232
// indexed statement and ending at end of
@@ -267,7 +236,7 @@ impl Scope {
267
236
// (This is the special case aluded to in the
268
237
// doc-comment for this method)
269
238
270
- let stmt_span = blk. stmts [ r . first_statement_index . index ( ) ] . span ;
239
+ let stmt_span = blk. stmts [ first_statement_index. index ( ) ] . span ;
271
240
272
241
// To avoid issues with macro-generated spans, the span
273
242
// of the statement must be nested in that of the block.
@@ -511,8 +480,8 @@ impl<'tcx> ScopeTree {
511
480
}
512
481
513
482
// record the destruction scopes for later so we can query them
514
- if let ScopeData :: Destruction ( n ) = child. data ( ) {
515
- self . destruction_scopes . insert ( n , child) ;
483
+ if let ScopeData :: Destruction = child. data ( ) {
484
+ self . destruction_scopes . insert ( child . item_local_id ( ) , child) ;
516
485
}
517
486
}
518
487
@@ -595,7 +564,7 @@ impl<'tcx> ScopeTree {
595
564
596
565
while let Some ( & ( p, _) ) = self . parent_map . get ( & id) {
597
566
match p. data ( ) {
598
- ScopeData :: Destruction ( .. ) => {
567
+ ScopeData :: Destruction => {
599
568
debug ! ( "temporary_scope({:?}) = {:?} [enclosing]" ,
600
569
expr_id, id) ;
601
570
return Some ( id) ;
@@ -650,8 +619,8 @@ impl<'tcx> ScopeTree {
650
619
/// Returns the id of the innermost containing body
651
620
pub fn containing_body ( & self , mut scope : Scope ) -> Option < hir:: ItemLocalId > {
652
621
loop {
653
- if let ScopeData :: CallSite ( id ) = scope. data ( ) {
654
- return Some ( id ) ;
622
+ if let ScopeData :: CallSite = scope. data ( ) {
623
+ return Some ( scope . item_local_id ( ) ) ;
655
624
}
656
625
657
626
match self . opt_encl_scope ( scope) {
@@ -867,10 +836,7 @@ fn resolve_block<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, blk:
867
836
// except for the first such subscope, which has the
868
837
// block itself as a parent.
869
838
visitor. enter_scope (
870
- Scope :: Remainder ( BlockRemainder {
871
- block : blk. hir_id . local_id ,
872
- first_statement_index : FirstStatementIndex :: new ( i)
873
- } )
839
+ Scope :: Remainder ( blk. hir_id . local_id , FirstStatementIndex :: new ( i) )
874
840
) ;
875
841
visitor. cx . var_parent = visitor. cx . parent ;
876
842
}
@@ -1033,7 +999,7 @@ fn resolve_expr<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, expr:
1033
999
match visitor. scope_tree . parent_map . get ( & scope) {
1034
1000
// Don't cross from closure bodies to their parent.
1035
1001
Some ( & ( superscope, _) ) => match superscope. data ( ) {
1036
- ScopeData :: CallSite ( _ ) => break ,
1002
+ ScopeData :: CallSite => break ,
1037
1003
_ => scope = superscope
1038
1004
} ,
1039
1005
None => break
0 commit comments