]> git.lizzy.rs Git - rust.git/blobdiff - 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
index ba7809413bd804bc2b7ffa828c12b733abed418b..51f9708d3cd629518734df9d0dc9d80473430de7 100644 (file)
 #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
 struct Empty;
 
-// A basic struct.
+// A basic struct. Note: because this derives `Copy`, it gets the simple
+// `clone` implemention that just does `*self`.
 #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
 struct Point {
     x: u32,
     y: u32,
 }
 
-// A large struct.
-#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
+// A basic packed struct. Note: because this derives `Copy`, it gets the simple
+// `clone` implemention that just does `*self`.
+#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
+#[repr(packed)]
+struct PackedPoint {
+    x: u32,
+    y: u32,
+}
+
+// A large struct. Note: because this derives `Copy`, it gets the simple
+// `clone` implemention that just does `*self`.
+#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
 struct Big {
     b1: u32, b2: u32, b3: u32, b4: u32, b5: u32, b6: u32, b7: u32, b8: u32,
 }
 
+// A struct that doesn't impl `Copy`, which means it gets the non-simple
+// `clone` implemention that clones the fields individually.
+#[derive(Clone)]
+struct NonCopy(u32);
+
+// A packed struct that doesn't impl `Copy`, which means it gets the non-simple
+// `clone` implemention that clones the fields individually.
+#[derive(Clone)]
+#[repr(packed)]
+struct PackedNonCopy(u32);
+
+// A struct that impls `Copy` manually, which means it gets the non-simple
+// `clone` implemention that clones the fields individually.
+#[derive(Clone)]
+struct ManualCopy(u32);
+impl Copy for ManualCopy {}
+
+// A packed struct that impls `Copy` manually, which means it gets the
+// non-simple `clone` implemention that clones the fields individually.
+#[derive(Clone)]
+#[repr(packed)]
+struct PackedManualCopy(u32);
+impl Copy for PackedManualCopy {}
+
 // A struct with an unsized field. Some derives are not usable in this case.
 #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
 struct Unsized([u32]);
 
-// A packed tuple struct that impls `Copy`.
-#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
+// A packed struct with an unsized `[u8]` field. This is currently allowed, but
+// causes a warning and will be phased out at some point.
+#[derive(Debug, Hash)]
 #[repr(packed)]
-struct PackedCopy(u32);
+struct PackedUnsizedU8([u8]);
+//~^ WARNING byte slice in a packed struct that derives a built-in trait
+//~^^ WARNING byte slice in a packed struct that derives a built-in trait
+//~^^^ this was previously accepted
+//~^^^^ this was previously accepted
 
-// A packed tuple struct that does not impl `Copy`. Note that the alignment of
-// the field must be 1 for this code to be valid. Otherwise it triggers an
-// error "`#[derive]` can't be used on a `#[repr(packed)]` struct that does not
-// derive Copy (error E0133)" at MIR building time. This is a weird case and
-// it's possible that this struct is not supposed to work, but for now it does.
-#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
+trait Trait {
+    type A;
+}
+
+// A generic struct involving an associated type.
+#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
+struct Generic<T: Trait, U> {
+    t: T,
+    ta: T::A,
+    u: U,
+}
+
+// A packed, generic tuple struct involving an associated type. Because it is
+// packed, a `T: Copy` bound is added to all impls (and where clauses within
+// them) except for `Default`. This is because we must access fields using
+// copies (e.g. `&{self.0}`), instead of using direct references (e.g.
+// `&self.0`) which may be misaligned in a packed struct.
+#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
 #[repr(packed)]
-struct PackedNonCopy(u8);
+struct PackedGeneric<T: Trait, U>(T, T::A, U);
 
 // An empty enum.
 #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
@@ -97,6 +149,13 @@ enum Fielded {
     Z(Option<i32>),
 }
 
+// A generic enum. Note that `Default` cannot be derived for this enum.
+#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
+enum EnumGeneric<T, U> {
+    One(T),
+    Two(U),
+}
+
 // A union. Most builtin traits are not derivable for unions.
 #[derive(Clone, Copy)]
 pub union Union {