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