Skip to content

Commit e1e79c3

Browse files
committed
Alignment of const_field results must be of the field's type
We're taking the value of the field, any future accesses to the constant will not know about this, so we need to adjust the allocation's alignment.
1 parent d86deda commit e1e79c3

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/librustc_mir/const_eval.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,19 @@ pub fn mplace_to_const<'tcx>(
100100
ecx: &CompileTimeEvalContext<'_, '_, 'tcx>,
101101
mplace: MPlaceTy<'tcx>,
102102
) -> EvalResult<'tcx, &'tcx ty::Const<'tcx>> {
103-
let MemPlace { ptr, align, meta } = *mplace;
104103
// extract alloc-offset pair
105-
assert!(meta.is_none());
106-
let ptr = ptr.to_ptr()?;
104+
assert!(mplace.meta.is_none());
105+
let ptr = mplace.ptr.to_ptr()?;
107106
let alloc = ecx.memory.get(ptr.alloc_id)?;
108-
assert!(alloc.align.abi() >= align.abi());
107+
assert!(alloc.align.abi() >= mplace.align.abi());
109108
assert!(alloc.bytes.len() as u64 - ptr.offset.bytes() >= mplace.layout.size.bytes());
109+
// FIXME: only clone the parts that interest us (starting at offset, going to offset + size)
110110
let mut alloc = alloc.clone();
111-
alloc.align = align;
111+
// we take `mplace.layout.align` instead of `mplace.align`
112+
// as this function is essentially copying the value
113+
// out of the larger allocation, so we lose all information about
114+
// potential surrounding types with different alignment.
115+
alloc.align = mplace.layout.align;
112116
// FIXME shouldnt it be the case that `intern_static` has already
113117
// interned this? I thought that is the entire point of that `FinishStatic` stuff?
114118
let alloc = ecx.tcx.intern_const_alloc(alloc);

0 commit comments

Comments
 (0)