@@ -154,10 +154,12 @@ pub struct AllocId(pub u64);
154
154
impl :: rustc_serialize:: UseSpecializedEncodable for AllocId { }
155
155
impl :: rustc_serialize:: UseSpecializedDecodable for AllocId { }
156
156
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
+ }
161
163
162
164
pub fn specialized_encode_alloc_id <
163
165
' a , ' tcx ,
@@ -166,26 +168,18 @@ pub fn specialized_encode_alloc_id<
166
168
encoder : & mut E ,
167
169
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
168
170
alloc_id : AllocId ,
169
- shorthand : Option < usize > ,
170
171
) -> Result < ( ) , E :: Error > {
171
- if let Some ( shorthand) = shorthand {
172
- return shorthand. encode ( encoder) ;
173
- }
174
172
if let Some ( alloc) = tcx. interpret_interner . get_alloc ( alloc_id) {
175
173
trace ! ( "encoding {:?} with {:#?}" , alloc_id, alloc) ;
176
- ALLOC_DISCRIMINANT . encode ( encoder) ?;
174
+ AllocKind :: Alloc . encode ( encoder) ?;
177
175
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) ?;
182
176
} else if let Some ( fn_instance) = tcx. interpret_interner . get_fn ( alloc_id) {
183
177
trace ! ( "encoding {:?} with {:#?}" , alloc_id, fn_instance) ;
184
- FN_DISCRIMINANT . encode ( encoder) ?;
178
+ AllocKind :: Fn . encode ( encoder) ?;
185
179
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) ?;
189
183
did. encode ( encoder) ?;
190
184
} else {
191
185
bug ! ( "alloc id without corresponding allocation: {}" , alloc_id) ;
@@ -196,53 +190,42 @@ pub fn specialized_encode_alloc_id<
196
190
pub fn specialized_decode_alloc_id <
197
191
' a , ' tcx ,
198
192
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 ) ,
201
194
> (
202
195
decoder : & mut D ,
203
196
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
204
- pos : usize ,
205
197
cache : CACHE ,
206
- short : SHORT ,
207
198
) -> Result < AllocId , D :: Error > {
208
- match usize :: decode ( decoder) ? {
209
- ALLOC_DISCRIMINANT => {
199
+ match AllocKind :: decode ( decoder) ? {
200
+ AllocKind :: Alloc => {
210
201
let alloc_id = tcx. interpret_interner . reserve ( ) ;
211
- trace ! ( "creating alloc id {:?} at {} " , alloc_id, pos ) ;
202
+ trace ! ( "creating alloc id {:?}" , alloc_id) ;
212
203
// insert early to allow recursive allocs
213
- cache ( decoder, pos , alloc_id) ;
204
+ cache ( decoder, alloc_id) ;
214
205
215
206
let allocation = Allocation :: decode ( decoder) ?;
216
207
trace ! ( "decoded alloc {:?} {:#?}" , alloc_id, allocation) ;
217
208
let allocation = tcx. intern_const_alloc ( allocation) ;
218
209
tcx. interpret_interner . intern_at_reserved ( alloc_id, allocation) ;
219
210
220
- if let Some ( glob) = Option :: < DefId > :: decode ( decoder) ? {
221
- tcx. interpret_interner . cache ( glob, alloc_id) ;
222
- }
223
-
224
211
Ok ( alloc_id)
225
212
} ,
226
- FN_DISCRIMINANT => {
227
- trace ! ( "creating fn alloc id at {}" , pos ) ;
213
+ AllocKind :: Fn => {
214
+ trace ! ( "creating fn alloc id" ) ;
228
215
let instance = ty:: Instance :: decode ( decoder) ?;
229
216
trace ! ( "decoded fn alloc instance: {:?}" , instance) ;
230
217
let id = tcx. interpret_interner . create_fn_alloc ( instance) ;
231
218
trace ! ( "created fn alloc id: {:?}" , id) ;
232
- cache ( decoder, pos , id) ;
219
+ cache ( decoder, id) ;
233
220
Ok ( id)
234
221
} ,
235
- EXTERN_STATIC_DISCRIMINANT => {
236
- trace ! ( "creating extern static alloc id at {}" , pos ) ;
222
+ AllocKind :: Static => {
223
+ trace ! ( "creating extern static alloc id at" ) ;
237
224
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) ;
240
227
Ok ( alloc_id)
241
228
} ,
242
- shorthand => {
243
- trace ! ( "loading shorthand {}" , shorthand) ;
244
- short ( decoder, shorthand)
245
- } ,
246
229
}
247
230
}
248
231
0 commit comments