rust/compiler
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
..
rustc rustc: Use unix_sigpipe instead of rustc_driver::set_sigpipe_handler 2022-10-02 17:49:36 +02:00
rustc_abi abi: add AddressSpace field to Primitive::Pointer 2023-01-22 23:41:39 -05:00
rustc_apfloat compiler: remove unnecessary imports and qualified paths 2022-12-10 18:45:34 +01:00
rustc_arena compiler: remove unnecessary imports and qualified paths 2022-12-10 18:45:34 +01:00
rustc_ast Rollup merge of #107194 - xfix:remove-slice-internals-dependency-in-rustc-ast, r=Nilstrieb 2023-01-28 00:23:12 +09:00
rustc_ast_lowering Remove HirId -> LocalDefId map from HIR. 2023-01-28 09:55:26 +00:00
rustc_ast_passes Autoderive ExternBlockSuggestion 2023-01-19 13:52:15 +01:00
rustc_ast_pretty Replace format flags u32 by enums and bools. 2023-01-27 08:53:39 +01:00
rustc_attr Fix uninlined_format_args for some compiler crates 2023-01-05 19:01:12 +01:00
rustc_baked_icu_data Update crate documentation of rustc_baked_icu_data crate 2022-11-18 14:46:36 -08:00
rustc_borrowck Auto merge of #107435 - matthiaskrgr:rollup-if5h6yu, r=matthiaskrgr 2023-01-29 07:01:58 +00:00
rustc_builtin_macros Allow more deriving on packed structs. 2023-01-30 12:00:42 +11:00
rustc_codegen_cranelift Auto merge of #106227 - bryangarza:ctfe-limit, r=oli-obk 2023-01-29 04:11:27 +00:00
rustc_codegen_gcc create and use GlobalAlloc::address_space 2023-01-25 01:46:19 -05:00
rustc_codegen_llvm make more pleasant to read 2023-01-28 16:35:07 +02:00
rustc_codegen_ssa Auto merge of #107435 - matthiaskrgr:rollup-if5h6yu, r=matthiaskrgr 2023-01-29 07:01:58 +00:00
rustc_const_eval Auto merge of #106227 - bryangarza:ctfe-limit, r=oli-obk 2023-01-29 04:11:27 +00:00
rustc_data_structures Auto merge of #106227 - bryangarza:ctfe-limit, r=oli-obk 2023-01-29 04:11:27 +00:00
rustc_driver Store the gctxt instead of fetching it twice. 2023-01-23 10:35:21 +00:00
rustc_error_codes compiler: Fix E0587 explanation 2023-01-27 10:59:51 +01:00
rustc_error_messages Auto merge of #106253 - nbdd0121:upcast, r=compiler-errors 2023-01-29 10:20:25 +00:00
rustc_errors add debug assertion for suggestions with overlapping parts 2023-01-20 20:16:37 +01:00
rustc_expand preserve delim spans during macro_rules! expansion if able 2023-01-20 20:16:37 +01:00
rustc_feature Reintroduce multiple_supertrait_upcastable lint 2023-01-28 15:08:07 +00:00
rustc_fs_util Remove useless borrows and derefs 2022-12-01 17:34:43 +00:00
rustc_graphviz Fix uninlined_format_args for some compiler crates 2023-01-05 19:01:12 +01:00
rustc_hir Remove HirId -> LocalDefId map from HIR. 2023-01-28 09:55:26 +00:00
rustc_hir_analysis Auto merge of #106253 - nbdd0121:upcast, r=compiler-errors 2023-01-29 10:20:25 +00:00
rustc_hir_pretty parse const closures 2023-01-12 02:28:37 +00:00
rustc_hir_typeck Auto merge of #106253 - nbdd0121:upcast, r=compiler-errors 2023-01-29 10:20:25 +00:00
rustc_incremental Remove HirId -> LocalDefId map from HIR. 2023-01-28 09:55:26 +00:00
rustc_index Fix IndexVec::drain_enumerated 2023-01-19 15:25:33 +00:00
rustc_infer Auto merge of #107303 - compiler-errors:intern-canonical-var-values, r=lcnr 2023-01-28 19:41:21 +00:00
rustc_interface Auto merge of #106227 - bryangarza:ctfe-limit, r=oli-obk 2023-01-29 04:11:27 +00:00
rustc_lexer Remove double spaces after dots in comments 2023-01-17 08:09:33 +00:00
rustc_lint Allow more deriving on packed structs. 2023-01-30 12:00:42 +11:00
rustc_lint_defs Allow more deriving on packed structs. 2023-01-30 12:00:42 +11:00
rustc_llvm Rollup merge of #106113 - krasimirgg:llvm-16-ext-tyid, r=nikic 2023-01-21 23:20:58 -05:00
rustc_log Add log-backtrace option to show backtraces along with logging 2023-01-12 00:17:48 +09:00
rustc_macros Detect references to non-existant messages in Fluent resources 2023-01-27 11:27:22 +00:00
rustc_metadata Auto merge of #107406 - cjgillot:eliminate-witnesses, r=compiler-errors 2023-01-29 01:27:11 +00:00
rustc_middle Allow more deriving on packed structs. 2023-01-30 12:00:42 +11:00
rustc_mir_build Remove HirId -> LocalDefId map from HIR. 2023-01-28 09:55:26 +00:00
rustc_mir_dataflow Create stable metric to measure long computation in Const Eval 2023-01-23 23:56:22 +00:00
rustc_mir_transform Allow more deriving on packed structs. 2023-01-30 12:00:42 +11:00
rustc_monomorphize add EarlyBinder::no_bound_vars 2023-01-26 20:28:31 -07:00
rustc_parse Migrate some range parsing diagnostics 2023-01-28 21:57:35 +00:00
rustc_parse_format Replace format flags u32 by enums and bools. 2023-01-27 08:53:39 +01:00
rustc_passes Remove HirId -> LocalDefId map from HIR. 2023-01-28 09:55:26 +00:00
rustc_plugin_impl Migrate all diagnostics 2022-10-23 10:09:44 +02:00
rustc_privacy Remove HirId -> LocalDefId map from HIR. 2023-01-28 09:55:26 +00:00
rustc_query_impl Fix uninlined_format_args for some compiler crates 2023-01-05 19:01:12 +01:00
rustc_query_system Remove double spaces after dots in comments 2023-01-17 08:09:33 +00:00
rustc_resolve suggest qualifying bare associated constants 2023-01-25 08:58:27 -05:00
rustc_save_analysis Remove HirId -> LocalDefId map from HIR. 2023-01-28 09:55:26 +00:00
rustc_serialize compiler: remove unnecessary imports and qualified paths 2022-12-10 18:45:34 +01:00
rustc_session Auto merge of #106227 - bryangarza:ctfe-limit, r=oli-obk 2023-01-29 04:11:27 +00:00
rustc_smir
rustc_span Reintroduce multiple_supertrait_upcastable lint 2023-01-28 15:08:07 +00:00
rustc_symbol_mangling Introduce GeneratorWitnessMIR. 2023-01-27 18:58:44 +00:00
rustc_target Rollup merge of #107248 - erikdesjardins:addrspace, r=oli-obk 2023-01-26 06:15:27 +01:00
rustc_trait_selection Auto merge of #106253 - nbdd0121:upcast, r=compiler-errors 2023-01-29 10:20:25 +00:00
rustc_traits Auto merge of #106253 - nbdd0121:upcast, r=compiler-errors 2023-01-29 10:20:25 +00:00
rustc_transmute Also remove #![feature(control_flow_enum)] where possible 2023-01-18 10:22:21 -08:00
rustc_ty_utils Pacify tidy. 2023-01-27 22:01:25 +00:00
rustc_type_ir Separate witness type computation from the generator transform. 2023-01-27 19:00:26 +00:00