Skip to content

Commit 03746a5

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 4d01ca8 commit 03746a5

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

example/mini_core.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,26 @@ impl<T: ?Sized> LegacyReceiver for &mut T {}
5555
impl<T: ?Sized> LegacyReceiver for Box<T> {}
5656

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

7979
#[lang = "sync"]
8080
pub unsafe trait Sync {}

0 commit comments

Comments
 (0)