rust/tests/ui/derives
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
clone-debug-dead-code-in-the-same-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
clone-debug-dead-code-in-the-same-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
clone-debug-dead-code.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
clone-debug-dead-code.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-assoc-type-not-impl.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-assoc-type-not-impl.stderr Tweak output 2023-01-11 19:31:34 +00:00
derive-deadlock.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-deadlock.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-Debug-use-ufcs-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-Debug-use-ufcs-tuple.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-hygiene.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-macro-const-default.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-marker-tricky.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-multiple-with-packed.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-on-trait-item-or-impl-item.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-on-trait-item-or-impl-item.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-partial-ord.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derive-renamed.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Clone-enum-struct-variant.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Clone-enum-struct-variant.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Clone-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Clone-enum.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Clone-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Clone-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Clone-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Clone-tuple-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Debug-enum-struct-variant.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Debug-enum-struct-variant.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Debug-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Debug-enum.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Debug-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Debug-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Debug-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Debug-tuple-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Default-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Default-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Default-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Default-tuple-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Eq-enum-struct-variant.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Eq-enum-struct-variant.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Eq-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Eq-enum.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Eq-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Eq-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Eq-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Eq-tuple-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Hash-enum-struct-variant.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Hash-enum-struct-variant.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Hash-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Hash-enum.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Hash-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Hash-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Hash-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Hash-tuple-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Ord-enum-struct-variant.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Ord-enum-struct-variant.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Ord-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Ord-enum.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Ord-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Ord-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Ord-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-Ord-tuple-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialEq-enum-struct-variant.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialEq-enum-struct-variant.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialEq-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialEq-enum.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialEq-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialEq-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialEq-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialEq-tuple-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialOrd-enum-struct-variant.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialOrd-enum-struct-variant.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialOrd-enum.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialOrd-enum.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialOrd-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialOrd-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialOrd-tuple-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
derives-span-PartialOrd-tuple-struct.stderr 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-bounds.stderr 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-copyclone.stderr Note predicate span on ImplDerivedObligation 2023-01-11 19:46:45 +00:00
deriving-meta-empty-trait-list.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-meta-unknown-trait.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-meta-unknown-trait.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-no-inner-impl-error-message.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-no-inner-impl-error-message.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-non-type.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-non-type.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-primitive.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-primitive.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deriving-with-repr-packed-2.rs Allow more deriving on packed structs. 2023-01-30 12:00:42 +11:00
deriving-with-repr-packed-2.stderr Allow more deriving on packed structs. 2023-01-30 12:00:42 +11:00
deriving-with-repr-packed.rs Allow more deriving on packed structs. 2023-01-30 12:00:42 +11:00
deriving-with-repr-packed.stderr Allow more deriving on packed structs. 2023-01-30 12:00:42 +11:00
issue-36617.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-36617.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-43023.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-43023.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-91492.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-91492.stderr Conserve cause of ImplDerivedObligation in E0599 2023-01-11 19:31:33 +00:00
issue-91550.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-91550.stderr Keep obligation chain when elaborating obligations 2023-01-13 18:20:23 +00:00
issue-97343.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-97343.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00