Skip to content

Commit 7f62a71

Browse files
authored
Rollup merge of rust-lang#98968 - RalfJung:scalar-sanity, r=oli-obk
assert Scalar sanity With rust-lang#96814 having landed, finally our `Scalar` layouts have the invariants they deserve. :)
2 parents 41e30e3 + 2a1a718 commit 7f62a71

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

compiler/rustc_const_eval/src/interpret/operand.rs

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

compiler/rustc_const_eval/src/interpret/place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ where
793793
)
794794
};
795795
let size = s.size(&tcx);
796-
//FIXME(#96185): assert_eq!(dest.layout.size, size, "abi::Scalar size does not match layout size");
796+
assert_eq!(size, dest.layout.size, "abi::Scalar size does not match layout size");
797797
alloc.write_scalar(alloc_range(Size::ZERO, size), scalar)
798798
}
799799
Immediate::ScalarPair(a_val, b_val) => {

compiler/rustc_middle/src/ty/layout.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,8 @@ fn sanity_check_layout<'tcx>(
235235
if cfg!(debug_assertions) {
236236
fn check_layout_abi<'tcx>(tcx: TyCtxt<'tcx>, layout: Layout<'tcx>) {
237237
match layout.abi() {
238-
Abi::Scalar(_scalar) => {
238+
Abi::Scalar(scalar) => {
239239
// No padding in scalars.
240-
/* FIXME(#96185):
241240
assert_eq!(
242241
layout.align().abi,
243242
scalar.align(&tcx).abi,
@@ -247,7 +246,7 @@ fn sanity_check_layout<'tcx>(
247246
layout.size(),
248247
scalar.size(&tcx),
249248
"size mismatch between ABI and layout in {layout:#?}"
250-
);*/
249+
);
251250
}
252251
Abi::Vector { count, element } => {
253252
// No padding in vectors. Alignment can be strengthened, though.

src/test/ui/consts/const-enum-cast.rs

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44
enum A { A1, A2 }
55
enum B { B1=4, B2=2 }
66

7+
#[allow(dead_code)]
8+
#[repr(align(8))]
9+
enum Aligned {
10+
Zero = 0,
11+
One = 1,
12+
}
13+
14+
// regression test for https://github.com/rust-lang/rust/issues/96185
15+
const X: u8 = {
16+
let aligned = Aligned::Zero;
17+
aligned as u8
18+
};
19+
720
pub fn main () {
821
static c1: isize = A::A2 as isize;
922
static c2: isize = B::B2 as isize;
@@ -23,4 +36,6 @@ pub fn main () {
2336
assert_eq!(c2_2, 4);
2437
assert_eq!(a1_2, 0);
2538
assert_eq!(a2_2, 4);
39+
40+
assert_eq!(X, 0);
2641
}

0 commit comments

Comments
 (0)