Skip to content

Commit 1a0a4ac

Browse files
committed
Add a function to TyCtxt for computing an Allocation for a static item's initializer
1 parent a9025c5 commit 1a0a4ac

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

src/librustc_middle/mir/interpret/queries.rs

+23
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,27 @@ impl<'tcx> TyCtxt<'tcx> {
7474
self.const_eval_validated(inputs)
7575
}
7676
}
77+
78+
/// Evaluate a static's initializer, returning the allocation of the initializer's memory.
79+
pub fn eval_static_initializer(
80+
self,
81+
def_id: DefId,
82+
) -> Result<&'tcx mir::Allocation, ErrorHandled> {
83+
trace!("eval_static_initializer: Need to compute {:?}", def_id);
84+
assert!(self.is_static(def_id));
85+
let instance = ty::Instance::mono(self, def_id);
86+
let gid = GlobalId { instance, promoted: None };
87+
self.eval_to_allocation(gid, ty::ParamEnv::reveal_all())
88+
}
89+
90+
/// Evaluate anything constant-like, returning the allocation of the final memory.
91+
fn eval_to_allocation(
92+
self,
93+
gid: GlobalId<'tcx>,
94+
param_env: ty::ParamEnv<'tcx>,
95+
) -> Result<&'tcx mir::Allocation, ErrorHandled> {
96+
trace!("eval_to_allocation: Need to compute {:?}", gid);
97+
let raw_const = self.const_eval_raw(param_env.and(gid))?;
98+
Ok(self.global_alloc(raw_const.alloc_id).unwrap_memory())
99+
}
77100
}

src/librustc_mir/interpret/memory.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ use std::ptr;
1414

1515
use rustc_ast::ast::Mutability;
1616
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
17-
use rustc_hir::def_id::DefId;
18-
use rustc_middle::ty::{self, Instance, ParamEnv, TyCtxt};
17+
use rustc_middle::ty::{Instance, ParamEnv, TyCtxt};
1918
use rustc_target::abi::{Align, HasDataLayout, Size, TargetDataLayout};
2019

2120
use super::{
22-
AllocId, AllocMap, Allocation, AllocationExtra, CheckInAllocMsg, GlobalAlloc, GlobalId,
23-
InterpResult, Machine, MayLeak, Pointer, PointerArithmetic, Scalar,
21+
AllocId, AllocMap, Allocation, AllocationExtra, CheckInAllocMsg, GlobalAlloc, InterpResult,
22+
Machine, MayLeak, Pointer, PointerArithmetic, Scalar,
2423
};
2524
use crate::util::pretty;
2625

@@ -119,17 +118,6 @@ pub struct Memory<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
119118
pub tcx: TyCtxt<'tcx>,
120119
}
121120

122-
/// Return the `tcx` allocation containing the initial value of the given static
123-
pub fn get_static(tcx: TyCtxt<'tcx>, def_id: DefId) -> InterpResult<'tcx, &'tcx Allocation> {
124-
trace!("get_static: Need to compute {:?}", def_id);
125-
let instance = Instance::mono(tcx, def_id);
126-
let gid = GlobalId { instance, promoted: None };
127-
// Use the raw query here to break validation cycles. Later uses of the static
128-
// will call the full query anyway.
129-
let raw_const = tcx.const_eval_raw(ty::ParamEnv::reveal_all().and(gid))?;
130-
Ok(tcx.global_alloc(raw_const.alloc_id).unwrap_memory())
131-
}
132-
133121
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> HasDataLayout for Memory<'mir, 'tcx, M> {
134122
#[inline]
135123
fn data_layout(&self) -> &TargetDataLayout {
@@ -489,7 +477,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
489477
throw_unsup!(ReadExternStatic(def_id));
490478
}
491479

492-
(get_static(tcx, def_id)?, Some(def_id))
480+
(tcx.eval_static_initializer(def_id)?, Some(def_id))
493481
}
494482
};
495483
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
@@ -21,7 +21,7 @@ pub use rustc_middle::mir::interpret::*; // have all the `interpret` symbols in
2121
pub use self::eval_context::{Frame, FrameInfo, InterpCx, LocalState, LocalValue, StackPopCleanup};
2222
pub use self::intern::{intern_const_alloc_recursive, InternKind};
2323
pub use self::machine::{compile_time_machine, AllocMap, Machine, MayLeak, StackPopJump};
24-
pub use self::memory::{get_static, AllocCheck, FnVal, Memory, MemoryKind};
24+
pub use self::memory::{AllocCheck, FnVal, Memory, MemoryKind};
2525
pub use self::operand::{ImmTy, Immediate, OpTy, Operand};
2626
pub use self::place::{MPlaceTy, MemPlace, MemPlaceMeta, Place, PlaceTy};
2727
pub use self::validity::RefTracking;

0 commit comments

Comments
 (0)