Skip to content

Commit 96fc9a9

Browse files
committed
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
1 parent 9c707a8 commit 96fc9a9

File tree

7 files changed

+121
-9
lines changed

7 files changed

+121
-9
lines changed

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ fn visit_implementation_of_copy(checker: &Checker<'_>) -> Result<(), ErrorGuaran
103103
}
104104

105105
let cause = traits::ObligationCause::misc(DUMMY_SP, impl_did);
106-
match type_allowed_to_implement_copy(tcx, param_env, self_type, cause) {
106+
match type_allowed_to_implement_copy(tcx, param_env, self_type, cause, impl_header.safety) {
107107
Ok(()) => Ok(()),
108108
Err(CopyImplementationError::InfringingFields(fields)) => {
109109
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
123123
let span = tcx.hir().expect_item(impl_did).expect_impl().self_ty.span;
124124
Err(tcx.dcx().emit_err(errors::CopyImplOnTypeWithDtor { span }))
125125
}
126+
Err(CopyImplementationError::HasUnsafeFields) => {
127+
let span = tcx.hir().expect_item(impl_did).expect_impl().self_ty.span;
128+
Err(tcx
129+
.dcx()
130+
.span_delayed_bug(span, format!("cannot implement `Copy` for `{}`", self_type)))
131+
}
126132
}
127133
}
128134

compiler/rustc_hir_analysis/src/coherence/unsafety.rs

