From 530d7bc5174e93682a38b22ac7ff4e3569bcd813 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Wed, 6 Jun 2018 13:30:35 +0200 Subject: [PATCH] Add #[repr(transparent)] to some libcore types * `UnsafeCell` * `Cell` * `NonZero*` * `NonNull` * `Unique` --- src/libcore/cell.rs | 2 ++ src/libcore/lib.rs | 1 + src/libcore/nonzero.rs | 1 + src/libcore/num/mod.rs | 2 ++ src/libcore/ptr.rs | 2 ++ 5 files changed, 8 insertions(+) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index d50ad580ed574..f922f8f2d93bd 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -235,6 +235,7 @@ use ptr; /// /// See the [module-level documentation](index.html) for more. #[stable(feature = "rust1", since = "1.0.0")] +#[repr(transparent)] pub struct Cell { value: UnsafeCell, } @@ -1282,6 +1283,7 @@ impl<'a, T: ?Sized + fmt::Display> fmt::Display for RefMut<'a, T> { /// ``` #[lang = "unsafe_cell"] #[stable(feature = "rust1", since = "1.0.0")] +#[repr(transparent)] pub struct UnsafeCell { value: T, } diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 40caee855410e..2d227ebc8c21b 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -124,6 +124,7 @@ #![feature(const_slice_len)] #![feature(const_str_as_bytes)] #![feature(const_str_len)] +#![cfg_attr(stage0, feature(repr_transparent))] #[prelude_import] #[allow(unused)] diff --git a/src/libcore/nonzero.rs b/src/libcore/nonzero.rs index ee5230cef8dd9..cc36ea7f71391 100644 --- a/src/libcore/nonzero.rs +++ b/src/libcore/nonzero.rs @@ -16,6 +16,7 @@ use ops::CoerceUnsized; /// NULL or 0 that might allow certain optimizations. #[lang = "non_zero"] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[repr(transparent)] pub(crate) struct NonZero(pub(crate) T); impl, U> CoerceUnsized> for NonZero {} diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 1168126c47c93..3aeb11832180f 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -48,6 +48,7 @@ macro_rules! nonzero_integers { /// ``` #[stable(feature = "nonzero", since = "1.28.0")] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] + #[repr(transparent)] pub struct $Ty(NonZero<$Int>); impl $Ty { @@ -123,6 +124,7 @@ nonzero_integers! { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Hash)] +#[repr(transparent)] pub struct Wrapping(#[stable(feature = "rust1", since = "1.0.0")] pub T); diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 81a8b3ef0474d..0070defa95362 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2692,6 +2692,7 @@ impl PartialOrd for *mut T { reason = "use NonNull instead and consider PhantomData \ (if you also use #[may_dangle]), Send, and/or Sync")] #[doc(hidden)] +#[repr(transparent)] pub struct Unique { pointer: NonZero<*const T>, // NOTE: this marker has no consequences for variance, but is necessary @@ -2840,6 +2841,7 @@ impl<'a, T: ?Sized> From> for Unique { /// such as Box, Rc, Arc, Vec, and LinkedList. This is the case because they /// provide a public API that follows the normal shared XOR mutable rules of Rust. #[stable(feature = "nonnull", since = "1.25.0")] +#[repr(transparent)] pub struct NonNull { pointer: NonZero<*const T>, }