Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 00fb45d

Browse files
committed
interpret: make write functions generic over the place type
1 parent 4fc6b33 commit 00fb45d

File tree

26 files changed

+240
-210
lines changed

26 files changed

+240
-210
lines changed

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ fn valtree_into_mplace<'tcx>(
345345
ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char => {
346346
let scalar_int = valtree.unwrap_leaf();
347347
debug!("writing trivial valtree {:?} to place {:?}", scalar_int, place);
348-
ecx.write_immediate(Immediate::Scalar(scalar_int.into()), &place.into()).unwrap();
348+
ecx.write_immediate(Immediate::Scalar(scalar_int.into()), place).unwrap();
349349
}
350350
ty::Ref(_, inner_ty, _) => {
351351
let mut pointee_place = create_pointee_place(ecx, *inner_ty, valtree);
@@ -369,7 +369,7 @@ fn valtree_into_mplace<'tcx>(
369369
};
370370
debug!(?imm);
371371

372-
ecx.write_immediate(imm, &place.into()).unwrap();
372+
ecx.write_immediate(imm, place).unwrap();
373373
}
374374
ty::Adt(_, _) | ty::Tuple(_) | ty::Array(_, _) | ty::Str | ty::Slice(_) => {
375375
let branches = valtree.unwrap_branch();
@@ -452,11 +452,11 @@ fn valtree_into_mplace<'tcx>(
452452

453453
if let Some(variant_idx) = variant_idx {
454454
// don't forget filling the place with the discriminant of the enum
455-
ecx.write_discriminant(variant_idx, &place.into()).unwrap();
455+
ecx.write_discriminant(variant_idx, place).unwrap();
456456
}
457457

458458
debug!("dump of place after writing discriminant:");
459-
dump_place(ecx, place.into());
459+
dump_place(ecx, place.clone().into());
460460
}
461461
_ => bug!("shouldn't have created a ValTree for {:?}", ty),
462462
}

compiler/rustc_const_eval/src/interpret/discriminant.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,28 @@ use rustc_middle::{mir, ty};
55
use rustc_target::abi::{self, TagEncoding};
66
use rustc_target::abi::{VariantIdx, Variants};
77

8-
use super::{ImmTy, InterpCx, InterpResult, Machine, OpTy, PlaceTy, Scalar};
8+
use super::place::Writeable;
9+
use super::{ImmTy, InterpCx, InterpResult, Machine, OpTy, Scalar};
910

1011
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
1112
/// Writes the discriminant of the given variant.
1213
#[instrument(skip(self), level = "trace")]
1314
pub fn write_discriminant(
1415
&mut self,
1516
variant_index: VariantIdx,
16-
dest: &PlaceTy<'tcx, M::Provenance>,
17+
dest: &impl Writeable<'tcx, M::Provenance>,
1718
) -> InterpResult<'tcx> {
1819
// Layout computation excludes uninhabited variants from consideration
1920
// therefore there's no way to represent those variants in the given layout.
2021
// Essentially, uninhabited variants do not have a tag that corresponds to their
2122
// discriminant, so we cannot do anything here.
2223
// When evaluating we will always error before even getting here, but ConstProp 'executes'
2324
// dead code, so we cannot ICE here.
24-
if dest.layout.for_variant(self, variant_index).abi.is_uninhabited() {
25+
if dest.layout().for_variant(self, variant_index).abi.is_uninhabited() {
2526
throw_ub!(UninhabitedEnumVariantWritten(variant_index))
2627
}
2728

28-
match dest.layout.variants {
29+
match dest.layout().variants {
2930
abi::Variants::Single { index } => {
3031
assert_eq!(index, variant_index);
3132
}
@@ -38,8 +39,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
3839
// No need to validate that the discriminant here because the
3940
// `TyAndLayout::for_variant()` call earlier already checks the variant is valid.
4041

41-
let discr_val =
42-
dest.layout.ty.discriminant_for_variant(*self.tcx, variant_index).unwrap().val;
42+
let discr_val = dest
43+
.layout()
44+
.ty
45+
.discriminant_for_variant(*self.tcx, variant_index)
46+
.unwrap()
47+
.val;
4348

4449
// raw discriminants for enums are isize or bigger during
4550
// their computation, but the in-memory tag is the smallest possible

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
432432
} else {
433433
self.project_index(&input, i)?.into()
434434
};
435-
self.copy_op(&value, &place.into(), /*allow_transmute*/ false)?;
435+
self.copy_op(&value, &place, /*allow_transmute*/ false)?;
436436
}
437437
}
438438
sym::simd_extract => {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
101101
let location = self.allocate(loc_layout, MemoryKind::CallerLocation).unwrap();
102102

103103
// Initialize fields.
104-
self.write_immediate(file.to_ref(self), &self.project_field(&location, 0).unwrap().into())
104+
self.write_immediate(file.to_ref(self), &self.project_field(&location, 0).unwrap())
105105
.expect("writing to memory we just allocated cannot fail");
106-
self.write_scalar(line, &self.project_field(&location, 1).unwrap().into())
106+
self.write_scalar(line, &self.project_field(&location, 1).unwrap())
107107
.expect("writing to memory we just allocated cannot fail");
108-
self.write_scalar(col, &self.project_field(&location, 2).unwrap().into())
108+
self.write_scalar(col, &self.project_field(&location, 2).unwrap())
109109
.expect("writing to memory we just allocated cannot fail");
110110

111111
location

compiler/rustc_const_eval/src/interpret/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub use self::intern::{intern_const_alloc_recursive, InternKind};
2525
pub use self::machine::{compile_time_machine, AllocMap, Machine, MayLeak, StackPopJump};
2626
pub use self::memory::{AllocKind, AllocRef, AllocRefMut, FnVal, Memory, MemoryKind};
2727
pub use self::operand::{ImmTy, Immediate, OpTy, Operand};
28-
pub use self::place::{MPlaceTy, MemPlace, MemPlaceMeta, Place, PlaceTy};
28+
pub use self::place::{MPlaceTy, MemPlace, MemPlaceMeta, Place, PlaceTy, Writeable};
2929
pub use self::projection::Projectable;
3030
pub use self::terminator::FnArg;
3131
pub use self::validity::{CtfeValidationMode, RefTracking};

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,13 @@ impl<'tcx, Prov: Provenance> ImmTy<'tcx, Prov> {
312312
}
313313
}
314314

