@@ -13,6 +13,7 @@ use std::fmt;
13
13
use std:: ptr;
14
14
15
15
use rustc_ast:: ast:: Mutability ;
16
+ use rustc_hir:: def_id:: DefId ;
16
17
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
17
18
use rustc_middle:: ty:: { self , Instance , ParamEnv , TyCtxt } ;
18
19
use rustc_target:: abi:: { Align , HasDataLayout , Size , TargetDataLayout } ;
@@ -118,6 +119,21 @@ pub struct Memory<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
118
119
pub tcx : TyCtxt < ' tcx > ,
119
120
}
120
121
122
+
123
+ /// Return the `tcx` allocation containing the initial value of the given static
124
+ pub fn get_static (
125
+ tcx : TyCtxt < ' tcx > ,
126
+ def_id : DefId ,
127
+ ) -> InterpResult < ' tcx , & ' tcx Allocation > {
128
+ trace ! ( "get_static: Need to compute {:?}" , def_id) ;
129
+ let instance = Instance :: mono ( tcx, def_id) ;
130
+ let gid = GlobalId { instance, promoted : None } ;
131
+ // Use the raw query here to break validation cycles. Later uses of the static
132
+ // will call the full query anyway.
133
+ let raw_const = tcx. const_eval_raw ( ty:: ParamEnv :: reveal_all ( ) . and ( gid) ) ?;
134
+ Ok ( tcx. global_alloc ( raw_const. alloc_id ) . unwrap_memory ( ) )
135
+ }
136
+
121
137
impl < ' mir , ' tcx , M : Machine < ' mir , ' tcx > > HasDataLayout for Memory < ' mir , ' tcx , M > {
122
138
#[ inline]
123
139
fn data_layout ( & self ) -> & TargetDataLayout {
@@ -473,17 +489,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
473
489
if tcx. is_foreign_item ( def_id) {
474
490
throw_unsup ! ( ReadExternStatic ( def_id) ) ;
475
491
}
476
- trace ! ( "get_global_alloc: Need to compute {:?}" , def_id) ;
477
- let instance = Instance :: mono ( tcx, def_id) ;
478
- let gid = GlobalId { instance, promoted : None } ;
479
- // Use the raw query here to break validation cycles. Later uses of the static
480
- // will call the full query anyway.
481
- let raw_const = tcx. const_eval_raw ( ty:: ParamEnv :: reveal_all ( ) . and ( gid) ) ?;
482
- // Make sure we use the ID of the resolved memory, not the lazy one!
483
- let id = raw_const. alloc_id ;
484
- let allocation = tcx. global_alloc ( id) . unwrap_memory ( ) ;
485
-
486
- ( allocation, Some ( def_id) )
492
+
493
+ ( get_static ( tcx, def_id) ?, Some ( def_id) )
487
494
}
488
495
} ;
489
496
M :: before_access_global ( memory_extra, id, alloc, def_id, is_write) ?;
0 commit comments