]> git.proxmox.com Git - rustc.git/blame - src/test/ui/consts/const-size_of-align_of.rs
Update unsuspicious file list
[rustc.git] / src / test / ui / consts / const-size_of-align_of.rs
CommitLineData
b7449926 1// run-pass
0bf4aa26 2#![allow(dead_code)]
3b2f2976
XL
3
4use std::mem;
5
6// Get around the limitations of CTFE in today's Rust.
7const 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
11const fn max_usize(a: usize, b: usize) -> usize {
12 choice_u64(a > b, a as u64, b as u64) as usize
13}
14
15const fn align_to(size: usize, align: usize) -> usize {
16 (size + (align - 1)) & !(align - 1)
17}
18
19const fn packed_union_size_of<A, B>() -> usize {
20 max_usize(mem::size_of::<A>(), mem::size_of::<B>())
21}
22
23const fn union_align_of<A, B>() -> usize {
24 max_usize(mem::align_of::<A>(), mem::align_of::<B>())
25}
26
27const fn union_size_of<A, B>() -> usize {
28 align_to(packed_union_size_of::<A, B>(), union_align_of::<A, B>())
29}
30
31macro_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.
42fake_union!(U { u16, [u8; 3] });
43
44fn test(u: U) {
45 assert_eq!(mem::size_of_val(&u._bytes), 4);
46}
47
48fn main() {
49 assert_eq!(mem::size_of::<U>(), 4);
50 assert_eq!(mem::align_of::<U>(), 2);
51}