2 // compile-flags: -Zunpretty=expanded
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
10 // [*] It excludes `Copy` in some cases, because that changes the code
11 // generated for `Clone`.
13 // [*] It excludes `RustcEncodable` and `RustDecodable`, which are obsolete and
14 // also require the `rustc_serialize` crate.
16 #![crate_type = "lib"]
21 #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
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)]
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)]
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)]
45 b1: u32, b2: u32, b3: u32, b4: u32, b5: u32, b6: u32, b7: u32, b8: u32,
48 // A struct that doesn't impl `Copy`, which means it gets the non-simple
49 // `clone` implemention that clones the fields individually.
53 // A packed struct that doesn't impl `Copy`, which means it gets the non-simple
54 // `clone` implemention that clones the fields individually.
57 struct PackedNonCopy(u32);
59 // A struct that impls `Copy` manually, which means it gets the non-simple
60 // `clone` implemention that clones the fields individually.
62 struct ManualCopy(u32);
63 impl Copy for ManualCopy {}
65 // A packed struct that impls `Copy` manually, which means it gets the
66 // non-simple `clone` implemention that clones the fields individually.
69 struct PackedManualCopy(u32);
70 impl Copy for PackedManualCopy {}
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]);
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)]
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
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> {
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)]
105 struct PackedGeneric<T: Trait, U>(T, T::A, U);
108 #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
111 // A single-variant enum.
112 #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
117 // A C-like, fieldless enum with a single variant.
118 #[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
124 // A C-like, fieldless enum.
125 #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
133 // An enum with multiple fieldless and fielded variants.
134 #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
140 S { d1: Option<u32>, d2: Option<i32> },
143 // An enum with no fieldless variants. Note that `Default` cannot be derived
145 #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
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> {
159 // A union. Most builtin traits are not derivable for unions.
160 #[derive(Clone, Copy)]