about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/librustc_mir/interpret/memory.rs29
-rw-r--r--src/librustc_mir/interpret/mod.rs2
2 files changed, 19 insertions, 12 deletions
diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs
index fce33eed66f..4dbaafe4a21 100644
--- a/src/librustc_mir/interpret/memory.rs
+++ b/src/librustc_mir/interpret/memory.rs
@@ -13,6 +13,7 @@ use std::fmt;
 use std::ptr;
 
 use rustc_ast::ast::Mutability;
+use rustc_hir::def_id::DefId;
 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
 use rustc_middle::ty::{self, Instance, ParamEnv, TyCtxt};
 use rustc_target::abi::{Align, HasDataLayout, Size, TargetDataLayout};
@@ -118,6 +119,21 @@ pub struct Memory<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
     pub tcx: TyCtxt<'tcx>,
 }
 
+
+/// Return the `tcx` allocation containing the initial value of the given static
+pub fn get_static(
+    tcx: TyCtxt<'tcx>,
+    def_id: DefId,
+) -> InterpResult<'tcx, &'tcx Allocation> {
+    trace!("get_static: Need to compute {:?}", def_id);
+    let instance = Instance::mono(tcx, def_id);
+    let gid = GlobalId { instance, promoted: None };
+    // Use the raw query here to break validation cycles. Later uses of the static
+    // will call the full query anyway.
+    let raw_const = tcx.const_eval_raw(ty::ParamEnv::reveal_all().and(gid))?;
+    Ok(tcx.global_alloc(raw_const.alloc_id).unwrap_memory())
+}
+
 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> HasDataLayout for Memory<'mir, 'tcx, M> {
     #[inline]
     fn data_layout(&self) -> &TargetDataLayout {
@@ -473,17 +489,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
                 if tcx.is_foreign_item(def_id) {
                     throw_unsup!(ReadExternStatic(def_id));
                 }
-                trace!("get_global_alloc: Need to compute {:?}", def_id);
-                let instance = Instance::mono(tcx, def_id);
-                let gid = GlobalId { instance, promoted: None };
-                // Use the raw query here to break validation cycles. Later uses of the static
-                // will call the full query anyway.
-                let raw_const = tcx.const_eval_raw(ty::ParamEnv::reveal_all().and(gid))?;
-                // Make sure we use the ID of the resolved memory, not the lazy one!
-                let id = raw_const.alloc_id;
-                let allocation = tcx.global_alloc(id).unwrap_memory();
-
-                (allocation, Some(def_id))
+
+                (get_static(tcx, def_id)?, Some(def_id))
             }
         };
         M::before_access_global(memory_extra, id, alloc, def_id, is_write)?;
diff --git a/src/librustc_mir/interpret/mod.rs b/src/librustc_mir/interpret/mod.rs
index d46010d98a5..04a2c3baef4 100644
--- a/src/librustc_mir/interpret/mod.rs
+++ b/src/librustc_mir/interpret/mod.rs
@@ -20,7 +20,7 @@ pub use rustc_middle::mir::interpret::*; // have all the `interpret` symbols in
 pub use self::eval_context::{Frame, InterpCx, LocalState, LocalValue, StackPopCleanup};
 pub use self::intern::{intern_const_alloc_recursive, InternKind};
 pub use self::machine::{compile_time_machine, AllocMap, Machine, MayLeak, StackPopJump};
-pub use self::memory::{AllocCheck, FnVal, Memory, MemoryKind};
+pub use self::memory::{AllocCheck, FnVal, Memory, MemoryKind, get_static};
 pub use self::operand::{ImmTy, Immediate, OpTy, Operand};
 pub use self::place::{MPlaceTy, MemPlace, MemPlaceMeta, Place, PlaceTy};
 pub use self::validity::RefTracking;