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