]> git.lizzy.rs Git - rust.git/blob - src/test/ui/structs-enums/codegen-tag-static-padding.rs
Auto merge of #106025 - matthiaskrgr:rollup-vz5rqah, r=matthiaskrgr
[rust.git] / src / test / ui / structs-enums / codegen-tag-static-padding.rs
1 // run-pass
2 #![allow(non_upper_case_globals)]
3
4 // Issue #13186
5
6 // For simplicity of explanations assuming code is compiled for x86_64
7 // Linux ABI.
8
9 // Size of TestOption<u64> is 16, and alignment of TestOption<u64> is 8.
10 // Size of u8 is 1, and alignment of u8 is 1.
11 // So size of Request is 24, and alignment of Request must be 8:
12 // the maximum alignment of its fields.
13 // Last 7 bytes of Request struct are not occupied by any fields.
14
15
16
17 enum TestOption<T> {
18     TestNone,
19     TestSome(T),
20 }
21
22 pub struct Request {
23     foo: TestOption<u64>,
24     bar: u8,
25 }
26
27 fn default_instance() -> &'static Request {
28     static instance: Request = Request {
29         // LLVM does not allow to specify alignment of expressions, thus
30         // alignment of `foo` in constant is 1, not 8.
31         foo: TestOption::TestNone,
32         bar: 17,
33         // Space after last field is not occupied by any data, but it is
34         // reserved to make struct aligned properly. If compiler does
35         // not insert padding after last field when emitting constant,
36         // size of struct may be not equal to size of struct, and
37         // compiler crashes in internal assertion check.
38     };
39     &instance
40 }
41
42 fn non_default_instance() -> &'static Request {
43     static instance: Request = Request {
44         foo: TestOption::TestSome(0x1020304050607080),
45         bar: 19,
46     };
47     &instance
48 }
49
50 pub fn main() {
51     match default_instance() {
52         &Request { foo: TestOption::TestNone, bar: 17 } => {},
53         _ => panic!(),
54     };
55     match non_default_instance() {
56         &Request { foo: TestOption::TestSome(0x1020304050607080), bar: 19 } => {},
57         _ => panic!(),
58     };
59 }