Skip to content

Commit ff080d3

Browse files
committed
miri: track the Align instead of packedness in PtrAndAlign.
1 parent 5cab0bf commit ff080d3

File tree

7 files changed

+152
-182
lines changed

7 files changed

+152
-182
lines changed

src/librustc/mir/interpret/value.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(unknown_lints)]
22

3-
use ty::layout::HasDataLayout;
3+
use ty::layout::{Align, HasDataLayout};
44

55
use super::{EvalResult, MemoryPointer, PointerArithmetic};
66
use syntax::ast::FloatTy;
@@ -9,8 +9,7 @@ use rustc_const_math::ConstFloat;
99
#[derive(Copy, Clone, Debug)]
1010
pub struct PtrAndAlign {
1111
pub ptr: Pointer,
12-
/// Remember whether this place is *supposed* to be aligned.
13-
pub aligned: bool,
12+
pub align: Align,
1413
}
1514

1615
impl PtrAndAlign {
@@ -20,7 +19,7 @@ impl PtrAndAlign {
2019
pub fn offset<'tcx, C: HasDataLayout>(self, i: u64, cx: C) -> EvalResult<'tcx, Self> {
2120
Ok(PtrAndAlign {
2221
ptr: self.ptr.offset(i, cx)?,
23-
aligned: self.aligned,
22+
align: self.align,
2423
})
2524
}
2625
}
@@ -182,13 +181,6 @@ pub enum PrimValKind {
182181
Char,
183182
}
184183

185-
impl<'a, 'tcx: 'a> Value {
186-
#[inline]
187-
pub fn by_ref(ptr: Pointer) -> Self {
188-
Value::ByRef(PtrAndAlign { ptr, aligned: true })
189-
}
190-
}
191-
192184
impl<'tcx> PrimVal {
193185
pub fn from_u128(n: u128) -> Self {
194186
PrimVal::Bytes(n)

src/librustc_mir/interpret/const_eval.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub fn eval_body<'a, 'tcx>(
7777
instance,
7878
mir.span,
7979
mir,
80-
Place::from_ptr(ptr),
80+
Place::from_ptr(ptr, layout.align),
8181
cleanup.clone(),
8282
)?;
8383

@@ -357,10 +357,11 @@ pub fn const_eval_provider<'a, 'tcx>(
357357
(_, Err(err)) => Err(err),
358358
(Ok((miri_val, miri_ty)), Ok(ctfe)) => {
359359
let mut ecx = mk_eval_cx(tcx, instance, key.param_env).unwrap();
360-
check_ctfe_against_miri(&mut ecx, PtrAndAlign {
360+
let miri_ptr = PtrAndAlign {
361361
ptr: miri_val,
362-
aligned: true
363-
}, miri_ty, ctfe.val);
362+
align: ecx.layout_of(miri_ty).unwrap().align
363+
};
364+
check_ctfe_against_miri(&mut ecx, miri_ptr, miri_ty, ctfe.val);
364365
Ok(ctfe)
365366
}
366367
}
@@ -380,7 +381,7 @@ fn check_ctfe_against_miri<'a, 'tcx>(
380381
use rustc::ty::TypeVariants::*;
381382
match miri_ty.sty {
382383
TyInt(int_ty) => {
383-
let value = ecx.read_maybe_aligned(miri_val.aligned, |ectx| {
384+
let value = ecx.read_with_align(miri_val.align, |ectx| {
384385
ectx.try_read_value(miri_val.ptr, miri_ty)
385386
});
386387
let prim = get_prim(ecx, value);
@@ -391,7 +392,7 @@ fn check_ctfe_against_miri<'a, 'tcx>(
391392
assert_eq!(c, ctfe, "miri evaluated to {:?}, but ctfe yielded {:?}", c, ctfe);
392393
},
393394
TyUint(uint_ty) => {
394-
let value = ecx.read_maybe_aligned(miri_val.aligned, |ectx| {
395+
let value = ecx.read_with_align(miri_val.align, |ectx| {
395396
ectx.try_read_value(miri_val.ptr, miri_ty)
396397
});
397398
let prim = get_prim(ecx, value);
@@ -402,15 +403,15 @@ fn check_ctfe_against_miri<'a, 'tcx>(
402403
assert_eq!(c, ctfe, "miri evaluated to {:?}, but ctfe yielded {:?}", c, ctfe);
403404
},
404405
TyFloat(ty) => {
405-
let value = ecx.read_maybe_aligned(miri_val.aligned, |ectx| {
406+
let value = ecx.read_with_align(miri_val.align, |ectx| {
406407
ectx.try_read_value(miri_val.ptr, miri_ty)
407408
});
408409
let prim = get_prim(ecx, value);
409410
let f = ConstVal::Float(ConstFloat { bits: prim, ty });
410411
assert_eq!(f, ctfe, "miri evaluated to {:?}, but ctfe yielded {:?}", f, ctfe);
411412
},
412413
TyBool => {
413-
let value = ecx.read_maybe_aligned(miri_val.aligned, |ectx| {
414+
let value = ecx.read_with_align(miri_val.align, |ectx| {
414415
ectx.try_read_value(miri_val.ptr, miri_ty)
415416
});
416417
let bits = get_prim(ecx, value);
@@ -421,7 +422,7 @@ fn check_ctfe_against_miri<'a, 'tcx>(
421422
assert_eq!(b, ctfe, "miri evaluated to {:?}, but ctfe yielded {:?}", b, ctfe);
422423
},
423424
TyChar => {
424-
let value = ecx.read_maybe_aligned(miri_val.aligned, |ectx| {
425+
let value = ecx.read_with_align(miri_val.align, |ectx| {
425426
ectx.try_read_value(miri_val.ptr, miri_ty)
426427
});
427428
let bits = get_prim(ecx, value);
@@ -435,7 +436,7 @@ fn check_ctfe_against_miri<'a, 'tcx>(
435436
}
436437
},
437438
TyStr => {
438-
let value = ecx.read_maybe_aligned(miri_val.aligned, |ectx| {
439+
let value = ecx.read_with_align(miri_val.align, |ectx| {
439440
ectx.try_read_value(miri_val.ptr, miri_ty)
440441
});
441442
if let Ok(Some(Value::ByValPair(PrimVal::Ptr(ptr), PrimVal::Bytes(len)))) = value {
@@ -522,8 +523,7 @@ fn check_ctfe_against_miri<'a, 'tcx>(
522523
Field::new(field),
523524
layout,
524525
).unwrap();
525-
let ptr = place.to_ptr_extra_aligned().0;
526-
check_ctfe_against_miri(ecx, ptr, elem.ty, elem.val);
526+
check_ctfe_against_miri(ecx, place.to_ptr_align(), elem.ty, elem.val);
527527
}
528528
},
529529
TySlice(_) => bug!("miri produced a slice?"),
@@ -543,7 +543,7 @@ fn check_ctfe_against_miri<'a, 'tcx>(
543543
// should be fine
544544
TyFnDef(..) => {}
545545
TyFnPtr(_) => {
546-
let value = ecx.read_maybe_aligned(miri_val.aligned, |ectx| {
546+
let value = ecx.read_with_align(miri_val.align, |ectx| {
547547
ectx.try_read_value(miri_val.ptr, miri_ty)
548548
});
549549
let ptr = match value {

0 commit comments

Comments
 (0)