Skip to content

Commit 691aec2

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: rust-lang#132922
1 parent 2c205e1 commit 691aec2

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

example/mini_core.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,24 @@ impl<T: ?Sized> LegacyReceiver for &mut T {}
5252
impl<T: ?Sized, A: Allocator> LegacyReceiver for Box<T, A> {}
5353

5454
#[lang = "copy"]
55-
pub unsafe trait Copy {}
56-
57-
unsafe impl Copy for bool {}
58-
unsafe impl Copy for u8 {}
59-
unsafe impl Copy for u16 {}
60-
unsafe impl Copy for u32 {}
61-
unsafe impl Copy for u64 {}
62-
unsafe impl Copy for usize {}
63-
unsafe impl Copy for i8 {}
64-
unsafe impl Copy for i16 {}
65-
unsafe impl Copy for i32 {}
66-
unsafe impl Copy for isize {}
67-
unsafe impl Copy for f32 {}
68-
unsafe impl Copy for f64 {}
69-
unsafe impl Copy for char {}
70-
unsafe impl<'a, T: ?Sized> Copy for &'a T {}
71-
unsafe impl<T: ?Sized> Copy for *const T {}
72-
unsafe impl<T: ?Sized> Copy for *mut T {}
55+
pub trait Copy {}
56+
57+
impl Copy for bool {}
58+
impl Copy for u8 {}
59+
impl Copy for u16 {}
60+
impl Copy for u32 {}
61+
impl Copy for u64 {}
62+
impl Copy for usize {}
63+
impl Copy for i8 {}
64+
impl Copy for i16 {}
65+
impl Copy for i32 {}
66+
impl Copy for isize {}
67+
impl Copy for f32 {}
68+
impl Copy for f64 {}
69+
impl Copy for char {}
70+
impl<'a, T: ?Sized> Copy for &'a T {}
71+
impl<T: ?Sized> Copy for *const T {}
72+
impl<T: ?Sized> Copy for *mut T {}
7373

7474
#[lang = "sync"]
7575
pub unsafe trait Sync {}

0 commit comments

Comments
 (0)