Skip to content

Commit c4cb043

Browse files
committedJul 15, 2022
interpret/visitor: support visiting with a PlaceTy
1 parent 6c6cccd commit c4cb043

File tree

5 files changed

+268
-48
lines changed

5 files changed

+268
-48
lines changed
 

‎compiler/rustc_const_eval/src/const_eval/valtrees.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ fn valtree_into_mplace<'tcx>(
436436

437437
let offset = place_adjusted.layout.fields.offset(i);
438438
place
439-
.offset(
439+
.offset_with_meta(
440440
offset,
441441
MemPlaceMeta::Meta(Scalar::from_machine_usize(
442442
num_elems as u64,

‎compiler/rustc_const_eval/src/interpret/operand.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,15 @@ impl<'tcx, Tag: Provenance> OpTy<'tcx, Tag> {
297297
}
298298
}
299299

300-
pub fn offset(
300+
pub fn offset_with_meta(
301301
&self,
302302
offset: Size,
303303
meta: MemPlaceMeta<Tag>,
304304
layout: TyAndLayout<'tcx>,
305305
cx: &impl HasDataLayout,
306306
) -> InterpResult<'tcx, Self> {
307307
match self.try_as_mplace() {
308-
Ok(mplace) => Ok(mplace.offset(offset, meta, layout, cx)?.into()),
308+
Ok(mplace) => Ok(mplace.offset_with_meta(offset, meta, layout, cx)?.into()),
309309
Err(imm) => {
310310
assert!(
311311
matches!(*imm, Immediate::Uninit),
@@ -317,6 +317,16 @@ impl<'tcx, Tag: Provenance> OpTy<'tcx, Tag> {
317317
}
318318
}
319319
}
320+
321+
pub fn offset(
322+
&self,
323+
offset: Size,
324+
layout: TyAndLayout<'tcx>,
325+
cx: &impl HasDataLayout,
326+
) -> InterpResult<'tcx, Self> {
327+
assert!(!layout.is_unsized());
328+
self.offset_with_meta(offset, MemPlaceMeta::None, layout, cx)
329+
}
320330
}
321331

322332
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {

‎compiler/rustc_const_eval/src/interpret/place.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<Tag: Provenance> MemPlace<Tag> {
171171
}
172172

173173
#[inline]
174-
pub fn offset<'tcx>(
174+
pub fn offset_with_meta<'tcx>(
175175
self,
176176
offset: Size,
177177
meta: MemPlaceMeta<Tag>,
@@ -205,20 +205,30 @@ impl<'tcx, Tag: Provenance> MPlaceTy<'tcx, Tag> {
205205
}
206206

207207
#[inline]
208-
pub fn offset(
208+
pub fn offset_with_meta(
209209
&self,
210210
offset: Size,
211211
meta: MemPlaceMeta<Tag>,
212212
layout: TyAndLayout<'tcx>,
213213
cx: &impl HasDataLayout,
214214
) -> InterpResult<'tcx, Self> {
215215
Ok(MPlaceTy {
216-
mplace: self.mplace.offset(offset, meta, cx)?,
216+
mplace: self.mplace.offset_with_meta(offset, meta, cx)?,
217217
align: self.align.restrict_for_offset(offset),
218218
layout,
219219
})
220220
}
221221

222+
pub fn offset(
223+
&self,
224+
offset: Size,
225+
layout: TyAndLayout<'tcx>,
226+
cx: &impl HasDataLayout,
227+
) -> InterpResult<'tcx, Self> {
228+
assert!(!layout.is_unsized());
229+
self.offset_with_meta(offset, MemPlaceMeta::None, layout, cx)
230+
}
231+
222232
#[inline]
223233
pub fn from_aligned_ptr(ptr: Pointer<Option<Tag>>, layout: TyAndLayout<'tcx>) -> Self {
224234
MPlaceTy { mplace: MemPlace::from_ptr(ptr), layout, align: layout.align.abi }

‎compiler/rustc_const_eval/src/interpret/projection.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ where
6363

6464
// We do not look at `base.layout.align` nor `field_layout.align`, unlike
6565
// codegen -- mostly to see if we can get away with that
66-
base.offset(offset, meta, field_layout, self)
66+
base.offset_with_meta(offset, meta, field_layout, self)
6767
}
6868

6969
/// Gets the place of a field inside the place, and also the field's type.
@@ -193,9 +193,7 @@ where
193193
let offset = stride * index; // `Size` multiplication
194194
// All fields have the same layout.
195195
let field_layout = base.layout.field(self, 0);
196-
assert!(!field_layout.is_unsized());
197-
198-
base.offset(offset, MemPlaceMeta::None, field_layout, self)
196+
base.offset(offset, field_layout, self)
199197
}
200198
_ => span_bug!(
201199
self.cur_span(),
@@ -215,10 +213,10 @@ where
215213
let abi::FieldsShape::Array { stride, .. } = base.layout.fields else {
216214
span_bug!(self.cur_span(), "operand_array_fields: expected an array layout");
217215
};
218-
let layout = base.layout.field(self, 0);
216+
let field_layout = base.layout.field(self, 0);
219217
let dl = &self.tcx.data_layout;
220218
// `Size` multiplication
221-
Ok((0..len).map(move |i| base.offset(stride * i, MemPlaceMeta::None, layout, dl)))
219+
Ok((0..len).map(move |i| base.offset(stride * i, field_layout, dl)))
222220
}
223221

224222
/// Index into an array.
@@ -326,7 +324,7 @@ where
326324
}
327325
};
328326
let layout = self.layout_of(ty)?;
329-
base.offset(from_offset, meta, layout, self)
327+
base.offset_with_meta(from_offset, meta, layout, self)
330328
}
331329

332330
pub fn place_subslice(

0 commit comments

Comments
 (0)
Please sign in to comment.