]> git.proxmox.com Git - rustc.git/blame - src/test/ui/print_type_sizes/niche-filling.rs
New upstream version 1.50.0+dfsg1
[rustc.git] / src / test / ui / print_type_sizes / niche-filling.rs
CommitLineData
476ff2be 1// compile-flags: -Z print-type-sizes
29967ef6 2// build-pass
dc9dc135
XL
3// ignore-pass
4// ^-- needed because `--pass check` does not emit the output needed.
5// FIXME: consider using an attribute instead of side-effects.
476ff2be 6
ff7c6d11
XL
7// This file illustrates how niche-filling enums are handled,
8// modelled after cases like `Option<&u32>`, `Option<bool>` and such.
476ff2be 9//
0531ce1d 10// It uses NonZeroU32 rather than `&_` or `Unique<_>`, because
476ff2be
SL
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
ff7c6d11 17#![feature(start)]
fc512014 18#![feature(rustc_attrs)]
476ff2be
SL
19#![allow(dead_code)]
20
0531ce1d 21use std::num::NonZeroU32;
476ff2be
SL
22
23pub enum MyOption<T> { None, Some(T) }
24
fc512014
XL
25#[rustc_layout_scalar_valid_range_start(0)]
26#[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
27pub struct MyNotNegativeOne {
28 _i: i32,
29}
30
476ff2be
SL
31impl<T> Default for MyOption<T> {
32 fn default() -> Self { MyOption::None }
33}
34
35pub enum EmbeddedDiscr {
36 None,
0531ce1d 37 Record { pre: u8, val: NonZeroU32, post: u16 },
476ff2be
SL
38}
39
40impl Default for EmbeddedDiscr {
41 fn default() -> Self { EmbeddedDiscr::None }
42}
43
44#[derive(Default)]
0531ce1d 45pub struct IndirectNonZero {
476ff2be 46 pre: u8,
0531ce1d 47 nested: NestedNonZero,
476ff2be
SL
48 post: u16,
49}
50
0531ce1d 51pub struct NestedNonZero {
476ff2be 52 pre: u8,
0531ce1d 53 val: NonZeroU32,
476ff2be
SL
54 post: u16,
55}
56
0531ce1d 57impl Default for NestedNonZero {
476ff2be 58 fn default() -> Self {
0531ce1d 59 NestedNonZero { pre: 0, val: NonZeroU32::new(1).unwrap(), post: 0 }
476ff2be
SL
60 }
61}
62
ff7c6d11
XL
63pub enum Enum4<A, B, C, D> {
64 One(A),
65 Two(B),
66 Three(C),
67 Four(D)
68}
69
48663c56
XL
70pub union Union1<A: Copy> {
71 a: A,
72}
73
74pub union Union2<A: Copy, B: Copy> {
75 a: A,
76 b: B,
77}
78
ff7c6d11
XL
79#[start]
80fn start(_: isize, _: *const *const u8) -> isize {
0531ce1d 81 let _x: MyOption<NonZeroU32> = Default::default();
476ff2be 82 let _y: EmbeddedDiscr = Default::default();
0531ce1d 83 let _z: MyOption<IndirectNonZero> = Default::default();
ff7c6d11
XL
84 let _a: MyOption<bool> = Default::default();
85 let _b: MyOption<char> = Default::default();
86 let _c: MyOption<std::cmp::Ordering> = Default::default();
fc512014 87 let _d: MyOption<MyOption<u8>> = Default::default();
ff7c6d11
XL
88 let _e: Enum4<(), char, (), ()> = Enum4::One(());
89 let _f: Enum4<(), (), bool, ()> = Enum4::One(());
90 let _g: Enum4<(), (), (), MyOption<u8>> = Enum4::One(());
fc512014 91 let _h: MyOption<MyNotNegativeOne> = Default::default();
48663c56
XL
92
93 // Unions do not currently participate in niche filling.
fc512014 94 let _i: MyOption<Union2<NonZeroU32, u32>> = Default::default();
48663c56
XL
95
96 // ...even when theoretically possible.
fc512014
XL
97 let _j: MyOption<Union1<NonZeroU32>> = Default::default();
98 let _k: MyOption<Union2<NonZeroU32, NonZeroU32>> = Default::default();
48663c56 99
ff7c6d11 100 0
476ff2be 101}