diff options
| author | Jack Wrenn <jack@wrenn.fyi> | 2024-12-06 17:11:36 +0000 | 
|---|---|---|
| committer | Jack Wrenn <jack@wrenn.fyi> | 2024-12-07 20:50:00 +0000 | 
| commit | 3ce35a4ec5f06828f908a018da083af5eb54301a (patch) | |
| tree | 23f073f2fb0ee1d071550e88ee7d36f2a7d1e9e8 /compiler/rustc_hir_analysis/src/coherence/builtin.rs | |
| parent | 9c707a8b769523bb6768bf58e74fa2c39cc24844 (diff) | |
| download | rust-3ce35a4ec5f06828f908a018da083af5eb54301a.tar.gz rust-3ce35a4ec5f06828f908a018da083af5eb54301a.zip | |
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 require `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
Diffstat (limited to 'compiler/rustc_hir_analysis/src/coherence/builtin.rs')
| -rw-r--r-- | compiler/rustc_hir_analysis/src/coherence/builtin.rs | 8 | 
1 files changed, 7 insertions, 1 deletions
| diff --git a/compiler/rustc_hir_analysis/src/coherence/builtin.rs b/compiler/rustc_hir_analysis/src/coherence/builtin.rs index 3b49bc41ffe..2eea65125b0 100644 --- a/compiler/rustc_hir_analysis/src/coherence/builtin.rs +++ b/compiler/rustc_hir_analysis/src/coherence/builtin.rs @@ -103,7 +103,7 @@ fn visit_implementation_of_copy(checker: &Checker<'_>) -> Result<(), ErrorGuaran } let cause = traits::ObligationCause::misc(DUMMY_SP, impl_did); - match type_allowed_to_implement_copy(tcx, param_env, self_type, cause) { + match type_allowed_to_implement_copy(tcx, param_env, self_type, cause, impl_header.safety) { Ok(()) => Ok(()), Err(CopyImplementationError::InfringingFields(fields)) => { let span = tcx.hir().expect_item(impl_did).expect_impl().self_ty.span; @@ -123,6 +123,12 @@ fn visit_implementation_of_copy(checker: &Checker<'_>) -> Result<(), ErrorGuaran let span = tcx.hir().expect_item(impl_did).expect_impl().self_ty.span; Err(tcx.dcx().emit_err(errors::CopyImplOnTypeWithDtor { span })) } + Err(CopyImplementationError::HasUnsafeFields) => { + let span = tcx.hir().expect_item(impl_did).expect_impl().self_ty.span; + Err(tcx + .dcx() + .span_delayed_bug(span, format!("cannot implement `Copy` for `{}`", self_type))) + } } } | 
