Skip to content

Commit a5ef2d1

Browse files
committed
Array and slice projections need to update the place alignment
1 parent 10102d1 commit a5ef2d1

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/librustc_mir/interpret/operand.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,13 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
271271
if mplace.layout.is_zst() {
272272
// Not all ZSTs have a layout we would handle below, so just short-circuit them
273273
// all here.
274-
self.memory.check_align(ptr, ptr_align.min(mplace.layout.align))?;
274+
self.memory.check_align(ptr, ptr_align)?;
275275
return Ok(Some(Immediate::Scalar(Scalar::zst().into())));
276276
}
277277

278278
// check for integer pointers before alignment to report better errors
279279
let ptr = ptr.to_ptr()?;
280-
self.memory.check_align(ptr.into(), ptr_align.min(mplace.layout.align))?;
280+
self.memory.check_align(ptr.into(), ptr_align)?;
281281
match mplace.layout.abi {
282282
layout::Abi::Scalar(..) => {
283283
let scalar = self.memory
@@ -295,7 +295,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
295295
let a_val = self.memory
296296
.get(ptr.alloc_id)?
297297
.read_scalar(self, a_ptr, a_size)?;
298-
self.memory.check_align(b_ptr.into(), b.align(self).min(ptr_align))?;
298+
let b_align = ptr_align.restrict_for_offset(b_offset);
299+
self.memory.check_align(b_ptr.into(), b_align)?;
299300
let b_val = self.memory
300301
.get(ptr.alloc_id)?
301302
.read_scalar(self, b_ptr, b_size)?;

src/librustc_mir/interpret/place.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,9 @@ where
393393
let dl = &self.tcx.data_layout;
394394
Ok((0..len).map(move |i| {
395395
let ptr = base.ptr.ptr_offset(i * stride, dl)?;
396+
let align = base.align.restrict_for_offset(i * stride);
396397
Ok(MPlaceTy {
397-
mplace: MemPlace { ptr, align: base.align, meta: None },
398+
mplace: MemPlace { ptr, align, meta: None },
398399
layout
399400
})
400401
}))
@@ -417,6 +418,7 @@ where
417418
_ => bug!("Unexpected layout of index access: {:#?}", base.layout),
418419
};
419420
let ptr = base.ptr.ptr_offset(from_offset, self)?;
421+
let align = base.align.restrict_for_offset(from_offset);
420422

421423
// Compute meta and new layout
422424
let inner_len = len - to - from;
@@ -435,7 +437,7 @@ where
435437
let layout = self.layout_of(ty)?;
436438

437439
Ok(MPlaceTy {
438-
mplace: MemPlace { ptr, align: base.align, meta },
440+
mplace: MemPlace { ptr, align, meta },
439441
layout
440442
})
441443
}
@@ -741,11 +743,11 @@ where
741743
dest.layout)
742744
};
743745
let (a_size, b_size) = (a.size(self), b.size(self));
744-
let b_align = b.align(self).abi;
745-
let b_offset = a_size.align_to(b_align);
746+
let b_offset = a_size.align_to(b.align(self).abi);
747+
let b_align = ptr_align.restrict_for_offset(b_offset);
746748
let b_ptr = ptr.offset(b_offset, self)?;
747749

748-
self.memory.check_align(b_ptr.into(), ptr_align.min(b_align))?;
750+
self.memory.check_align(b_ptr.into(), b_align)?;
749751

750752
// It is tempting to verify `b_offset` against `layout.fields.offset(1)`,
751753
// but that does not work: We could be a newtype around a pair, then the

0 commit comments

Comments
 (0)