rust/compiler/rustc_trait_selection/src
León Orell Valerian Liehr 3eaa785daa
Rollup merge of #134008 - jswrenn:unsafe-fields-copy, r=compiler-errors
Make `Copy` unsafe to implement for ADTs with `unsafe` fields

As a rule, the application of `unsafe` to a declaration requires that use-sites of that declaration also entail `unsafe`. For example, a field declared `unsafe` may only be read in the lexical context of an `unsafe` block.

For nearly all safe traits, the safety obligations of fields are explicitly discharged when they are mentioned in method definitions. For example, idiomatically implementing `Clone` (a safe trait) for a type with unsafe fields will require `unsafe` to clone those fields.

Prior to this commit, `Copy` violated this rule. The trait is marked safe, and although it has no explicit methods, its implementation permits reads of `Self`.

This commit resolves this by making `Copy` conditionally safe to implement. It remains safe to implement for ADTs without unsafe fields, but unsafe to implement for ADTs with unsafe fields.

Tracking: #132922

r? ```@compiler-errors```
2024-12-10 13:51:10 +01:00
..
error_reporting Add more info on type/trait mismatches for different crate versions 2024-12-07 18:18:08 +00:00
errors ty::BrK -> ty::BoundRegionKind::K 2024-11-04 04:45:52 +00:00
solve Assert that obligations are empty before deeply normalizing 2024-12-02 22:51:18 +00:00
traits Make Copy unsafe to implement for ADTs with unsafe fields 2024-12-07 20:50:00 +00:00
errors.rs Make precise capturing suggestion machine-applicable only if it has not APITs 2024-11-12 04:08:34 +00:00
infer.rs remove Ty::is_copy_modulo_regions 2024-12-02 13:57:56 +01:00
lib.rs Stabilize the map/value methods on ControlFlow 2024-09-25 19:00:17 -07:00
regions.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
solve.rs impossible obligations check fast path 2024-10-10 06:09:50 -04:00