Skip to content

Commit 2287219

Browse files
committed
Factor out mplace offsetting into its own method
1 parent 972d798 commit 2287219

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

src/librustc_mir/interpret/place.rs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ impl<Tag> MemPlace<Tag> {
159159
Some(meta) => Immediate::ScalarPair(self.ptr.into(), meta.into()),
160160
}
161161
}
162+
163+
pub fn offset(
164+
self,
165+
offset: Size,
166+
meta: Option<Scalar<Tag>>,
167+
cx: &impl HasDataLayout,
168+
) -> EvalResult<'tcx, Self> {
169+
Ok(MemPlace {
170+
ptr: self.ptr.ptr_offset(offset, cx)?,
171+
align: self.align.restrict_for_offset(offset),
172+
meta,
173+
})
174+
}
162175
}
163176

164177
impl<'tcx, Tag> MPlaceTy<'tcx, Tag> {
@@ -174,6 +187,19 @@ impl<'tcx, Tag> MPlaceTy<'tcx, Tag> {
174187
}
175188
}
176189

190+
pub fn offset(
191+
self,
192+
offset: Size,
193+
meta: Option<Scalar<Tag>>,
194+
layout: TyLayout<'tcx>,
195+
cx: &impl HasDataLayout,
196+
) -> EvalResult<'tcx, Self> {
197+
Ok(MPlaceTy {
198+
mplace: self.mplace.offset(offset, meta, cx)?,
199+
layout,
200+
})
201+
}
202+
177203
#[inline]
178204
fn from_aligned_ptr(ptr: Pointer<Tag>, layout: TyLayout<'tcx>) -> Self {
179205
MPlaceTy { mplace: MemPlace::from_ptr(ptr, layout.align.abi), layout }
@@ -367,13 +393,9 @@ where
367393
(None, offset)
368394
};
369395

370-
let ptr = base.ptr.ptr_offset(offset, self)?;
371-
let align = base.align
372-
// We do not look at `base.layout.align` nor `field_layout.align`, unlike
373-
// codegen -- mostly to see if we can get away with that
374-
.restrict_for_offset(offset); // must be last thing that happens
375-
376-
Ok(MPlaceTy { mplace: MemPlace { ptr, align, meta }, layout: field_layout })
396+
// We do not look at `base.layout.align` nor `field_layout.align`, unlike
397+
// codegen -- mostly to see if we can get away with that
398+
base.offset(offset, meta, field_layout, self)
377399
}
378400

379401
// Iterates over all fields of an array. Much more efficient than doing the
@@ -391,14 +413,7 @@ where
391413
};
392414
let layout = base.layout.field(self, 0)?;
393415
let dl = &self.tcx.data_layout;
394-
Ok((0..len).map(move |i| {
395-
let ptr = base.ptr.ptr_offset(i * stride, dl)?;
396-
let align = base.align.restrict_for_offset(i * stride);
397-
Ok(MPlaceTy {
398-
mplace: MemPlace { ptr, align, meta: None },
399-
layout
400-
})
401-
}))
416+
Ok((0..len).map(move |i| base.offset(i * stride, None, layout, dl)))
402417
}
403418

404419
pub fn mplace_subslice(
@@ -417,8 +432,6 @@ where
417432
stride * from,
418433
_ => bug!("Unexpected layout of index access: {:#?}", base.layout),
419434
};
420-
let ptr = base.ptr.ptr_offset(from_offset, self)?;
421-
let align = base.align.restrict_for_offset(from_offset);
422435

423436
// Compute meta and new layout
424437
let inner_len = len - to - from;
@@ -435,11 +448,7 @@ where
435448
bug!("cannot subslice non-array type: `{:?}`", base.layout.ty),
436449
};
437450
let layout = self.layout_of(ty)?;
438-
439-
Ok(MPlaceTy {
440-
mplace: MemPlace { ptr, align, meta },
441-
layout
442-
})
451+
base.offset(from_offset, meta, layout, self)
443452
}
444453

445454
pub fn mplace_downcast(

0 commit comments

Comments
 (0)