]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/type-sizes.rs
Don't ICE on tuple struct ctor with incorrect arg count
[rust.git] / src / test / run-pass / type-sizes.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(never_type)]
12
13 use std::mem::size_of;
14
15 struct t {a: u8, b: i8}
16 struct u {a: u8, b: i8, c: u8}
17 struct v {a: u8, b: i8, c: v2, d: u32}
18 struct v2 {u: char, v: u8}
19 struct w {a: isize, b: ()}
20 struct x {a: isize, b: (), c: ()}
21 struct y {x: isize}
22
23 enum e1 {
24     a(u8, u32), b(u32), c
25 }
26 enum e2 {
27     a(u32), b
28 }
29
30 #[repr(C, u8)]
31 enum e3 {
32     a([u16; 0], u8), b
33 }
34
35 struct ReorderedStruct {
36     a: u8,
37     b: u16,
38     c: u8
39 }
40
41 enum ReorderedEnum {
42     A(u8, u16, u8),
43     B(u8, u16, u8),
44 }
45
46 enum NicheFilledEnumWithInhabitedVariant {
47     A(&'static ()),
48     B(&'static (), !),
49     C,
50 }
51
52 pub fn main() {
53     assert_eq!(size_of::<u8>(), 1 as usize);
54     assert_eq!(size_of::<u32>(), 4 as usize);
55     assert_eq!(size_of::<char>(), 4 as usize);
56     assert_eq!(size_of::<i8>(), 1 as usize);
57     assert_eq!(size_of::<i32>(), 4 as usize);
58     assert_eq!(size_of::<t>(), 2 as usize);
59     assert_eq!(size_of::<u>(), 3 as usize);
60     // Alignment causes padding before the char and the u32.
61
62     assert_eq!(size_of::<v>(),
63                 16 as usize);
64     assert_eq!(size_of::<isize>(), size_of::<usize>());
65     assert_eq!(size_of::<w>(), size_of::<isize>());
66     assert_eq!(size_of::<x>(), size_of::<isize>());
67     assert_eq!(size_of::<isize>(), size_of::<y>());
68
69     // Make sure enum types are the appropriate size, mostly
70     // around ensuring alignment is handled properly
71
72     assert_eq!(size_of::<e1>(), 8 as usize);
73     assert_eq!(size_of::<e2>(), 8 as usize);
74     assert_eq!(size_of::<e3>(), 4 as usize);
75     assert_eq!(size_of::<ReorderedStruct>(), 4);
76     assert_eq!(size_of::<ReorderedEnum>(), 6);
77     assert_eq!(size_of::<NicheFilledEnumWithInhabitedVariant>(), size_of::<&'static ()>());
78 }