315-
impl<'mir, 'tcx: 'mir, Prov: Provenance> Projectable<'mir, 'tcx, Prov> for ImmTy<'tcx, Prov> {
315+
impl<'tcx, Prov: Provenance> Projectable<'tcx, Prov> for ImmTy<'tcx, Prov> {
316316
#[inline(always)]
317317
fn layout(&self) -> TyAndLayout<'tcx> {
318318
self.layout
319319
}
320320

321-
fn meta<M: Machine<'mir, 'tcx, Provenance = Prov>>(
321+
fn meta<'mir, M: Machine<'mir, 'tcx, Provenance = Prov>>(
322322
&self,
323323
_ecx: &InterpCx<'mir, 'tcx, M>,
324324
) -> InterpResult<'tcx, MemPlaceMeta<M::Provenance>> {
@@ -337,7 +337,7 @@ impl<'mir, 'tcx: 'mir, Prov: Provenance> Projectable<'mir, 'tcx, Prov> for ImmTy
337337
Ok(self.offset_(offset, layout, cx))
338338
}
339339

340-
fn to_op<M: Machine<'mir, 'tcx, Provenance = Prov>>(
340+
fn to_op<'mir, M: Machine<'mir, 'tcx, Provenance = Prov>>(
341341
&self,
342342
_ecx: &InterpCx<'mir, 'tcx, M>,
343343
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
@@ -362,15 +362,13 @@ impl<'tcx, Prov: Provenance> OpTy<'tcx, Prov> {
362362
}
363363
}
364364

365-
impl<'mir, 'tcx: 'mir, Prov: Provenance + 'static> Projectable<'mir, 'tcx, Prov>
366-
for OpTy<'tcx, Prov>
367-
{
365+
impl<'tcx, Prov: Provenance + 'static> Projectable<'tcx, Prov> for OpTy<'tcx, Prov> {
368366
#[inline(always)]
369367
fn layout(&self) -> TyAndLayout<'tcx> {
370368
self.layout
371369
}
372370

373-
fn meta<M: Machine<'mir, 'tcx, Provenance = Prov>>(
371+
fn meta<'mir, M: Machine<'mir, 'tcx, Provenance = Prov>>(
374372
&self,
375373
_ecx: &InterpCx<'mir, 'tcx, M>,
376374
) -> InterpResult<'tcx, MemPlaceMeta<M::Provenance>> {
@@ -394,7 +392,7 @@ impl<'mir, 'tcx: 'mir, Prov: Provenance + 'static> Projectable<'mir, 'tcx, Prov>
394392
}
395393
}
396394

397-
fn to_op<M: Machine<'mir, 'tcx, Provenance = Prov>>(
395+
fn to_op<'mir, M: Machine<'mir, 'tcx, Provenance = Prov>>(
398396
&self,
399397
_ecx: &InterpCx<'mir, 'tcx, M>,
400398
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {

0 commit comments

Comments
 (0)