Skip to content

Commit 7e244f8

Browse files
committed
interpret: make Writeable trait about a to_place operation
1 parent d689034 commit 7e244f8

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

compiler/rustc_const_eval/src/interpret/operand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
691691
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
692692
match place.as_mplace_or_local() {
693693
Left(mplace) => Ok(mplace.into()),
694-
Right((local, offset, locals_addr)) => {
694+
Right((local, offset, locals_addr, _)) => {
695695
debug_assert!(place.layout.is_sized()); // only sized locals can ever be `Place::Local`.
696696
debug_assert_eq!(locals_addr, self.frame().locals_addr());
697697
let base = self.local_to_op(local, None)?;

compiler/rustc_const_eval/src/interpret/place.rs

+12-17
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,12 @@ impl<'tcx, Prov: Provenance> PlaceTy<'tcx, Prov> {
231231
#[inline(always)]
232232
pub fn as_mplace_or_local(
233233
&self,
234-
) -> Either<MPlaceTy<'tcx, Prov>, (mir::Local, Option<Size>, usize)> {
234+
) -> Either<MPlaceTy<'tcx, Prov>, (mir::Local, Option<Size>, usize, TyAndLayout<'tcx>)> {
235235
match self.place {
236236
Place::Ptr(mplace) => Left(MPlaceTy { mplace, layout: self.layout }),
237-
Place::Local { local, offset, locals_addr } => Right((local, offset, locals_addr)),
237+
Place::Local { local, offset, locals_addr } => {
238+
Right((local, offset, locals_addr, self.layout))
239+
}
238240
}
239241
}
240242

@@ -277,7 +279,7 @@ impl<'tcx, Prov: Provenance> Projectable<'tcx, Prov> for PlaceTy<'tcx, Prov> {
277279
) -> InterpResult<'tcx, Self> {
278280
Ok(match self.as_mplace_or_local() {
279281
Left(mplace) => mplace.offset_with_meta(offset, mode, meta, layout, ecx)?.into(),
280-
Right((local, old_offset, locals_addr)) => {
282+
Right((local, old_offset, locals_addr, _)) => {
281283
debug_assert!(layout.is_sized(), "unsized locals should live in memory");
282284
assert_matches!(meta, MemPlaceMeta::None); // we couldn't store it anyway...
283285
// `Place::Local` are always in-bounds of their surrounding local, so we can just
@@ -328,9 +330,7 @@ impl<'tcx, Prov: Provenance> OpTy<'tcx, Prov> {
328330

329331
/// The `Weiteable` trait describes interpreter values that can be written to.
330332
pub trait Writeable<'tcx, Prov: Provenance>: Projectable<'tcx, Prov> {
331-
fn as_mplace_or_local(
332-
&self,
333-
) -> Either<MPlaceTy<'tcx, Prov>, (mir::Local, Option<Size>, usize, TyAndLayout<'tcx>)>;
333+
fn to_place(&self) -> PlaceTy<'tcx, Prov>;
334334

335335
fn force_mplace<M: Machine<'tcx, Provenance = Prov>>(
336336
&self,
@@ -340,11 +340,8 @@ pub trait Writeable<'tcx, Prov: Provenance>: Projectable<'tcx, Prov> {
340340

341341
impl<'tcx, Prov: Provenance> Writeable<'tcx, Prov> for PlaceTy<'tcx, Prov> {
342342
#[inline(always)]
343-
fn as_mplace_or_local(
344-
&self,
345-
) -> Either<MPlaceTy<'tcx, Prov>, (mir::Local, Option<Size>, usize, TyAndLayout<'tcx>)> {
346-
self.as_mplace_or_local()
347-
.map_right(|(local, offset, locals_addr)| (local, offset, locals_addr, self.layout))
343+
fn to_place(&self) -> PlaceTy<'tcx, Prov> {
344+
self.clone()
348345
}
349346

350347
#[inline(always)]
@@ -358,10 +355,8 @@ impl<'tcx, Prov: Provenance> Writeable<'tcx, Prov> for PlaceTy<'tcx, Prov> {
358355

359356
impl<'tcx, Prov: Provenance> Writeable<'tcx, Prov> for MPlaceTy<'tcx, Prov> {
360357
#[inline(always)]
361-
fn as_mplace_or_local(
362-
&self,
363-
) -> Either<MPlaceTy<'tcx, Prov>, (mir::Local, Option<Size>, usize, TyAndLayout<'tcx>)> {
364-
Left(self.clone())
358+
fn to_place(&self) -> PlaceTy<'tcx, Prov> {
359+
self.clone().into()
365360
}
366361

367362
#[inline(always)]
@@ -613,7 +608,7 @@ where
613608

614609
// See if we can avoid an allocation. This is the counterpart to `read_immediate_raw`,
615610
// but not factored as a separate function.
616-
let mplace = match dest.as_mplace_or_local() {
611+
let mplace = match dest.to_place().as_mplace_or_local() {
617612
Right((local, offset, locals_addr, layout)) => {
618613
if offset.is_some() {
619614
// This has been projected to a part of this local. We could have complicated
@@ -726,7 +721,7 @@ where
726721
&mut self,
727722
dest: &impl Writeable<'tcx, M::Provenance>,
728723
) -> InterpResult<'tcx> {
729-
let mplace = match dest.as_mplace_or_local() {
724+
let mplace = match dest.to_place().as_mplace_or_local() {
730725
Left(mplace) => mplace,
731726
Right((local, offset, locals_addr, layout)) => {
732727
if offset.is_some() {

0 commit comments

Comments
 (0)