1
Fork 0

Rollup merge of #98968 - RalfJung:scalar-sanity, r=oli-obk

assert Scalar sanity

With https://github.com/rust-lang/rust/pull/96814 having landed, finally our `Scalar` layouts have the invariants they deserve. :)
This commit is contained in:
Dylan DPC 2022-07-06 14:49:13 +05:30 committed by GitHub
commit 7f62a719af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 7 deletions

View file

@ -306,9 +306,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
s.is_ptr() || (number_may_have_provenance && size == self.pointer_size()) s.is_ptr() || (number_may_have_provenance && size == self.pointer_size())
}; };
if let Some(s) = scalar_layout { if let Some(s) = scalar_layout {
//FIXME(#96185): let size = s.size(self); let size = s.size(self);
//FIXME(#96185): assert_eq!(size, mplace.layout.size, "abi::Scalar size does not match layout size"); assert_eq!(size, mplace.layout.size, "abi::Scalar size does not match layout size");
let size = mplace.layout.size; //FIXME(#96185): remove this line
let scalar = let scalar =
alloc.read_scalar(alloc_range(Size::ZERO, size), read_provenance(s, size))?; alloc.read_scalar(alloc_range(Size::ZERO, size), read_provenance(s, size))?;
return Ok(Some(ImmTy { imm: scalar.into(), layout: mplace.layout })); return Ok(Some(ImmTy { imm: scalar.into(), layout: mplace.layout }));

View file

@ -793,7 +793,7 @@ where
) )
}; };
let size = s.size(&tcx); let size = s.size(&tcx);
//FIXME(#96185): assert_eq!(dest.layout.size, size, "abi::Scalar size does not match layout size"); assert_eq!(size, dest.layout.size, "abi::Scalar size does not match layout size");
alloc.write_scalar(alloc_range(Size::ZERO, size), scalar) alloc.write_scalar(alloc_range(Size::ZERO, size), scalar)
} }
Immediate::ScalarPair(a_val, b_val) => { Immediate::ScalarPair(a_val, b_val) => {

View file

@ -235,9 +235,8 @@ fn sanity_check_layout<'tcx>(
if cfg!(debug_assertions) { if cfg!(debug_assertions) {
fn check_layout_abi<'tcx>(tcx: TyCtxt<'tcx>, layout: Layout<'tcx>) { fn check_layout_abi<'tcx>(tcx: TyCtxt<'tcx>, layout: Layout<'tcx>) {
match layout.abi() { match layout.abi() {
Abi::Scalar(_scalar) => { Abi::Scalar(scalar) => {
// No padding in scalars. // No padding in scalars.
/* FIXME(#96185):
assert_eq!( assert_eq!(
layout.align().abi, layout.align().abi,
scalar.align(&tcx).abi, scalar.align(&tcx).abi,
@ -247,7 +246,7 @@ fn sanity_check_layout<'tcx>(
layout.size(), layout.size(),
scalar.size(&tcx), scalar.size(&tcx),
"size mismatch between ABI and layout in {layout:#?}" "size mismatch between ABI and layout in {layout:#?}"
);*/ );
} }
Abi::Vector { count, element } => { Abi::Vector { count, element } => {
// No padding in vectors. Alignment can be strengthened, though. // No padding in vectors. Alignment can be strengthened, though.

View file

@ -4,6 +4,19 @@
enum A { A1, A2 } enum A { A1, A2 }
enum B { B1=4, B2=2 } enum B { B1=4, B2=2 }
#[allow(dead_code)]
#[repr(align(8))]
enum Aligned {
Zero = 0,
One = 1,
}
// regression test for https://github.com/rust-lang/rust/issues/96185
const X: u8 = {
let aligned = Aligned::Zero;
aligned as u8
};
pub fn main () { pub fn main () {
static c1: isize = A::A2 as isize; static c1: isize = A::A2 as isize;
static c2: isize = B::B2 as isize; static c2: isize = B::B2 as isize;
@ -23,4 +36,6 @@ pub fn main () {
assert_eq!(c2_2, 4); assert_eq!(c2_2, 4);
assert_eq!(a1_2, 0); assert_eq!(a1_2, 0);
assert_eq!(a2_2, 4); assert_eq!(a2_2, 4);
assert_eq!(X, 0);
} }