rust/tests/ui/deriving
Nicholas Nethercote 2e93f2c92f Allow more deriving on packed structs.
Currently, deriving on packed structs has some non-trivial limitations,
related to the fact that taking references on unaligned fields is UB.

The current approach to field accesses in derived code:
- Normal case: `&self.0`
- In a packed struct that derives `Copy`: `&{self.0}`
- In a packed struct that doesn't derive `Copy`: `&self.0`

Plus, we disallow deriving any builtin traits other than `Default` for any
packed generic type, because it's possible that there might be
misaligned fields. This is a fairly broad restriction.

Plus, we disallow deriving any builtin traits other than `Default` for most
packed types that don't derive `Copy`. (The exceptions are those where the
alignments inherently satisfy the packing, e.g. in a type with
`repr(packed(N))` where all the fields have alignments of `N` or less
anyway. Such types are pretty strange, because the `packed` attribute is
not having any effect.)

This commit introduces a new, simpler approach to field accesses:
- Normal case: `&self.0`
- In a packed struct: `&{self.0}`

In the latter case, this requires that all fields impl `Copy`, which is
a new restriction. This means that the following example compiles under
the old approach and doesn't compile under the new approach.
```
 #[derive(Debug)]
 struct NonCopy(u8);

 #[derive(Debug)
 #[repr(packed)]
 struct MyType(NonCopy);
```
(Note that the old approach's support for cases like this was brittle.
Changing the `u8` to a `u16` would be enough to stop it working. So not
much capability is lost here.)

However, the other constraints from the old rules are removed. We can now
derive builtin traits for packed generic structs like this:
```
 trait Trait { type A; }

 #[derive(Hash)]
 #[repr(packed)]
 pub struct Foo<T: Trait>(T, T::A);
```
To allow this, we add a `T: Copy` bound in the derived impl and a `T::A:
Copy` bound in where clauses. So `T` and `T::A` must impl `Copy`.

We can now also derive builtin traits for packed structs that don't derive
`Copy`, so long as the fields impl `Copy`:
```
 #[derive(Hash)]
 #[repr(packed)]
 pub struct Foo(u32);
```
This includes types that hand-impl `Copy` rather than deriving it, such as the
following, that show up in winapi-0.2:
```
 #[derive(Clone)]
 #[repr(packed)]
 struct MyType(i32);

 impl Copy for MyType {}
```
The new approach is simpler to understand and implement, and it avoids
the need for the `unsafe_derive_on_repr_packed` check.

One exception is required for backwards-compatibility: we allow `[u8]`
fields for now. There is a new lint for this,
`byte_slice_in_packed_struct_with_derive`.
2023-01-30 12:00:42 +11:00
..
auxiliary Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-no-std.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-partialord-correctness.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-all-codegen.rs Allow more deriving on packed structs. 2023-01-30 12:00:42 +11:00
deriving-all-codegen.stderr Allow more deriving on packed structs. 2023-01-30 12:00:42 +11:00
deriving-all-codegen.stdout Allow more deriving on packed structs. 2023-01-30 12:00:42 +11:00
deriving-associated-types.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-bounds.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-clone-array.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-clone-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-clone-generic-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-clone-generic-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-clone-generic-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-clone-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-clone-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-cmp-generic-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-cmp-generic-struct-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-cmp-generic-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-cmp-generic-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-cmp-shortcircuit.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-copyclone.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-default-box.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-default-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-enum-single-variant.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-eq-ord-boxed-slice.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-hash.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-in-fn.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-in-macro.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-meta-multiple.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-meta.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-self-lifetime-totalord-totaleq.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-show-2.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-show.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-via-extension-c-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-via-extension-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-via-extension-hash-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-via-extension-hash-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-via-extension-struct-empty.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-via-extension-struct-like-enum-variant.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-via-extension-struct-tuple.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-via-extension-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-via-extension-type-params.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-with-helper.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-with-repr-packed.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-3935.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-6341.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-19358.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-58319.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-89188-gat-hrtb.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-103157.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-103157.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-105101.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-105101.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00