]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/type-sizes.rs
Auto merge of #54265 - arielb1:civilize-proc-macros, r=alexcrichton
[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 #![allow(non_camel_case_types)]
12 #![allow(dead_code)]
13 #![feature(never_type)]
14
15 use std::mem::size_of;
16
17 struct t {a: u8, b: i8}
18 struct u {a: u8, b: i8, c: u8}
19 struct v {a: u8, b: i8, c: v2, d: u32}
20 struct v2 {u: char, v: u8}
21 struct w {a: isize, b: ()}
22 struct x {a: isize, b: (), c: ()}
23 struct y {x: isize}
24
25 enum e1 {
26     a(u8, u32), b(u32), c
27 }
28 enum e2 {
29     a(u32), b
30 }
31
32 #[repr(C, u8)]
33 enum e3 {
34     a([u16; 0], u8), b
35 }
36
37 struct ReorderedStruct {
38     a: u8,
39     b: u16,
40     c: u8
41 }
42
43 enum ReorderedEnum {
44     A(u8, u16, u8),
45     B(u8, u16, u8),
46 }
47
48 enum EnumEmpty {}
49
50 enum EnumSingle1 {
51     A,
52 }
53
54 enum EnumSingle2 {
55     A = 42 as isize,
56 }
57
58 enum EnumSingle3 {
59     A,
60     B(!),
61 }
62
63 #[repr(u8)]
64 enum EnumSingle4 {
65     A,
66 }
67
68 #[repr(u8)]
69 enum EnumSingle5 {
70     A = 42 as u8,
71 }
72
73 enum EnumWithMaybeUninhabitedVariant<T> {
74     A(&'static ()),
75     B(&'static (), T),
76     C,
77 }
78
79 enum NicheFilledEnumWithAbsentVariant {
80     A(&'static ()),
81     B((), !),
82     C,
83 }
84
85 pub fn main() {
86     assert_eq!(size_of::<u8>(), 1 as usize);
87     assert_eq!(size_of::<u32>(), 4 as usize);
88     assert_eq!(size_of::<char>(), 4 as usize);
89     assert_eq!(size_of::<i8>(), 1 as usize);
90     assert_eq!(size_of::<i32>(), 4 as usize);
91     assert_eq!(size_of::<t>(), 2 as usize);
92     assert_eq!(size_of::<u>(), 3 as usize);
93     // Alignment causes padding before the char and the u32.
94
95     assert_eq!(size_of::<v>(),
96                 16 as usize);
97     assert_eq!(size_of::<isize>(), size_of::<usize>());
98     assert_eq!(size_of::<w>(), size_of::<isize>());
99     assert_eq!(size_of::<x>(), size_of::<isize>());
100     assert_eq!(size_of::<isize>(), size_of::<y>());
101
102     // Make sure enum types are the appropriate size, mostly
103     // around ensuring alignment is handled properly
104
105     assert_eq!(size_of::<e1>(), 8 as usize);
106     assert_eq!(size_of::<e2>(), 8 as usize);
107     assert_eq!(size_of::<e3>(), 4 as usize);
108     assert_eq!(size_of::<ReorderedStruct>(), 4);
109     assert_eq!(size_of::<ReorderedEnum>(), 6);
110
111     assert_eq!(size_of::<EnumEmpty>(), 0);
112     assert_eq!(size_of::<EnumSingle1>(), 0);
113     assert_eq!(size_of::<EnumSingle2>(), 0);
114     assert_eq!(size_of::<EnumSingle3>(), 0);
115     assert_eq!(size_of::<EnumSingle4>(), 1);
116     assert_eq!(size_of::<EnumSingle5>(), 1);
117
118     assert_eq!(size_of::<EnumWithMaybeUninhabitedVariant<!>>(),
119                size_of::<EnumWithMaybeUninhabitedVariant<()>>());
120     assert_eq!(size_of::<NicheFilledEnumWithAbsentVariant>(), size_of::<&'static ()>());
121
122     assert_eq!(size_of::<Option<Option<(bool, &())>>>(), size_of::<(bool, &())>());
123     assert_eq!(size_of::<Option<Option<(&(), bool)>>>(), size_of::<(bool, &())>());
124 }