]> git.lizzy.rs Git - rust.git/blob - tests/ui/deriving/deriving-all-codegen.rs
Auto merge of #107443 - cjgillot:generator-less-query, r=compiler-errors
[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. Note: because this derives `Copy`, it gets the simple
25 // `clone` implemention that just does `*self`.
26 #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
27 struct Point {
28     x: u32,
29     y: u32,
30 }
31
32 // A basic packed struct. Note: because this derives `Copy`, it gets the simple
33 // `clone` implemention that just does `*self`.
34 #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
35 #[repr(packed)]
36 struct PackedPoint {
37     x: u32,
38     y: u32,
39 }
40
41 // A large struct. Note: because this derives `Copy`, it gets the simple
42 // `clone` implemention that just does `*self`.
43 #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
44 struct Big {
45     b1: u32, b2: u32, b3: u32, b4: u32, b5: u32, b6: u32, b7: u32, b8: u32,
46 }
47
48 // A struct that doesn't impl `Copy`, which means it gets the non-simple
49 // `clone` implemention that clones the fields individually.
50 #[derive(Clone)]
51 struct NonCopy(u32);
52
53 // A packed struct that doesn't impl `Copy`, which means it gets the non-simple
54 // `clone` implemention that clones the fields individually.
55 #[derive(Clone)]
56 #[repr(packed)]
57 struct PackedNonCopy(u32);
58
59 // A struct that impls `Copy` manually, which means it gets the non-simple
60 // `clone` implemention that clones the fields individually.
61 #[derive(Clone)]
62 struct ManualCopy(u32);
63 impl Copy for ManualCopy {}
64
65 // A packed struct that impls `Copy` manually, which means it gets the
66 // non-simple `clone` implemention that clones the fields individually.
67 #[derive(Clone)]
68 #[repr(packed)]
69 struct PackedManualCopy(u32);
70 impl Copy for PackedManualCopy {}
71
72 // A struct with an unsized field. Some derives are not usable in this case.
73 #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
74 struct Unsized([u32]);
75
76 // A packed struct with an unsized `[u8]` field. This is currently allowed, but
77 // causes a warning and will be phased out at some point.
78 #[derive(Debug, Hash)]
79 #[repr(packed)]
80 struct PackedUnsizedU8([u8]);
81 //~^ WARNING byte slice in a packed struct that derives a built-in trait
82 //~^^ WARNING byte slice in a packed struct that derives a built-in trait
83 //~^^^ this was previously accepted
84 //~^^^^ this was previously accepted
85
86 trait Trait {
87     type A;
88 }
89
90 // A generic struct involving an associated type.
91 #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
92 struct Generic<T: Trait, U> {
93     t: T,
94     ta: T::A,
95     u: U,
96 }
97
98 // A packed, generic tuple struct involving an associated type. Because it is
99 // packed, a `T: Copy` bound is added to all impls (and where clauses within
100 // them) except for `Default`. This is because we must access fields using
101 // copies (e.g. `&{self.0}`), instead of using direct references (e.g.
102 // `&self.0`) which may be misaligned in a packed struct.
103 #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
104 #[repr(packed)]
105 struct PackedGeneric<T: Trait, U>(T, T::A, U);
106
107 // An empty enum.
108 #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
109 enum Enum0 {}
110
111 // A single-variant enum.
112 #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
113 enum Enum1 {
114     Single { x: u32 }
115 }
116
117 // A C-like, fieldless enum with a single variant.
118 #[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
119 enum Fieldless1 {
120     #[default]
121     A,
122 }
123
124 // A C-like, fieldless enum.
125 #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
126 enum Fieldless {
127     #[default]
128     A,
129     B,
130     C,
131 }
132
133 // An enum with multiple fieldless and fielded variants.
134 #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
135 enum Mixed {
136     #[default]
137     P,
138     Q,
139     R(u32),
140     S { d1: Option<u32>, d2: Option<i32> },
141 }
142
143 // An enum with no fieldless variants. Note that `Default` cannot be derived
144 // for this enum.
145 #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
146 enum Fielded {
147     X(u32),
148     Y(bool),
149     Z(Option<i32>),
150 }
151
152 // A generic enum. Note that `Default` cannot be derived for this enum.
153 #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
154 enum EnumGeneric<T, U> {
155     One(T),
156     Two(U),
157 }
158
159 // A union. Most builtin traits are not derivable for unions.
160 #[derive(Clone, Copy)]
161 pub union Union {
162     pub b: bool,
163     pub u: u32,
164     pub i: i32,
165 }