Skip to content

Commit 069e84a

Browse files
committed
move getting the initial value of a static into helper function
1 parent debe597 commit 069e84a

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

src/librustc_mir/interpret/memory.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::fmt;
1313
use std::ptr;
1414

1515
use rustc_ast::ast::Mutability;
16+
use rustc_hir::def_id::DefId;
1617
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1718
use rustc_middle::ty::{self, Instance, ParamEnv, TyCtxt};
1819
use rustc_target::abi::{Align, HasDataLayout, Size, TargetDataLayout};
@@ -118,6 +119,21 @@ pub struct Memory<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
118119
pub tcx: TyCtxt<'tcx>,
119120
}
120121

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+
121137
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> HasDataLayout for Memory<'mir, 'tcx, M> {
122138
#[inline]
123139
fn data_layout(&self) -> &TargetDataLayout {
@@ -473,17 +489,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
473489
if tcx.is_foreign_item(def_id) {
474490
throw_unsup!(ReadExternStatic(def_id));
475491
}
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))
487494
}
488495
};
489496
M::before_access_global(memory_extra, id, alloc, def_id, is_write)?;

src/librustc_mir/interpret/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub use rustc_middle::mir::interpret::*; // have all the `interpret` symbols in
2020
pub use self::eval_context::{Frame, InterpCx, LocalState, LocalValue, StackPopCleanup};
2121
pub use self::intern::{intern_const_alloc_recursive, InternKind};
2222
pub use self::machine::{compile_time_machine, AllocMap, Machine, MayLeak, StackPopJump};
23-
pub use self::memory::{AllocCheck, FnVal, Memory, MemoryKind};
23+
pub use self::memory::{AllocCheck, FnVal, Memory, MemoryKind, get_static};
2424
pub use self::operand::{ImmTy, Immediate, OpTy, Operand};
2525
pub use self::place::{MPlaceTy, MemPlace, MemPlaceMeta, Place, PlaceTy};
2626
pub use self::validity::RefTracking;

0 commit comments

Comments
 (0)