]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/const-size_of_val-align_of_val.rs
Add 'compiler/rustc_codegen_cranelift/' from commit '793d26047f994e23415f8f6bb5686ff2...
[rust.git] / src / test / ui / consts / const-size_of_val-align_of_val.rs
1 // run-pass
2
3 #![feature(const_size_of_val, const_align_of_val)]
4
5 use std::mem;
6
7 struct Foo(u32);
8
9 #[derive(Clone, Copy)]
10 struct Bar {
11     _x: u8,
12     _y: u16,
13     _z: u8,
14 }
15
16 union Ugh {
17     _a: [u8; 3],
18     _b: Bar,
19 }
20
21 const FOO: Foo = Foo(4);
22 const BAR: Bar = Bar { _x: 4, _y: 1, _z: 2 };
23 const UGH: Ugh = Ugh { _a: [0; 3] };
24
25 const SIZE_OF_FOO: usize = mem::size_of_val(&FOO);
26 const SIZE_OF_BAR: usize = mem::size_of_val(&BAR);
27 const SIZE_OF_UGH: usize = mem::size_of_val(&UGH);
28
29 const ALIGN_OF_FOO: usize = mem::align_of_val(&FOO);
30 const ALIGN_OF_BAR: usize = mem::align_of_val(&BAR);
31 const ALIGN_OF_UGH: usize = mem::align_of_val(&UGH);
32
33 const SIZE_OF_SLICE: usize = mem::size_of_val("foobar".as_bytes());
34
35 fn main() {
36     assert_eq!(SIZE_OF_FOO, mem::size_of::<Foo>());
37     assert_eq!(SIZE_OF_BAR, mem::size_of::<Bar>());
38     assert_eq!(SIZE_OF_UGH, mem::size_of::<Ugh>());
39
40     assert_eq!(ALIGN_OF_FOO, mem::align_of::<Foo>());
41     assert_eq!(ALIGN_OF_BAR, mem::align_of::<Bar>());
42     assert_eq!(ALIGN_OF_UGH, mem::align_of::<Ugh>());
43
44     assert_eq!(SIZE_OF_SLICE, "foobar".len());
45 }