Skip to content

Commit 6cca716

Browse files
committed
pass MemoryExtra to find_foreign_static and adjust_static_allocation; they might have to create allocations
1 parent 4c090fe commit 6cca716

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

src/librustc_mir/const_eval.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -433,16 +433,18 @@ impl<'a, 'mir, 'tcx> interpret::Machine<'a, 'mir, 'tcx>
433433
}
434434

435435
fn find_foreign_static(
436-
_tcx: TyCtxtAt<'a, 'tcx, 'tcx>,
437436
_def_id: DefId,
437+
_tcx: TyCtxtAt<'a, 'tcx, 'tcx>,
438+
_memory_extra: &(),
438439
) -> EvalResult<'tcx, Cow<'tcx, Allocation<Self::PointerTag>>> {
439440
err!(ReadForeignStatic)
440441
}
441442

442443
#[inline(always)]
443-
fn adjust_static_allocation(
444-
alloc: &'_ Allocation
445-
) -> Cow<'_, Allocation<Self::PointerTag>> {
444+
fn adjust_static_allocation<'b>(
445+
alloc: &'b Allocation,
446+
_memory_extra: &(),
447+
) -> Cow<'b, Allocation<Self::PointerTag>> {
446448
// We do not use a tag so we can just cheaply forward the reference
447449
Cow::Borrowed(alloc)
448450
}

src/librustc_mir/interpret/machine.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ pub trait Machine<'a, 'mir, 'tcx>: Sized {
140140
/// the machine memory. (This relies on `AllocMap::get_or` being able to add the
141141
/// owned allocation to the map even when the map is shared.)
142142
fn find_foreign_static(
143-
tcx: TyCtxtAt<'a, 'tcx, 'tcx>,
144143
def_id: DefId,
144+
tcx: TyCtxtAt<'a, 'tcx, 'tcx>,
145+
memory_extra: &Self::MemoryExtra,
145146
) -> EvalResult<'tcx, Cow<'tcx, Allocation<Self::PointerTag, Self::AllocExtra>>>;
146147

147148
/// Called to turn an allocation obtained from the `tcx` into one that has
@@ -151,9 +152,10 @@ pub trait Machine<'a, 'mir, 'tcx>: Sized {
151152
/// allocation (because a copy had to be done to add tags or metadata), machine memory will
152153
/// cache the result. (This relies on `AllocMap::get_or` being able to add the
153154
/// owned allocation to the map even when the map is shared.)
154-
fn adjust_static_allocation(
155-
alloc: &'_ Allocation
156-
) -> Cow<'_, Allocation<Self::PointerTag, Self::AllocExtra>>;
155+
fn adjust_static_allocation<'b>(
156+
alloc: &'b Allocation,
157+
memory_extra: &Self::MemoryExtra,
158+
) -> Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>;
157159

158160
/// Called for all binary operations on integer(-like) types when one operand is a pointer
159161
/// value, and for the `Offset` operation that is inherently about pointers.

src/librustc_mir/interpret/memory.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -320,15 +320,16 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
320320
/// this machine use the same pointer tag, so it is indirected through
321321
/// `M::static_with_default_tag`.
322322
fn get_static_alloc(
323-
tcx: TyCtxtAt<'a, 'tcx, 'tcx>,
324323
id: AllocId,
324+
tcx: TyCtxtAt<'a, 'tcx, 'tcx>,
325+
memory_extra: &M::MemoryExtra,
325326
) -> EvalResult<'tcx, Cow<'tcx, Allocation<M::PointerTag, M::AllocExtra>>> {
326327
let alloc = tcx.alloc_map.lock().get(id);
327328
let def_id = match alloc {
328329
Some(AllocType::Memory(mem)) => {
329330
// We got tcx memory. Let the machine figure out whether and how to
330331
// turn that into memory with the right pointer tag.
331-
return Ok(M::adjust_static_allocation(mem))
332+
return Ok(M::adjust_static_allocation(mem, memory_extra))
332333
}
333334
Some(AllocType::Function(..)) => {
334335
return err!(DerefFunctionPointer)
@@ -342,7 +343,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
342343
// We got a "lazy" static that has not been computed yet, do some work
343344
trace!("static_alloc: Need to compute {:?}", def_id);
344345
if tcx.is_foreign_item(def_id) {
345-
return M::find_foreign_static(tcx, def_id);
346+
return M::find_foreign_static(def_id, tcx, memory_extra);
346347
}
347348
let instance = Instance::mono(tcx.tcx, def_id);
348349
let gid = GlobalId {
@@ -362,7 +363,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
362363
let allocation = tcx.alloc_map.lock().unwrap_memory(raw_const.alloc_id);
363364
// We got tcx memory. Let the machine figure out whether and how to
364365
// turn that into memory with the right pointer tag.
365-
M::adjust_static_allocation(allocation)
366+
M::adjust_static_allocation(allocation, memory_extra)
366367
})
367368
}
368369

@@ -372,7 +373,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
372373
// `get_static_alloc` that we can actually use directly without inserting anything anywhere.
373374
// So the error type is `EvalResult<'tcx, &Allocation<M::PointerTag>>`.
374375
let a = self.alloc_map.get_or(id, || {
375-
let alloc = Self::get_static_alloc(self.tcx, id).map_err(Err)?;
376+
let alloc = Self::get_static_alloc(id, self.tcx, &self.extra).map_err(Err)?;
376377
match alloc {
377378
Cow::Borrowed(alloc) => {
378379
// We got a ref, cheaply return that as an "error" so that the
@@ -401,10 +402,11 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
401402
id: AllocId,
402403
) -> EvalResult<'tcx, &mut Allocation<M::PointerTag, M::AllocExtra>> {
403404
let tcx = self.tcx;
405+
let memory_extra = &self.extra;
404406
let a = self.alloc_map.get_mut_or(id, || {
405407
// Need to make a copy, even if `get_static_alloc` is able
406408
// to give us a cheap reference.
407-
let alloc = Self::get_static_alloc(tcx, id)?;
409+
let alloc = Self::get_static_alloc(id, tcx, memory_extra)?;
408410
if alloc.mutability == Mutability::Immutable {
409411
return err!(ModifiedConstantMemory);
410412
}

0 commit comments

Comments
 (0)