]> git.lizzy.rs Git - rust.git/blob - tests/ui/packed/packed-struct-size.rs
Rollup merge of #107194 - xfix:remove-slice-internals-dependency-in-rustc-ast, r...
[rust.git] / tests / ui / packed / packed-struct-size.rs
1 // run-pass
2 #![allow(dead_code)]
3 #![allow(non_camel_case_types)]
4 #![allow(non_upper_case_globals)]
5
6 use std::mem;
7
8 #[repr(packed)]
9 struct P1S4 {
10     a: u8,
11     b: [u8;  3],
12 }
13
14 #[repr(packed(2))]
15 struct P2S4 {
16     a: u8,
17     b: [u8;  3],
18 }
19
20 #[repr(packed)]
21 struct P1S5 {
22     a: u8,
23     b: u32
24 }
25
26 #[repr(packed(2))]
27 struct P2S2 {
28     a: u8,
29     b: u8
30 }
31
32 #[repr(packed(2))]
33 struct P2S6 {
34     a: u8,
35     b: u32
36 }
37
38 #[repr(packed(2))]
39 struct P2S12 {
40     a: u32,
41     b: u64
42 }
43
44 #[repr(packed)]
45 struct P1S13 {
46     a: i64,
47     b: f32,
48     c: u8,
49 }
50
51 #[repr(packed(2))]
52 struct P2S14 {
53     a: i64,
54     b: f32,
55     c: u8,
56 }
57
58 #[repr(packed(4))]
59 struct P4S16 {
60     a: u8,
61     b: f32,
62     c: i64,
63     d: u16,
64 }
65
66 #[repr(C, packed(4))]
67 struct P4CS20 {
68     a: u8,
69     b: f32,
70     c: i64,
71     d: u16,
72 }
73
74 enum Foo {
75     Bar = 1,
76     Baz = 2
77 }
78
79 #[repr(packed)]
80 struct P1S3_Foo {
81     a: u8,
82     b: u16,
83     c: Foo
84 }
85
86 #[repr(packed(2))]
87 struct P2_Foo {
88     a: Foo,
89 }
90
91 #[repr(packed(2))]
92 struct P2S3_Foo {
93     a: u8,
94     b: u16,
95     c: Foo
96 }
97
98 #[repr(packed)]
99 struct P1S7_Option {
100     a: f32,
101     b: u8,
102     c: u16,
103     d: Option<Box<f64>>
104 }
105
106 #[repr(packed(2))]
107 struct P2_Option {
108     a: Option<Box<f64>>
109 }
110
111 #[repr(packed(2))]
112 struct P2S7_Option {
113     a: f32,
114     b: u8,
115     c: u16,
116     d: Option<Box<f64>>
117 }
118
119 // Placing packed structs in statics should work
120 static TEST_P1S4: P1S4 = P1S4 { a: 1, b: [2, 3, 4] };
121 static TEST_P1S5: P1S5 = P1S5 { a: 3, b: 67 };
122 static TEST_P1S3_Foo: P1S3_Foo = P1S3_Foo { a: 1, b: 2, c: Foo::Baz };
123 static TEST_P2S2: P2S2 = P2S2 { a: 1, b: 2 };
124 static TEST_P2S4: P2S4 = P2S4 { a: 1, b: [2, 3, 4] };
125 static TEST_P2S6: P2S6 = P2S6 { a: 1, b: 2 };
126 static TEST_P2S12: P2S12 = P2S12 { a: 1, b: 2 };
127 static TEST_P4S16: P4S16 = P4S16 { a: 1, b: 2.0, c: 3, d: 4 };
128 static TEST_P4CS20: P4CS20 = P4CS20 { a: 1, b: 2.0, c: 3, d: 4 };
129
130 fn align_to(value: usize, align: usize) -> usize {
131     (value + (align - 1)) & !(align - 1)
132 }
133
134 macro_rules! check {
135     ($t:ty, $align:expr, $size:expr) => ({
136         assert_eq!(mem::align_of::<$t>(), $align);
137         assert_eq!(mem::size_of::<$t>(), $size);
138     });
139 }
140
141 pub fn main() {
142     check!(P1S4, 1, 4);
143     check!(P1S5, 1, 5);
144     check!(P1S13, 1, 13);
145     check!(P1S3_Foo, 1, 3 + mem::size_of::<Foo>());
146     check!(P1S7_Option, 1, 7 + mem::size_of::<Option<Box<f64>>>());
147
148     check!(P2S2, 1, 2);
149     check!(P2S4, 1, 4);
150     check!(P2S6, 2, 6);
151     check!(P2S12, 2, 12);
152     check!(P2S14, 2, 14);
153     check!(P4S16, 4, 16);
154     check!(P4CS20, 4, 20);
155     check!(P2S3_Foo, 2, align_to(3 + mem::size_of::<P2_Foo>(), 2));
156     check!(P2S7_Option, 2, align_to(7 + mem::size_of::<P2_Option>(), 2));
157 }