]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/union/union-packed.rs
New upstream version 1.31.0~beta.4+dfsg1
[rustc.git] / src / test / run-pass / union / union-packed.rs
1 // Copyright 2016 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 // run-pass
12 #![allow(dead_code)]
13 #![allow(non_snake_case)]
14
15 #![feature(untagged_unions)]
16 #![feature(repr_packed)]
17
18 use std::mem::{size_of, size_of_val, align_of, align_of_val};
19
20 struct S {
21 a: u16,
22 b: [u8; 3],
23 }
24
25 #[repr(packed)]
26 struct Sp1 {
27 a: u16,
28 b: [u8; 3],
29 }
30
31 #[repr(packed(2))]
32 struct Sp2 {
33 a: u16,
34 b: [u8; 3],
35 }
36
37 union U {
38 a: u16,
39 b: [u8; 3],
40 }
41
42 #[repr(packed)]
43 union Up1 {
44 a: u16,
45 b: [u8; 3],
46 }
47
48 #[repr(packed(2))]
49 union Up2 {
50 a: u16,
51 b: [u8; 3],
52 }
53
54 #[repr(C, packed(4))]
55 union Up4c {
56 a: u16,
57 b: [u8; 3],
58 }
59
60 const CS: S = S { a: 0, b: [0, 0, 0] };
61 const CSP1: Sp1 = Sp1 { a: 0, b: [0, 0, 0] };
62 const CSP2: Sp2 = Sp2 { a: 0, b: [0, 0, 0] };
63 const CU: U = U { b: [0, 0, 0] };
64 const CUP1: Up1 = Up1 { b: [0, 0, 0] };
65 const CUP2: Up2 = Up2 { b: [0, 0, 0] };
66 const CUP4C: Up4c = Up4c { b: [0, 0, 0] };
67
68 fn main() {
69 let s = S { a: 0, b: [0, 0, 0] };
70 assert_eq!(size_of::<S>(), 6);
71 assert_eq!(size_of_val(&s), 6);
72 assert_eq!(size_of_val(&CS), 6);
73 assert_eq!(align_of::<S>(), 2);
74 assert_eq!(align_of_val(&s), 2);
75 assert_eq!(align_of_val(&CS), 2);
76
77 let sp1 = Sp1 { a: 0, b: [0, 0, 0] };
78 assert_eq!(size_of::<Sp1>(), 5);
79 assert_eq!(size_of_val(&sp1), 5);
80 assert_eq!(size_of_val(&CSP1), 5);
81 assert_eq!(align_of::<Sp1>(), 1);
82 assert_eq!(align_of_val(&sp1), 1);
83 assert_eq!(align_of_val(&CSP1), 1);
84
85 let sp2 = Sp2 { a: 0, b: [0, 0, 0] };
86 assert_eq!(size_of::<Sp2>(), 6);
87 assert_eq!(size_of_val(&sp2), 6);
88 assert_eq!(size_of_val(&CSP2), 6);
89 assert_eq!(align_of::<Sp2>(), 2);
90 assert_eq!(align_of_val(&sp2), 2);
91 assert_eq!(align_of_val(&CSP2), 2);
92
93 let u = U { b: [0, 0, 0] };
94 assert_eq!(size_of::<U>(), 4);
95 assert_eq!(size_of_val(&u), 4);
96 assert_eq!(size_of_val(&CU), 4);
97 assert_eq!(align_of::<U>(), 2);
98 assert_eq!(align_of_val(&u), 2);
99 assert_eq!(align_of_val(&CU), 2);
100
101 let Up1 = Up1 { b: [0, 0, 0] };
102 assert_eq!(size_of::<Up1>(), 3);
103 assert_eq!(size_of_val(&Up1), 3);
104 assert_eq!(size_of_val(&CUP1), 3);
105 assert_eq!(align_of::<Up1>(), 1);
106 assert_eq!(align_of_val(&Up1), 1);
107 assert_eq!(align_of_val(&CUP1), 1);
108
109 let up2 = Up2 { b: [0, 0, 0] };
110 assert_eq!(size_of::<Up2>(), 4);
111 assert_eq!(size_of_val(&up2), 4);
112 assert_eq!(size_of_val(&CUP2), 4);
113 assert_eq!(align_of::<Up2>(), 2);
114 assert_eq!(align_of_val(&up2), 2);
115 assert_eq!(align_of_val(&CUP2), 2);
116
117 let up4c = Up4c { b: [0, 0, 0] };
118 assert_eq!(size_of::<Up4c>(), 4);
119 assert_eq!(size_of_val(&up4c), 4);
120 assert_eq!(size_of_val(&CUP4C), 4);
121 assert_eq!(align_of::<Up4c>(), 2);
122 assert_eq!(align_of_val(&up4c), 2);
123 assert_eq!(align_of_val(&CUP4C), 2);
124
125 hybrid::check_hybrid();
126 }
127
128 mod hybrid {
129 use std::mem::{size_of, align_of};
130
131 #[repr(packed)]
132 struct S1 {
133 a: u16,
134 b: u8,
135 }
136
137 #[repr(packed)]
138 union U {
139 s: S1,
140 c: u16,
141 }
142
143 #[repr(packed)]
144 struct S2 {
145 d: u8,
146 u: U,
147 }
148
149 #[repr(C, packed(2))]
150 struct S1C {
151 a: u16,
152 b: u8,
153 }
154
155 #[repr(C, packed(2))]
156 union UC {
157 s: S1,
158 c: u16,
159 }
160
161 #[repr(C, packed(2))]
162 struct S2C {
163 d: u8,
164 u: UC,
165 }
166
167 pub fn check_hybrid() {
168 assert_eq!(align_of::<S1>(), 1);
169 assert_eq!(size_of::<S1>(), 3);
170 assert_eq!(align_of::<U>(), 1);
171 assert_eq!(size_of::<U>(), 3);
172 assert_eq!(align_of::<S2>(), 1);
173 assert_eq!(size_of::<S2>(), 4);
174
175 assert_eq!(align_of::<S1C>(), 2);
176 assert_eq!(size_of::<S1C>(), 4);
177 assert_eq!(align_of::<UC>(), 2);
178 assert_eq!(size_of::<UC>(), 4);
179 assert_eq!(align_of::<S2C>(), 2);
180 assert_eq!(size_of::<S2C>(), 6);
181 }
182 }