@@ -122,45 +122,6 @@ impl<'a, 'mir, 'tcx, M> Hash for Memory<'a, 'mir, 'tcx, M>
122
122
}
123
123
}
124
124
125
- /// Helper function to obtain the global (tcx) allocation for a static
126
- fn const_eval_static < ' a , ' mir , ' tcx , M : Machine < ' mir , ' tcx > > (
127
- tcx : TyCtxtAt < ' a , ' tcx , ' tcx > ,
128
- id : AllocId
129
- ) -> EvalResult < ' tcx , & ' tcx Allocation > {
130
- let alloc = tcx. alloc_map . lock ( ) . get ( id) ;
131
- let def_id = match alloc {
132
- Some ( AllocType :: Memory ( mem) ) => {
133
- return Ok ( mem)
134
- }
135
- Some ( AllocType :: Function ( ..) ) => {
136
- return err ! ( DerefFunctionPointer )
137
- }
138
- Some ( AllocType :: Static ( did) ) => {
139
- did
140
- }
141
- None =>
142
- return err ! ( DanglingPointerDeref ) ,
143
- } ;
144
- // We got a "lazy" static that has not been computed yet, do some work
145
- trace ! ( "static_alloc: Need to compute {:?}" , def_id) ;
146
- if tcx. is_foreign_item ( def_id) {
147
- return M :: find_foreign_static ( tcx, def_id) ;
148
- }
149
- let instance = Instance :: mono ( tcx. tcx , def_id) ;
150
- let gid = GlobalId {
151
- instance,
152
- promoted : None ,
153
- } ;
154
- tcx. const_eval ( ty:: ParamEnv :: reveal_all ( ) . and ( gid) ) . map_err ( |err| {
155
- // no need to report anything, the const_eval call takes care of that for statics
156
- assert ! ( tcx. is_static( def_id) . is_some( ) ) ;
157
- EvalErrorKind :: ReferencedConstant ( err) . into ( )
158
- } ) . map ( |val| {
159
- // FIXME We got our static (will be a ByRef), now we make a *copy*?!?
160
- tcx. const_to_allocation ( val)
161
- } )
162
- }
163
-
164
125
impl < ' a , ' mir , ' tcx , M : Machine < ' mir , ' tcx > > Memory < ' a , ' mir , ' tcx , M > {
165
126
pub fn new ( tcx : TyCtxtAt < ' a , ' tcx , ' tcx > , data : M :: MemoryData ) -> Self {
166
127
Memory {
@@ -343,13 +304,52 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
343
304
344
305
/// Allocation accessors
345
306
impl < ' a , ' mir , ' tcx , M : Machine < ' mir , ' tcx > > Memory < ' a , ' mir , ' tcx , M > {
307
+ /// Helper function to obtain the global (tcx) allocation for a static
308
+ fn get_static_alloc (
309
+ tcx : TyCtxtAt < ' a , ' tcx , ' tcx > ,
310
+ id : AllocId ,
311
+ ) -> EvalResult < ' tcx , & ' tcx Allocation > {
312
+ let alloc = tcx. alloc_map . lock ( ) . get ( id) ;
313
+ let def_id = match alloc {
314
+ Some ( AllocType :: Memory ( mem) ) => {
315
+ return Ok ( mem)
316
+ }
317
+ Some ( AllocType :: Function ( ..) ) => {
318
+ return err ! ( DerefFunctionPointer )
319
+ }
320
+ Some ( AllocType :: Static ( did) ) => {
321
+ did
322
+ }
323
+ None =>
324
+ return err ! ( DanglingPointerDeref ) ,
325
+ } ;
326
+ // We got a "lazy" static that has not been computed yet, do some work
327
+ trace ! ( "static_alloc: Need to compute {:?}" , def_id) ;
328
+ if tcx. is_foreign_item ( def_id) {
329
+ return M :: find_foreign_static ( tcx, def_id) ;
330
+ }
331
+ let instance = Instance :: mono ( tcx. tcx , def_id) ;
332
+ let gid = GlobalId {
333
+ instance,
334
+ promoted : None ,
335
+ } ;
336
+ tcx. const_eval ( ty:: ParamEnv :: reveal_all ( ) . and ( gid) ) . map_err ( |err| {
337
+ // no need to report anything, the const_eval call takes care of that for statics
338
+ assert ! ( tcx. is_static( def_id) . is_some( ) ) ;
339
+ EvalErrorKind :: ReferencedConstant ( err) . into ( )
340
+ } ) . map ( |val| {
341
+ // FIXME We got our static (will be a ByRef), now we make a *copy*?!?
342
+ tcx. const_to_allocation ( val)
343
+ } )
344
+ }
345
+
346
346
pub fn get ( & self , id : AllocId ) -> EvalResult < ' tcx , & Allocation > {
347
347
match self . alloc_map . get ( & id) {
348
348
// Normal alloc?
349
349
Some ( alloc) => Ok ( & alloc. 1 ) ,
350
350
// Static. No need to make any copies, just provide read access to the global static
351
351
// memory in tcx.
352
- None => const_eval_static :: < M > ( self . tcx , id) ,
352
+ None => Self :: get_static_alloc ( self . tcx , id) ,
353
353
}
354
354
}
355
355
@@ -381,7 +381,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
381
381
if ptr. offset . bytes ( ) != 0 {
382
382
return err ! ( InvalidFunctionPointer ) ;
383
383
}
384
- debug ! ( "reading fn ptr: {}" , ptr. alloc_id) ;
384
+ trace ! ( "reading fn ptr: {}" , ptr. alloc_id) ;
385
385
match self . tcx . alloc_map . lock ( ) . get ( ptr. alloc_id ) {
386
386
Some ( AllocType :: Function ( instance) ) => Ok ( instance) ,
387
387
_ => Err ( EvalErrorKind :: ExecuteMemory . into ( ) ) ,
@@ -610,7 +610,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
610
610
id : AllocId ,
611
611
kind : MemoryKind < M :: MemoryKinds > ,
612
612
) -> EvalResult < ' tcx > {
613
- let alloc = const_eval_static :: < M > ( self . tcx , id) ?;
613
+ let alloc = Self :: get_static_alloc ( self . tcx , id) ?;
614
614
if alloc. mutability == Mutability :: Immutable {
615
615
return err ! ( ModifiedConstantMemory ) ;
616
616
}
0 commit comments