]> git.lizzy.rs Git - rust.git/blob - tests/ui/deriving/deriving-all-codegen.rs
Test drop_tracking_mir before querying generator.
[rust.git] / tests / ui / deriving / deriving-all-codegen.rs
1 // check-pass
2 // compile-flags: -Zunpretty=expanded
3 // edition:2021
4 //
5 // This test checks the code generated for all[*] the builtin derivable traits
6 // on a variety of structs and enums. It protects against accidental changes to
7 // the generated code, and makes deliberate changes to the generated code
8 // easier to review.
9 //
10 // [*] It excludes `Copy` in some cases, because that changes the code
11 // generated for `Clone`.
12 //
13 // [*] It excludes `RustcEncodable` and `RustDecodable`, which are obsolete and
14 // also require the `rustc_serialize` crate.
15
16 #![crate_type = "lib"]
17 #![allow(dead_code)]
18 #![allow(deprecated)]
19
20 // Empty struct.
21 #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
22 struct Empty;
23
24 // A basic struct.
25 #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
26 struct Point {
27     x: u32,
28     y: u32,
29 }
30
31 // A large struct.
32 #[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
33 struct Big {
34     b1: u32, b2: u32, b3: u32, b4: u32, b5: u32, b6: u32, b7: u32, b8: u32,
35 }
36
37 // A struct with an unsized field. Some derives are not usable in this case.
38 #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
39 struct Unsized([u32]);
40
41 // A packed tuple struct that impls `Copy`.
42 #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
43 #[repr(packed)]
44 struct PackedCopy(u32);
45
46 // A packed tuple struct that does not impl `Copy`. Note that the alignment of
47 // the field must be 1 for this code to be valid. Otherwise it triggers an
48 // error "`#[derive]` can't be used on a `#[repr(packed)]` struct that does not
49 // derive Copy (error E0133)" at MIR building time. This is a weird case and
50 // it's possible that this struct is not supposed to work, but for now it does.
51 #[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
52 #[repr(packed)]
53 struct PackedNonCopy(u8);
54
55 // An empty enum.
56 #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
57 enum Enum0 {}
58
59 // A single-variant enum.
60 #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
61 enum Enum1 {
62     Single { x: u32 }
63 }
64
65 // A C-like, fieldless enum with a single variant.
66 #[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
67 enum Fieldless1 {
68     #[default]
69     A,
70 }
71
72 // A C-like, fieldless enum.
73 #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
74 enum Fieldless {
75     #[default]
76     A,
77     B,
78     C,
79 }
80
81 // An enum with multiple fieldless and fielded variants.
82 #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
83 enum Mixed {
84     #[default]
85     P,
86     Q,
87     R(u32),
88     S { d1: Option<u32>, d2: Option<i32> },
89 }
90
91 // An enum with no fieldless variants. Note that `Default` cannot be derived
92 // for this enum.
93 #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
94 enum Fielded {
95     X(u32),
96     Y(bool),
97     Z(Option<i32>),
98 }
99
100 // A union. Most builtin traits are not derivable for unions.
101 #[derive(Clone, Copy)]
102 pub union Union {
103     pub b: bool,
104     pub u: u32,
105     pub i: i32,
106 }