Skip to content

Commit d06a2a3

Browse files
committed
Pass MPlaceTy by reference not value
1 parent 5888556 commit d06a2a3

File tree

13 files changed

+72
-65
lines changed

13 files changed

+72
-65
lines changed

compiler/rustc_mir/src/const_eval/eval_queries.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
7272
None => InternKind::Constant,
7373
}
7474
};
75-
intern_const_alloc_recursive(ecx, intern_kind, ret)?;
75+
intern_const_alloc_recursive(ecx, intern_kind, &ret)?;
7676

7777
debug!("eval_body_using_ecx done: {:?}", *ret);
7878
Ok(ret)
@@ -137,7 +137,7 @@ pub(super) fn op_to_const<'tcx>(
137137
op.try_as_mplace(ecx)
138138
};
139139

140-
let to_const_value = |mplace: MPlaceTy<'_>| match mplace.ptr {
140+
let to_const_value = |mplace: &MPlaceTy<'_>| match mplace.ptr {
141141
Scalar::Ptr(ptr) => {
142142
let alloc = ecx.tcx.global_alloc(ptr.alloc_id).unwrap_memory();
143143
ConstValue::ByRef { alloc, offset: ptr.offset }
@@ -155,12 +155,12 @@ pub(super) fn op_to_const<'tcx>(
155155
}
156156
};
157157
match immediate {
158-
Ok(mplace) => to_const_value(mplace),
158+
Ok(ref mplace) => to_const_value(mplace),
159159
// see comment on `let try_as_immediate` above
160160
Err(imm) => match *imm {
161161
Immediate::Scalar(x) => match x {
162162
ScalarMaybeUninit::Scalar(s) => ConstValue::Scalar(s),
163-
ScalarMaybeUninit::Uninit => to_const_value(op.assert_mem_place(ecx)),
163+
ScalarMaybeUninit::Uninit => to_const_value(&op.assert_mem_place(ecx)),
164164
},
165165
Immediate::ScalarPair(a, b) => {
166166
let (data, start) = match a.check_init().unwrap() {

compiler/rustc_mir/src/const_eval/machine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
4040
assert!(args.len() == 1);
4141

4242
let msg_place = self.deref_operand(&args[0])?;
43-
let msg = Symbol::intern(self.read_str(msg_place)?);
43+
let msg = Symbol::intern(self.read_str(&msg_place)?);
4444
let span = self.find_closest_untracked_caller_location();
4545
let (file, line, col) = self.location_triple_for_span(span);
4646
Err(ConstEvalErrKind::Panic { msg, file, line, col }.into())

compiler/rustc_mir/src/const_eval/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub(crate) fn const_caller_location(
2929
let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), false);
3030

3131
let loc_place = ecx.alloc_caller_location(file, line, col);
32-
if intern_const_alloc_recursive(&mut ecx, InternKind::Constant, loc_place).is_err() {
32+
if intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &loc_place).is_err() {
3333
bug!("intern_const_alloc_recursive should not error in this case")
3434
}
3535
ConstValue::Scalar(loc_place.ptr)

compiler/rustc_mir/src/interpret/eval_context.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
548548
/// This can fail to provide an answer for extern types.
549549
pub(super) fn size_and_align_of(
550550
&self,
551-
metadata: MemPlaceMeta<M::PointerTag>,
552-
layout: TyAndLayout<'tcx>,
551+
metadata: &MemPlaceMeta<M::PointerTag>,
552+
layout: &TyAndLayout<'tcx>,
553553
) -> InterpResult<'tcx, Option<(Size, Align)>> {
554554
if !layout.is_unsized() {
555555
return Ok(Some((layout.size, layout.align.abi)));
@@ -577,7 +577,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
577577
// the last field). Can't have foreign types here, how would we
578578
// adjust alignment and size for them?
579579
let field = layout.field(self, layout.fields.count() - 1)?;
580-
let (unsized_size, unsized_align) = match self.size_and_align_of(metadata, field)? {
580+
let (unsized_size, unsized_align) = match self.size_and_align_of(metadata, &field)? {
581581
Some(size_and_align) => size_and_align,
582582
None => {
583583
// A field with extern type. If this field is at offset 0, we behave
@@ -645,9 +645,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
645645
#[inline]
646646
pub fn size_and_align_of_mplace(
647647
&self,
648-
mplace: MPlaceTy<'tcx, M::PointerTag>,
648+
mplace: &MPlaceTy<'tcx, M::PointerTag>,
649649
) -> InterpResult<'tcx, Option<(Size, Align)>> {
650-
self.size_and_align_of(mplace.meta, mplace.layout)
650+
self.size_and_align_of(&mplace.meta, &mplace.layout)
651651
}
652652

653653
pub fn push_stack_frame(

compiler/rustc_mir/src/interpret/intern.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ pub enum InternKind {
296296
pub fn intern_const_alloc_recursive<M: CompileTimeMachine<'mir, 'tcx, const_eval::MemoryKind>>(
297297
ecx: &mut InterpCx<'mir, 'tcx, M>,
298298
intern_kind: InternKind,
299-
ret: MPlaceTy<'tcx>,
299+
ret: &MPlaceTy<'tcx>,
300300
) -> Result<(), ErrorReported>
301301
where
302302
'tcx: 'mir,
@@ -328,7 +328,7 @@ where
328328
Some(ret.layout.ty),
329329
);
330330

331-
ref_tracking.track((ret, base_intern_mode), || ());
331+
ref_tracking.track((*ret, base_intern_mode), || ());
332332

333333
while let Some(((mplace, mode), _)) = ref_tracking.todo.pop() {
334334
let res = InternVisitor {
@@ -435,11 +435,11 @@ impl<'mir, 'tcx: 'mir, M: super::intern::CompileTimeMachine<'mir, 'tcx, !>>
435435
layout: TyAndLayout<'tcx>,
436436
f: impl FnOnce(
437437
&mut InterpCx<'mir, 'tcx, M>,
438-
MPlaceTy<'tcx, M::PointerTag>,
438+
&MPlaceTy<'tcx, M::PointerTag>,
439439
) -> InterpResult<'tcx, ()>,
440440
) -> InterpResult<'tcx, &'tcx Allocation> {
441441
let dest = self.allocate(layout, MemoryKind::Stack);
442-
f(self, dest)?;
442+
f(self, &dest)?;
443443
let ptr = dest.ptr.assert_ptr();
444444
assert_eq!(ptr.offset, Size::ZERO);
445445
let mut alloc = self.memory.alloc_map.remove(&ptr.alloc_id).unwrap().1;

compiler/rustc_mir/src/interpret/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
145145
// dereferencable!
146146
let place = self.ref_to_mplace(&self.read_immediate(&args[0])?)?;
147147
let (size, align) = self
148-
.size_and_align_of_mplace(place)?
148+
.size_and_align_of_mplace(&place)?
149149
.ok_or_else(|| err_unsup_format!("`extern type` does not have known layout"))?;
150150

151151
let result = match intrinsic_name {

compiler/rustc_mir/src/interpret/intrinsics/caller_location.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
9292
let location = self.allocate(loc_layout, MemoryKind::CallerLocation);
9393

9494
// Initialize fields.
95-
self.write_immediate(file.to_ref(), &self.mplace_field(location, 0).unwrap().into())
95+
self.write_immediate(file.to_ref(), &self.mplace_field(&location, 0).unwrap().into())
9696
.expect("writing to memory we just allocated cannot fail");
97-
self.write_scalar(line, &self.mplace_field(location, 1).unwrap().into())
97+
self.write_scalar(line, &self.mplace_field(&location, 1).unwrap().into())
9898
.expect("writing to memory we just allocated cannot fail");
99-
self.write_scalar(col, &self.mplace_field(location, 2).unwrap().into())
99+
self.write_scalar(col, &self.mplace_field(&location, 2).unwrap().into())
100100
.expect("writing to memory we just allocated cannot fail");
101101

102102
location

compiler/rustc_mir/src/interpret/operand.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ impl<'tcx, Tag: Copy> From<MPlaceTy<'tcx, Tag>> for OpTy<'tcx, Tag> {
180180
}
181181
}
182182

183+
impl<'tcx, Tag: Copy> From<&'_ MPlaceTy<'tcx, Tag>> for OpTy<'tcx, Tag> {
184+
#[inline(always)]
185+
fn from(mplace: &MPlaceTy<'tcx, Tag>) -> Self {
186+
OpTy { op: Operand::Indirect(**mplace), layout: mplace.layout }
187+
}
188+
}
189+
183190
impl<'tcx, Tag> From<ImmTy<'tcx, Tag>> for OpTy<'tcx, Tag> {
184191
#[inline(always)]
185192
fn from(val: ImmTy<'tcx, Tag>) -> Self {
@@ -243,7 +250,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
243250
/// Returns `None` if the layout does not permit loading this as a value.
244251
fn try_read_immediate_from_mplace(
245252
&self,
246-
mplace: MPlaceTy<'tcx, M::PointerTag>,
253+
mplace: &MPlaceTy<'tcx, M::PointerTag>,
247254
) -> InterpResult<'tcx, Option<ImmTy<'tcx, M::PointerTag>>> {
248255
if mplace.layout.is_unsized() {
249256
// Don't touch unsized
@@ -307,11 +314,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
307314
src: &OpTy<'tcx, M::PointerTag>,
308315
) -> InterpResult<'tcx, Result<ImmTy<'tcx, M::PointerTag>, MPlaceTy<'tcx, M::PointerTag>>> {
309316
Ok(match src.try_as_mplace(self) {
310-
Ok(mplace) => {
317+
Ok(ref mplace) => {
311318
if let Some(val) = self.try_read_immediate_from_mplace(mplace)? {
312319
Ok(val)
313320
} else {
314-
Err(mplace)
321+
Err(*mplace)
315322
}
316323
}
317324
Err(val) => Ok(val),
@@ -340,7 +347,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
340347
}
341348

342349
// Turn the wide MPlace into a string (must already be dereferenced!)
343-
pub fn read_str(&self, mplace: MPlaceTy<'tcx, M::PointerTag>) -> InterpResult<'tcx, &str> {
350+
pub fn read_str(&self, mplace: &MPlaceTy<'tcx, M::PointerTag>) -> InterpResult<'tcx, &str> {
344351
let len = mplace.len(self)?;
345352
let bytes = self.memory.read_bytes(mplace.ptr, Size::from_bytes(len))?;
346353
let str = std::str::from_utf8(bytes).map_err(|err| err_ub!(InvalidStr(err)))?;
@@ -354,7 +361,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
354361
field: usize,
355362
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
356363
let base = match op.try_as_mplace(self) {
357-
Ok(mplace) => {
364+
Ok(ref mplace) => {
358365
// We can reuse the mplace field computation logic for indirect operands.
359366
let field = self.mplace_field(mplace, field)?;
360367
return Ok(field.into());
@@ -397,7 +404,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
397404
} else {
398405
// Indexing into a big array. This must be an mplace.
399406
let mplace = op.assert_mem_place(self);
400-
Ok(self.mplace_index(mplace, index)?.into())
407+
Ok(self.mplace_index(&mplace, index)?.into())
401408
}
402409
}
403410

@@ -408,7 +415,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
408415
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
409416
// Downcasts only change the layout
410417
Ok(match op.try_as_mplace(self) {
411-
Ok(mplace) => self.mplace_downcast(mplace, variant)?.into(),
418+
Ok(ref mplace) => self.mplace_downcast(mplace, variant)?.into(),
412419
Err(..) => {
413420
let layout = op.layout.for_variant(self, variant);
414421
OpTy { layout, ..*op }
@@ -430,7 +437,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
430437
// The rest should only occur as mplace, we do not use Immediates for types
431438
// allowing such operations. This matches place_projection forcing an allocation.
432439
let mplace = base.assert_mem_place(self);
433-
self.mplace_projection(mplace, proj_elem)?.into()
440+
self.mplace_projection(&mplace, proj_elem)?.into()
434441
}
435442
})
436443
}

0 commit comments

Comments
 (0)