]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-size_of-align_of.rs
Rollup merge of #72279 - RalfJung:raw-ref-macros, r=nikomatsakis
[rust.git] / src / test / ui / consts / const-size_of-align_of.rs
1 // run-pass
2 #![allow(dead_code)]
3
4 use std::mem;
5
6 // Get around the limitations of CTFE in today's Rust.
7 const fn choice_u64(c: bool, a: u64, b: u64) -> u64 {
8     (-(c as i64) as u64) & a | (-(!c as i64) as u64) & b
9 }
10
11 const fn max_usize(a: usize, b: usize) -> usize {
12     choice_u64(a > b, a as u64, b as u64) as usize
13 }
14
15 const fn align_to(size: usize, align: usize) -> usize {
16     (size + (align - 1)) & !(align - 1)
17 }
18
19 const fn packed_union_size_of<A, B>() -> usize {
20     max_usize(mem::size_of::<A>(), mem::size_of::<B>())
21 }
22
23 const fn union_align_of<A, B>() -> usize {
24     max_usize(mem::align_of::<A>(), mem::align_of::<B>())
25 }
26
27 const fn union_size_of<A, B>() -> usize {
28     align_to(packed_union_size_of::<A, B>(), union_align_of::<A, B>())
29 }
30
31 macro_rules! fake_union {
32     ($name:ident { $a:ty, $b:ty }) => (
33         struct $name {
34             _align: ([$a; 0], [$b; 0]),
35             _bytes: [u8; union_size_of::<$a, $b>()]
36         }
37     )
38 }
39
40 // Check that we can (poorly) emulate unions by
41 // calling size_of and align_of at compile-time.
42 fake_union!(U { u16, [u8; 3] });
43
44 fn test(u: U) {
45     assert_eq!(mem::size_of_val(&u._bytes), 4);
46 }
47
48 fn main() {
49     assert_eq!(mem::size_of::<U>(), 4);
50     assert_eq!(mem::align_of::<U>(), 2);
51 }