@@ -98,9 +98,16 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
9898/// generated via deriving here.
9999#[ derive( Clone , PartialEq , PartialOrd , Eq , Ord , Hash , Debug , Copy , RustcEncodable , RustcDecodable ) ]
100100pub struct Scope {
101- pub scope_data : ScopeData
101+ pub ( crate ) id : hir:: ItemLocalId ,
102+ pub ( crate ) code : u32
102103}
103104
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+
104111#[ derive( Clone , PartialEq , PartialOrd , Eq , Ord , Hash , Debug , Copy , RustcEncodable , RustcDecodable ) ]
105112pub enum ScopeData {
106113 Node ( hir:: ItemLocalId ) ,
@@ -148,11 +155,9 @@ pub struct BlockRemainder {
148155 RustcDecodable , Debug , Copy ) ]
149156pub struct FirstStatementIndex { pub idx : u32 }
150157
151- pub const FIRST_STATEMENT_MAX : usize = !0u32 as usize ;
152-
153158impl Idx for FirstStatementIndex {
154159 fn new ( idx : usize ) -> Self {
155- assert ! ( idx <= FIRST_STATEMENT_MAX ) ;
160+ assert ! ( idx <= SCOPE_DATA_REMAINDER_MAX as usize ) ;
156161 FirstStatementIndex { idx : idx as u32 }
157162 }
158163
@@ -164,15 +169,31 @@ impl Idx for FirstStatementIndex {
164169impl From < ScopeData > for Scope {
165170 #[ inline]
166171 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 }
168180 }
169181}
170182
171183#[ allow( non_snake_case) ]
172184impl Scope {
173185 #[ inline]
174186 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+ }
176197 }
177198
178199 #[ inline]
@@ -207,17 +228,7 @@ impl Scope {
207228 /// NB: likely to be replaced as API is refined; e.g. pnkfelix
208229 /// anticipates `fn entry_node_id` and `fn each_exit_node_id`.
209230 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
221232 }
222233
223234 pub fn node_id ( & self , tcx : TyCtxt , scope_tree : & ScopeTree ) -> ast:: NodeId {
0 commit comments