]> git.lizzy.rs Git - rust.git/blob - src/test/ui/print_type_sizes/niche-filling.rs
add '// ignore-pass' where applicable.
[rust.git] / src / test / ui / print_type_sizes / niche-filling.rs
1 // compile-flags: -Z print-type-sizes
2 // compile-pass
3 // ignore-pass
4 // ^-- needed because `--pass check` does not emit the output needed.
5 //     FIXME: consider using an attribute instead of side-effects.
6
7 // This file illustrates how niche-filling enums are handled,
8 // modelled after cases like `Option<&u32>`, `Option<bool>` and such.
9 //
10 // It uses NonZeroU32 rather than `&_` or `Unique<_>`, because
11 // the test is not set up to deal with target-dependent pointer width.
12 //
13 // It avoids using u64/i64 because on some targets that is only 4-byte
14 // aligned (while on most it is 8-byte aligned) and so the resulting
15 // padding and overall computed sizes can be quite different.
16
17 #![feature(start)]
18 #![allow(dead_code)]
19
20 use std::num::NonZeroU32;
21
22 pub enum MyOption<T> { None, Some(T) }
23
24 impl<T> Default for MyOption<T> {
25     fn default() -> Self { MyOption::None }
26 }
27
28 pub enum EmbeddedDiscr {
29     None,
30     Record { pre: u8, val: NonZeroU32, post: u16 },
31 }
32
33 impl Default for EmbeddedDiscr {
34     fn default() -> Self { EmbeddedDiscr::None }
35 }
36
37 #[derive(Default)]
38 pub struct IndirectNonZero {
39     pre: u8,
40     nested: NestedNonZero,
41     post: u16,
42 }
43
44 pub struct NestedNonZero {
45     pre: u8,
46     val: NonZeroU32,
47     post: u16,
48 }
49
50 impl Default for NestedNonZero {
51     fn default() -> Self {
52         NestedNonZero { pre: 0, val: NonZeroU32::new(1).unwrap(), post: 0 }
53     }
54 }
55
56 pub enum Enum4<A, B, C, D> {
57     One(A),
58     Two(B),
59     Three(C),
60     Four(D)
61 }
62
63 pub union Union1<A: Copy> {
64     a: A,
65 }
66
67 pub union Union2<A: Copy, B: Copy> {
68     a: A,
69     b: B,
70 }
71
72 #[start]
73 fn start(_: isize, _: *const *const u8) -> isize {
74     let _x: MyOption<NonZeroU32> = Default::default();
75     let _y: EmbeddedDiscr = Default::default();
76     let _z: MyOption<IndirectNonZero> = Default::default();
77     let _a: MyOption<bool> = Default::default();
78     let _b: MyOption<char> = Default::default();
79     let _c: MyOption<std::cmp::Ordering> = Default::default();
80     let _b: MyOption<MyOption<u8>> = Default::default();
81     let _e: Enum4<(), char, (), ()> = Enum4::One(());
82     let _f: Enum4<(), (), bool, ()> = Enum4::One(());
83     let _g: Enum4<(), (), (), MyOption<u8>> = Enum4::One(());
84
85     // Unions do not currently participate in niche filling.
86     let _h: MyOption<Union2<NonZeroU32, u32>> = Default::default();
87
88     // ...even when theoretically possible.
89     let _i: MyOption<Union1<NonZeroU32>> = Default::default();
90     let _j: MyOption<Union2<NonZeroU32, NonZeroU32>> = Default::default();
91
92     0
93 }