+31-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use rustc_errors::codes::*;
55
use rustc_errors::struct_span_code_err;
6-
use rustc_hir::Safety;
6+
use rustc_hir::{LangItem, Safety};
77
use rustc_middle::ty::ImplPolarity::*;
88
use rustc_middle::ty::print::PrintTraitRefExt as _;
99
use rustc_middle::ty::{ImplTraitHeader, TraitDef, TyCtxt};
@@ -20,7 +20,20 @@ pub(super) fn check_item(
2020
tcx.generics_of(def_id).own_params.iter().find(|p| p.pure_wrt_drop).map(|_| "may_dangle");
2121
let trait_ref = trait_header.trait_ref.instantiate_identity();
2222

23-
match (trait_def.safety, unsafe_attr, trait_header.safety, trait_header.polarity) {
23+
let is_copy = tcx.is_lang_item(trait_def.def_id, LangItem::Copy);
24+
let trait_def_safety = if is_copy {
25+
use rustc_type_ir::inherent::*;
26+
// If `Self` has unsafe fields, `Copy` is unsafe to implement.
27+
if trait_header.trait_ref.skip_binder().self_ty().has_unsafe_fields() {
28+
rustc_hir::Safety::Unsafe
29+
} else {
30+
rustc_hir::Safety::Safe
31+
}
32+
} else {
33+
trait_def.safety
34+
};
35+
36+
match (trait_def_safety, unsafe_attr, trait_header.safety, trait_header.polarity) {
2437
(Safety::Safe, None, Safety::Unsafe, Positive | Reservation) => {
2538
let span = tcx.def_span(def_id);
2639
return Err(struct_span_code_err!(
@@ -48,12 +61,22 @@ pub(super) fn check_item(
4861
"the trait `{}` requires an `unsafe impl` declaration",
4962
trait_ref.print_trait_sugared()
5063
)
51-
.with_note(format!(
52-
"the trait `{}` enforces invariants that the compiler can't check. \
53-
Review the trait documentation and make sure this implementation \
54-
upholds those invariants before adding the `unsafe` keyword",
55-
trait_ref.print_trait_sugared()
56-
))
64+
.with_note(if is_copy {
65+
format!(
66+
"the trait `{}` cannot be safely implemented for `{}` \
67+
because it has unsafe fields. Review the invariants \
68+
of those fields before adding an `unsafe impl`",
69+
trait_ref.print_trait_sugared(),
70+
trait_ref.self_ty(),
71+
)
72+
} else {
73+
format!(
74+
"the trait `{}` enforces invariants that the compiler can't check. \
75+
Review the trait documentation and make sure this implementation \
76+
upholds those invariants before adding the `unsafe` keyword",
77+
trait_ref.print_trait_sugared()
78+
)
79+
})
5780
.with_span_suggestion_verbose(
5881
span.shrink_to_lo(),
5982
"add `unsafe` to this trait implementation",

compiler/rustc_lint/src/builtin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingCopyImplementations {
625625
cx.param_env,
626626
ty,
627627
traits::ObligationCause::misc(item.span, item.owner_id.def_id),
628+
hir::Safety::Safe,
628629
)
629630
.is_ok()
630631
{

compiler/rustc_trait_selection/src/traits/misc.rs

+12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub enum CopyImplementationError<'tcx> {
1818
InfringingFields(Vec<(&'tcx ty::FieldDef, Ty<'tcx>, InfringingFieldsReason<'tcx>)>),
1919
NotAnAdt,
2020
HasDestructor,
21+
HasUnsafeFields,
2122
}
2223

2324
pub enum ConstParamTyImplementationError<'tcx> {
@@ -39,12 +40,19 @@ pub enum InfringingFieldsReason<'tcx> {
3940
///
4041
/// If it's not an ADT, int ty, `bool`, float ty, `char`, raw pointer, `!`,
4142
/// a reference or an array returns `Err(NotAnAdt)`.
43+
///
44+
/// If the impl is `Safe`, `self_type` must not have unsafe fields. When used to
45+
/// generate suggestions in lints, `Safe` should be supplied so as to not
46+
/// suggest implementing `Copy` for types with unsafe fields.
4247
pub fn type_allowed_to_implement_copy<'tcx>(
4348
tcx: TyCtxt<'tcx>,
4449
param_env: ty::ParamEnv<'tcx>,
4550
self_type: Ty<'tcx>,
4651
parent_cause: ObligationCause<'tcx>,
52+
impl_safety: hir::Safety,
4753
) -> Result<(), CopyImplementationError<'tcx>> {
54+
use rustc_type_ir::inherent::*;
55+
4856
let (adt, args) = match self_type.kind() {
4957
// These types used to have a builtin impl.
5058
// Now libcore provides that impl.
@@ -78,6 +86,10 @@ pub fn type_allowed_to_implement_copy<'tcx>(
7886
return Err(CopyImplementationError::HasDestructor);
7987
}
8088

89+
if impl_safety == hir::Safety::Safe && self_type.has_unsafe_fields() {
90+
return Err(CopyImplementationError::HasUnsafeFields);
91+
}
92+
8193
Ok(())
8294
}
8395

src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs

+1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
200200
cx.param_env,
201201
ty,
202202
traits::ObligationCause::dummy_with_span(span),
203+
rustc_hir::Safety::Safe,
203204
)
204205
.is_ok()
205206
{

tests/ui/unsafe-fields/copy-trait.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//@ compile-flags: --crate-type=lib
2+
3+
#![feature(unsafe_fields)]
4+
#![allow(incomplete_features)]
5+
#![deny(missing_copy_implementations)]
6+
7+
mod good_safe_impl {
8+
enum SafeEnum {
9+
Safe(u8),
10+
}
11+
12+
impl Copy for SafeEnum {}
13+
}
14+
15+
mod bad_safe_impl {
16+
enum UnsafeEnum {
17+
Safe(u8),
18+
Unsafe { unsafe field: u8 },
19+
}
20+
21+
impl Copy for UnsafeEnum {}
22+
//~^ ERROR the trait `Copy` requires an `unsafe impl` declaration
23+
}
24+
25+
mod good_unsafe_impl {
26+
enum UnsafeEnum {
27+
Safe(u8),
28+
Unsafe { unsafe field: u8 },
29+
}
30+
31+
unsafe impl Copy for UnsafeEnum {}
32+
}
33+
34+
mod bad_unsafe_impl {
35+
enum SafeEnum {
36+
Safe(u8),
37+
}
38+
39+
unsafe impl Copy for SafeEnum {}
40+
//~^ ERROR implementing the trait `Copy` is not unsafe
41+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0200]: the trait `Copy` requires an `unsafe impl` declaration
2+
--> $DIR/copy-trait.rs:21:5
3+
|
4+
LL | impl Copy for UnsafeEnum {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: the trait `Copy` cannot be safely implemented for `bad_safe_impl::UnsafeEnum` because it has unsafe fields. Review the invariants of those fields before adding an `unsafe impl`
8+
help: add `unsafe` to this trait implementation
9+
|
10+
LL | unsafe impl Copy for UnsafeEnum {}
11+
| ++++++
12+
13+
error[E0199]: implementing the trait `Copy` is not unsafe
14+
--> $DIR/copy-trait.rs:39:5
15+
|
16+
LL | unsafe impl Copy for SafeEnum {}
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
|
19+
help: remove `unsafe` from this trait implementation
20+
|
21+
LL - unsafe impl Copy for SafeEnum {}
22+
LL + impl Copy for SafeEnum {}
23+
|
24+
25+
error: aborting due to 2 previous errors
26+
27+
Some errors have detailed explanations: E0199, E0200.
28+
For more information about an error, try `rustc --explain E0199`.

0 commit comments

Comments
 (0)