Skip to content

Commit acdba55

Browse files
authored
Rollup merge of #95693 - RalfJung:more-context, r=oli-obk
interp: pass TyCtxt to Machine methods that do not take InterpCx This just seems like something you might need, so let's consistently have it. One day we might have to add `ParamEnv` as well, though that seems less likely (and in Miri you can always use `reveal_all` anyway). It might make sense to have a type that packages `TyCtxt` and `ParamEnv`, this pairing occurs quite frequently in rustc... r? `@oli-obk`
2 parents 84e1aa2 + fcdfc3e commit acdba55

File tree

5 files changed

+15
-7
lines changed

5 files changed

+15
-7
lines changed

compiler/rustc_const_eval/src/const_eval/machine.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_hir::def::DefKind;
22
use rustc_middle::mir;
3-
use rustc_middle::ty::{self, Ty};
3+
use rustc_middle::ty::{self, Ty, TyCtxt};
44
use std::borrow::Borrow;
55
use std::collections::hash_map::Entry;
66
use std::hash::Hash;
@@ -471,6 +471,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
471471
}
472472

473473
fn before_access_global(
474+
_tcx: TyCtxt<'tcx>,
474475
machine: &Self,
475476
alloc_id: AllocId,
476477
alloc: ConstAllocation<'tcx>,

compiler/rustc_const_eval/src/interpret/machine.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::fmt::Debug;
77
use std::hash::Hash;
88

99
use rustc_middle::mir;
10-
use rustc_middle::ty::{self, Ty};
10+
use rustc_middle::ty::{self, Ty, TyCtxt};
1111
use rustc_span::def_id::DefId;
1212
use rustc_target::abi::Size;
1313
use rustc_target::spec::abi::Abi;
@@ -246,6 +246,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
246246
/// `def_id` is `Some` if this is the "lazy" allocation of a static.
247247
#[inline]
248248
fn before_access_global(
249+
_tcx: TyCtxt<'tcx>,
249250
_machine: &Self,
250251
_alloc_id: AllocId,
251252
_allocation: ConstAllocation<'tcx>,
@@ -317,6 +318,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
317318
/// need to mutate.
318319
#[inline(always)]
319320
fn memory_read(
321+
_tcx: TyCtxt<'tcx>,
320322
_machine: &Self,
321323
_alloc_extra: &Self::AllocExtra,
322324
_tag: Self::PointerTag,
@@ -328,6 +330,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
328330
/// Hook for performing extra checks on a memory write access.
329331
#[inline(always)]
330332
fn memory_written(
333+
_tcx: TyCtxt<'tcx>,
331334
_machine: &mut Self,
332335
_alloc_extra: &mut Self::AllocExtra,
333336
_tag: Self::PointerTag,
@@ -339,6 +342,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
339342
/// Hook for performing extra operations on a memory deallocation.
340343
#[inline(always)]
341344
fn memory_deallocated(
345+
_tcx: TyCtxt<'tcx>,
342346
_machine: &mut Self,
343347
_alloc_extra: &mut Self::AllocExtra,
344348
_tag: Self::PointerTag,

compiler/rustc_const_eval/src/interpret/memory.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
327327
// Let the machine take some extra action
328328
let size = alloc.size();
329329
M::memory_deallocated(
330+
*self.tcx,
330331
&mut self.machine,
331332
&mut alloc.extra,
332333
ptr.provenance,
@@ -509,7 +510,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
509510
(self.tcx.eval_static_initializer(def_id)?, Some(def_id))
510511
}
511512
};
512-
M::before_access_global(&self.machine, id, alloc, def_id, is_write)?;
513+
M::before_access_global(*self.tcx, &self.machine, id, alloc, def_id, is_write)?;
513514
// We got tcx memory. Let the machine initialize its "extra" stuff.
514515
let alloc = M::init_allocation_extra(
515516
self,
@@ -575,7 +576,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
575576
)?;
576577
if let Some((alloc_id, offset, ptr, alloc)) = ptr_and_alloc {
577578
let range = alloc_range(offset, size);
578-
M::memory_read(&self.machine, &alloc.extra, ptr.provenance, range)?;
579+
M::memory_read(*self.tcx, &self.machine, &alloc.extra, ptr.provenance, range)?;
579580
Ok(Some(AllocRef { alloc, range, tcx: *self.tcx, alloc_id }))
580581
} else {
581582
// Even in this branch we have to be sure that we actually access the allocation, in
@@ -636,7 +637,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
636637
// We cannot call `get_raw_mut` inside `check_and_deref_ptr` as that would duplicate `&mut self`.
637638
let (alloc, machine) = self.get_alloc_raw_mut(alloc_id)?;
638639
let range = alloc_range(offset, size);
639-
M::memory_written(machine, &mut alloc.extra, ptr.provenance, range)?;
640+
M::memory_written(tcx, machine, &mut alloc.extra, ptr.provenance, range)?;
640641
Ok(Some(AllocRefMut { alloc, range, tcx, alloc_id }))
641642
} else {
642643
Ok(None)
@@ -1009,7 +1010,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
10091010
};
10101011
let src_alloc = self.get_alloc_raw(src_alloc_id)?;
10111012
let src_range = alloc_range(src_offset, size);
1012-
M::memory_read(&self.machine, &src_alloc.extra, src.provenance, src_range)?;
1013+
M::memory_read(*tcx, &self.machine, &src_alloc.extra, src.provenance, src_range)?;
10131014
// We need the `dest` ptr for the next operation, so we get it now.
10141015
// We already did the source checks and called the hooks so we are good to return early.
10151016
let Some((dest_alloc_id, dest_offset, dest)) = dest_parts else {
@@ -1034,7 +1035,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
10341035
// Destination alloc preparations and access hooks.
10351036
let (dest_alloc, extra) = self.get_alloc_raw_mut(dest_alloc_id)?;
10361037
let dest_range = alloc_range(dest_offset, size * num_copies);
1037-
M::memory_written(extra, &mut dest_alloc.extra, dest.provenance, dest_range)?;
1038+
M::memory_written(*tcx, extra, &mut dest_alloc.extra, dest.provenance, dest_range)?;
10381039
let dest_bytes = dest_alloc
10391040
.get_bytes_mut_ptr(&tcx, dest_range)
10401041
.map_err(|e| e.to_interp_error(dest_alloc_id))?

compiler/rustc_mir_transform/src/const_prop.rs

+1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx>
265265
}
266266

267267
fn before_access_global(
268+
_tcx: TyCtxt<'tcx>,
268269
_machine: &Self,
269270
_alloc_id: AllocId,
270271
alloc: ConstAllocation<'tcx, Self::PointerTag, Self::AllocExtra>,

compiler/rustc_mir_transform/src/const_prop_lint.rs

+1
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx>
261261
}
262262

263263
fn before_access_global(
264+
_tcx: TyCtxt<'tcx>,
264265
_machine: &Self,
265266
_alloc_id: AllocId,
266267
alloc: ConstAllocation<'tcx, Self::PointerTag, Self::AllocExtra>,

0 commit comments

Comments
 (0)