@@ -98,9 +98,16 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
98
98
/// generated via deriving here.
99
99
#[ derive( Clone , PartialEq , PartialOrd , Eq , Ord , Hash , Debug , Copy , RustcEncodable , RustcDecodable ) ]
100
100
pub struct Scope {
101
- pub scope_data : ScopeData
101
+ pub ( crate ) id : hir:: ItemLocalId ,
102
+ pub ( crate ) code : u32
102
103
}
103
104
105
+ const SCOPE_DATA_NODE : u32 = !0 ;
106
+ const SCOPE_DATA_CALLSITE : u32 = !1 ;
107
+ const SCOPE_DATA_ARGUMENTS : u32 = !2 ;
108
+ const SCOPE_DATA_DESTRUCTION : u32 = !3 ;
109
+ const SCOPE_DATA_REMAINDER_MAX : u32 = !4 ;
110
+
104
111
#[ derive( Clone , PartialEq , PartialOrd , Eq , Ord , Hash , Debug , Copy , RustcEncodable , RustcDecodable ) ]
105
112
pub enum ScopeData {
106
113
Node ( hir:: ItemLocalId ) ,
@@ -148,11 +155,9 @@ pub struct BlockRemainder {
148
155
RustcDecodable , Debug , Copy ) ]
149
156
pub struct FirstStatementIndex { pub idx : u32 }
150
157
151
- pub const FIRST_STATEMENT_MAX : usize = !0u32 as usize ;
152
-
153
158
impl Idx for FirstStatementIndex {
154
159
fn new ( idx : usize ) -> Self {
155
- assert ! ( idx <= FIRST_STATEMENT_MAX ) ;
160
+ assert ! ( idx <= SCOPE_DATA_REMAINDER_MAX as usize ) ;
156
161
FirstStatementIndex { idx : idx as u32 }
157
162
}
158
163
@@ -164,15 +169,31 @@ impl Idx for FirstStatementIndex {
164
169
impl From < ScopeData > for Scope {
165
170
#[ inline]
166
171
fn from ( scope_data : ScopeData ) -> Self {
167
- Self { scope_data }
172
+ let ( id, code) = match scope_data {
173
+ ScopeData :: Node ( id) => ( id, SCOPE_DATA_NODE ) ,
174
+ ScopeData :: CallSite ( id) => ( id, SCOPE_DATA_CALLSITE ) ,
175
+ ScopeData :: Arguments ( id) => ( id, SCOPE_DATA_ARGUMENTS ) ,
176
+ ScopeData :: Destruction ( id) => ( id, SCOPE_DATA_DESTRUCTION ) ,
177
+ ScopeData :: Remainder ( r) => ( r. block , r. first_statement_index . index ( ) as u32 )
178
+ } ;
179
+ Self { id, code }
168
180
}
169
181
}
170
182
171
183
#[ allow( non_snake_case) ]
172
184
impl Scope {
173
185
#[ inline]
174
186
pub fn data ( self ) -> ScopeData {
175
- self . scope_data
187
+ match self . code {
188
+ SCOPE_DATA_NODE => ScopeData :: Node ( self . id ) ,
189
+ SCOPE_DATA_CALLSITE => ScopeData :: CallSite ( self . id ) ,
190
+ SCOPE_DATA_ARGUMENTS => ScopeData :: Arguments ( self . id ) ,
191
+ SCOPE_DATA_DESTRUCTION => ScopeData :: Destruction ( self . id ) ,
192
+ idx => ScopeData :: Remainder ( BlockRemainder {
193
+ block : self . id ,
194
+ first_statement_index : FirstStatementIndex { idx }
195
+ } )
196
+ }
176
197
}
177
198
178
199
#[ inline]
@@ -207,17 +228,7 @@ impl Scope {
207
228
/// NB: likely to be replaced as API is refined; e.g. pnkfelix
208
229
/// anticipates `fn entry_node_id` and `fn each_exit_node_id`.
209
230
pub fn item_local_id ( & self ) -> hir:: ItemLocalId {
210
- // TODO: killme
211
- match self . data ( ) {
212
- ScopeData :: Node ( id) => id,
213
-
214
- // These cases all return rough approximations to the
215
- // precise scope denoted by `self`.
216
- ScopeData :: Remainder ( br) => br. block ,
217
- ScopeData :: Destruction ( id) |
218
- ScopeData :: CallSite ( id) |
219
- ScopeData :: Arguments ( id) => id,
220
- }
231
+ self . id
221
232
}
222
233
223
234
pub fn node_id ( & self , tcx : TyCtxt , scope_tree : & ScopeTree ) -> ast:: NodeId {
0 commit comments