]> git.lizzy.rs Git - rust.git/blob - src/test/ui/print_type_sizes/niche-filling.rs
Rollup merge of #106043 - c410-f3r:moar-errors, r=petrochenkov
[rust.git] / src / test / ui / print_type_sizes / niche-filling.rs
1 // compile-flags: -Z print-type-sizes --crate-type=lib
2 // build-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(rustc_attrs)]
18 #![allow(dead_code)]
19
20 use std::num::NonZeroU32;
21
22 pub enum MyOption<T> { None, Some(T) }
23
24 #[rustc_layout_scalar_valid_range_start(0)]
25 #[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
26 pub struct MyNotNegativeOne {
27   _i: i32,
28 }
29
30 impl<T> Default for MyOption<T> {
31     fn default() -> Self { MyOption::None }
32 }
33
34 pub enum EmbeddedDiscr {
35     None,
36     Record { pre: u8, val: NonZeroU32, post: u16 },
37 }
38
39 impl Default for EmbeddedDiscr {
40     fn default() -> Self { EmbeddedDiscr::None }
41 }
42
43 #[derive(Default)]
44 pub struct IndirectNonZero {
45     pre: u8,
46     nested: NestedNonZero,
47     post: u16,
48 }
49
50 pub struct NestedNonZero {
51     pre: u8,
52     val: NonZeroU32,
53     post: u16,
54 }
55
56 impl Default for NestedNonZero {
57     fn default() -> Self {
58         NestedNonZero { pre: 0, val: unsafe { NonZeroU32::new_unchecked(1) }, post: 0 }
59     }
60 }
61
62 pub enum Enum4<A, B, C, D> {
63     One(A),
64     Two(B),
65     Three(C),
66     Four(D)
67 }
68
69 pub union Union1<A: Copy> {
70     a: A,
71 }
72
73 pub union Union2<A: Copy, B: Copy> {
74     a: A,
75     b: B,
76 }
77
78 pub fn test() {
79     let _x: MyOption<NonZeroU32> = Default::default();
80     let _y: EmbeddedDiscr = Default::default();
81     let _z: MyOption<IndirectNonZero> = Default::default();
82     let _a: MyOption<bool> = Default::default();
83     let _b: MyOption<char> = Default::default();
84     let _c: MyOption<std::cmp::Ordering> = Default::default();
85     let _d: MyOption<MyOption<u8>> = Default::default();
86     let _e: Enum4<(), char, (), ()> = Enum4::One(());
87     let _f: Enum4<(), (), bool, ()> = Enum4::One(());
88     let _g: Enum4<(), (), (), MyOption<u8>> = Enum4::One(());
89     let _h: MyOption<MyNotNegativeOne> = Default::default();
90
91     // Unions do not currently participate in niche filling.
92     let _i: MyOption<Union2<NonZeroU32, u32>> = Default::default();
93
94     // ...even when theoretically possible.
95     let _j: MyOption<Union1<NonZeroU32>> = Default::default();
96     let _k: MyOption<Union2<NonZeroU32, NonZeroU32>> = Default::default();
97 }