@@ -154,10 +154,12 @@ pub struct AllocId(pub u64);
154154impl :: rustc_serialize:: UseSpecializedEncodable for AllocId { }
155155impl :: rustc_serialize:: UseSpecializedDecodable for AllocId { }
156156
157- pub const ALLOC_DISCRIMINANT : usize = 0 ;
158- pub const FN_DISCRIMINANT : usize = 1 ;
159- pub const EXTERN_STATIC_DISCRIMINANT : usize = 2 ;
160- pub const SHORTHAND_START : usize = 3 ;
157+ #[ derive( RustcDecodable , RustcEncodable ) ]
158+ enum AllocKind {
159+ Alloc ,
160+ Fn ,
161+ Static ,
162+ }
161163
162164pub fn specialized_encode_alloc_id <
163165 ' a , ' tcx ,
@@ -166,26 +168,18 @@ pub fn specialized_encode_alloc_id<
166168 encoder : & mut E ,
167169 tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
168170 alloc_id : AllocId ,
169- shorthand : Option < usize > ,
170171) -> Result < ( ) , E :: Error > {
171- if let Some ( shorthand) = shorthand {
172- return shorthand. encode ( encoder) ;
173- }
174172 if let Some ( alloc) = tcx. interpret_interner . get_alloc ( alloc_id) {
175173 trace ! ( "encoding {:?} with {:#?}" , alloc_id, alloc) ;
176- ALLOC_DISCRIMINANT . encode ( encoder) ?;
174+ AllocKind :: Alloc . encode ( encoder) ?;
177175 alloc. encode ( encoder) ?;
178- // encode whether this allocation is the root allocation of a static
179- tcx. interpret_interner
180- . get_corresponding_static_def_id ( alloc_id)
181- . encode ( encoder) ?;
182176 } else if let Some ( fn_instance) = tcx. interpret_interner . get_fn ( alloc_id) {
183177 trace ! ( "encoding {:?} with {:#?}" , alloc_id, fn_instance) ;
184- FN_DISCRIMINANT . encode ( encoder) ?;
178+ AllocKind :: Fn . encode ( encoder) ?;
185179 fn_instance. encode ( encoder) ?;
186- } else if let Some ( did) = tcx. interpret_interner . get_corresponding_static_def_id ( alloc_id) {
187- // extern "C" statics don 't have allocations, just encode its def_id
188- EXTERN_STATIC_DISCRIMINANT . encode ( encoder) ?;
180+ } else if let Some ( did) = tcx. interpret_interner . get_static ( alloc_id) {
181+ // referring to statics doesn 't need to know about their allocations, just about its DefId
182+ AllocKind :: Static . encode ( encoder) ?;
189183 did. encode ( encoder) ?;
190184 } else {
191185 bug ! ( "alloc id without corresponding allocation: {}" , alloc_id) ;
@@ -196,53 +190,42 @@ pub fn specialized_encode_alloc_id<
196190pub fn specialized_decode_alloc_id <
197191 ' a , ' tcx ,
198192 D : Decoder ,
199- CACHE : FnOnce ( & mut D , usize , AllocId ) ,
200- SHORT : FnOnce ( & mut D , usize ) -> Result < AllocId , D :: Error >
193+ CACHE : FnOnce ( & mut D , AllocId ) ,
201194> (
202195 decoder : & mut D ,
203196 tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
204- pos : usize ,
205197 cache : CACHE ,
206- short : SHORT ,
207198) -> Result < AllocId , D :: Error > {
208- match usize :: decode ( decoder) ? {
209- ALLOC_DISCRIMINANT => {
199+ match AllocKind :: decode ( decoder) ? {
200+ AllocKind :: Alloc => {
210201 let alloc_id = tcx. interpret_interner . reserve ( ) ;
211- trace ! ( "creating alloc id {:?} at {} " , alloc_id, pos ) ;
202+ trace ! ( "creating alloc id {:?}" , alloc_id) ;
212203 // insert early to allow recursive allocs
213- cache ( decoder, pos , alloc_id) ;
204+ cache ( decoder, alloc_id) ;
214205
215206 let allocation = Allocation :: decode ( decoder) ?;
216207 trace ! ( "decoded alloc {:?} {:#?}" , alloc_id, allocation) ;
217208 let allocation = tcx. intern_const_alloc ( allocation) ;
218209 tcx. interpret_interner . intern_at_reserved ( alloc_id, allocation) ;
219210
220- if let Some ( glob) = Option :: < DefId > :: decode ( decoder) ? {
221- tcx. interpret_interner . cache ( glob, alloc_id) ;
222- }
223-
224211 Ok ( alloc_id)
225212 } ,
226- FN_DISCRIMINANT => {
227- trace ! ( "creating fn alloc id at {}" , pos ) ;
213+ AllocKind :: Fn => {
214+ trace ! ( "creating fn alloc id" ) ;
228215 let instance = ty:: Instance :: decode ( decoder) ?;
229216 trace ! ( "decoded fn alloc instance: {:?}" , instance) ;
230217 let id = tcx. interpret_interner . create_fn_alloc ( instance) ;
231218 trace ! ( "created fn alloc id: {:?}" , id) ;
232- cache ( decoder, pos , id) ;
219+ cache ( decoder, id) ;
233220 Ok ( id)
234221 } ,
235- EXTERN_STATIC_DISCRIMINANT => {
236- trace ! ( "creating extern static alloc id at {}" , pos ) ;
222+ AllocKind :: Static => {
223+ trace ! ( "creating extern static alloc id at" ) ;
237224 let did = DefId :: decode ( decoder) ?;
238- let alloc_id = tcx. interpret_interner . reserve ( ) ;
239- tcx . interpret_interner . cache ( did , alloc_id) ;
225+ let alloc_id = tcx. interpret_interner . cache_static ( did ) ;
226+ cache ( decoder , alloc_id) ;
240227 Ok ( alloc_id)
241228 } ,
242- shorthand => {
243- trace ! ( "loading shorthand {}" , shorthand) ;
244- short ( decoder, shorthand)
245- } ,
246229 }
247230}
248231
0 commit comments