From ee85bfdcc23de49bbc565142efff87b14a7fc775 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Fri, 16 Feb 2018 11:33:22 +0100 Subject: [PATCH 1/3] Make core::nonzero private It is now an implementation detail of ptr::NonNull and num::NonZero* --- src/libcore/lib.rs | 2 +- src/libcore/nonzero.rs | 96 +------------------ src/libcore/num/mod.rs | 4 +- src/libcore/ptr.rs | 11 +-- .../ctfe/tuple-struct-constructors.rs | 7 +- 5 files changed, 10 insertions(+), 110 deletions(-) diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 54f35d17974fb..06fbfcecba801 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -171,7 +171,6 @@ pub mod prelude; pub mod intrinsics; pub mod mem; -pub mod nonzero; pub mod ptr; pub mod hint; @@ -221,6 +220,7 @@ pub mod heap { // note: does not need to be public mod iter_private; +mod nonzero; mod tuple; mod unit; diff --git a/src/libcore/nonzero.rs b/src/libcore/nonzero.rs index 19836d98844e2..ee5230cef8dd9 100644 --- a/src/libcore/nonzero.rs +++ b/src/libcore/nonzero.rs @@ -9,103 +9,13 @@ // except according to those terms. //! Exposes the NonZero lang item which provides optimization hints. -#![unstable(feature = "nonzero", reason = "deprecated", issue = "49137")] -#![rustc_deprecated(reason = "use `std::ptr::NonNull` or `std::num::NonZero*` instead", - since = "1.26.0")] -#![allow(deprecated)] use ops::CoerceUnsized; -/// Unsafe trait to indicate what types are usable with the NonZero struct -pub unsafe trait Zeroable { - /// Whether this value is zero - fn is_zero(&self) -> bool; -} - -macro_rules! impl_zeroable_for_pointer_types { - ( $( $Ptr: ty )+ ) => { - $( - /// For fat pointers to be considered "zero", only the "data" part needs to be null. - unsafe impl Zeroable for $Ptr { - #[inline] - fn is_zero(&self) -> bool { - (*self).is_null() - } - } - )+ - } -} - -macro_rules! impl_zeroable_for_integer_types { - ( $( $Int: ty )+ ) => { - $( - unsafe impl Zeroable for $Int { - #[inline] - fn is_zero(&self) -> bool { - *self == 0 - } - } - )+ - } -} - -impl_zeroable_for_pointer_types! { - *const T - *mut T -} - -impl_zeroable_for_integer_types! { - usize u8 u16 u32 u64 u128 - isize i8 i16 i32 i64 i128 -} - /// A wrapper type for raw pointers and integers that will never be /// NULL or 0 that might allow certain optimizations. #[lang = "non_zero"] -#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)] -pub struct NonZero(pub(crate) T); - -impl NonZero { - /// Creates an instance of NonZero with the provided value. - /// You must indeed ensure that the value is actually "non-zero". - #[inline] - pub const unsafe fn new_unchecked(inner: T) -> Self { - NonZero(inner) - } - - /// Creates an instance of NonZero with the provided value. - #[inline] - pub fn new(inner: T) -> Option { - if inner.is_zero() { - None - } else { - Some(NonZero(inner)) - } - } - - /// Gets the inner value. - pub fn get(self) -> T { - self.0 - } -} - -impl, U: Zeroable> CoerceUnsized> for NonZero {} - -impl<'a, T: ?Sized> From<&'a mut T> for NonZero<*mut T> { - fn from(reference: &'a mut T) -> Self { - NonZero(reference) - } -} - -impl<'a, T: ?Sized> From<&'a mut T> for NonZero<*const T> { - fn from(reference: &'a mut T) -> Self { - let ptr: *mut T = reference; - NonZero(ptr) - } -} +#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] +pub(crate) struct NonZero(pub(crate) T); -impl<'a, T: ?Sized> From<&'a T> for NonZero<*const T> { - fn from(reference: &'a T) -> Self { - NonZero(reference) - } -} +impl, U> CoerceUnsized> for NonZero {} diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index a062fbda5bad0..ef914a0fc5cd7 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -16,7 +16,7 @@ use convert::TryFrom; use fmt; use intrinsics; use mem; -#[allow(deprecated)] use nonzero::NonZero; +use nonzero::NonZero; use ops; use str::FromStr; @@ -49,11 +49,9 @@ macro_rules! nonzero_integers { /// ``` #[$stability] #[$deprecation] - #[allow(deprecated)] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct $Ty(NonZero<$Int>); - #[allow(deprecated)] impl $Ty { /// Create a non-zero without checking the value. /// diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 83dfac7a3a2ea..63bcc02402015 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -23,7 +23,7 @@ use fmt; use hash; use marker::{PhantomData, Unsize}; use mem; -#[allow(deprecated)] use nonzero::NonZero; +use nonzero::NonZero; use cmp::Ordering::{self, Less, Equal, Greater}; @@ -2742,7 +2742,6 @@ impl PartialOrd for *mut T { #[unstable(feature = "ptr_internals", issue = "0", reason = "use NonNull instead and consider PhantomData \ (if you also use #[may_dangle]), Send, and/or Sync")] -#[allow(deprecated)] #[doc(hidden)] pub struct Unique { pointer: NonZero<*const T>, @@ -2790,7 +2789,6 @@ impl Unique { } #[unstable(feature = "ptr_internals", issue = "0")] -#[allow(deprecated)] impl Unique { /// Creates a new `Unique`. /// @@ -2855,7 +2853,6 @@ impl fmt::Pointer for Unique { } #[unstable(feature = "ptr_internals", issue = "0")] -#[allow(deprecated)] impl<'a, T: ?Sized> From<&'a mut T> for Unique { fn from(reference: &'a mut T) -> Self { Unique { pointer: NonZero(reference as _), _marker: PhantomData } @@ -2863,7 +2860,6 @@ impl<'a, T: ?Sized> From<&'a mut T> for Unique { } #[unstable(feature = "ptr_internals", issue = "0")] -#[allow(deprecated)] impl<'a, T: ?Sized> From<&'a T> for Unique { fn from(reference: &'a T) -> Self { Unique { pointer: NonZero(reference as _), _marker: PhantomData } @@ -2896,7 +2892,7 @@ impl<'a, T: ?Sized> From> for Unique { /// provide a public API that follows the normal shared XOR mutable rules of Rust. #[stable(feature = "nonnull", since = "1.25.0")] pub struct NonNull { - #[allow(deprecated)] pointer: NonZero<*const T>, + pointer: NonZero<*const T>, } /// `NonNull` pointers are not `Send` because the data they reference may be aliased. @@ -2923,7 +2919,6 @@ impl NonNull { } } -#[allow(deprecated)] impl NonNull { /// Creates a new `NonNull`. /// @@ -3054,7 +3049,6 @@ impl From> for NonNull { } #[stable(feature = "nonnull", since = "1.25.0")] -#[allow(deprecated)] impl<'a, T: ?Sized> From<&'a mut T> for NonNull { fn from(reference: &'a mut T) -> Self { NonNull { pointer: NonZero(reference as _) } @@ -3062,7 +3056,6 @@ impl<'a, T: ?Sized> From<&'a mut T> for NonNull { } #[stable(feature = "nonnull", since = "1.25.0")] -#[allow(deprecated)] impl<'a, T: ?Sized> From<&'a T> for NonNull { fn from(reference: &'a T) -> Self { NonNull { pointer: NonZero(reference as _) } diff --git a/src/test/run-pass/ctfe/tuple-struct-constructors.rs b/src/test/run-pass/ctfe/tuple-struct-constructors.rs index ecc5d37663679..ee1ce192fe03c 100644 --- a/src/test/run-pass/ctfe/tuple-struct-constructors.rs +++ b/src/test/run-pass/ctfe/tuple-struct-constructors.rs @@ -10,11 +10,10 @@ // https://github.com/rust-lang/rust/issues/41898 -#![feature(nonzero, const_fn)] -extern crate core; -use core::nonzero::NonZero; +#![feature(nonzero)] +use std::num::NonZeroU64; fn main() { - const FOO: NonZero = unsafe { NonZero::new_unchecked(2) }; + const FOO: NonZeroU64 = unsafe { NonZeroU64::new_unchecked(2) }; if let FOO = FOO {} } From c536639c1e1ce7e2b4816a40f2e954c581431456 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Sat, 24 Mar 2018 11:36:29 +0100 Subject: [PATCH 2/3] Remove unstable deprecated num::NonZeroI* types --- src/libcore/num/mod.rs | 18 +----------------- src/libstd/num.rs | 5 +---- 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index ef914a0fc5cd7..342f57b50f0d4 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -24,7 +24,6 @@ macro_rules! impl_nonzero_fmt { ( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => { $( #[$stability] - #[allow(deprecated)] impl fmt::$Trait for $Ty { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -36,7 +35,7 @@ macro_rules! impl_nonzero_fmt { } macro_rules! nonzero_integers { - ( #[$stability: meta] #[$deprecation: meta] $( $Ty: ident($Int: ty); )+ ) => { + ( #[$stability: meta] $( $Ty: ident($Int: ty); )+ ) => { $( /// An integer that is known not to equal zero. /// @@ -48,7 +47,6 @@ macro_rules! nonzero_integers { /// assert_eq!(size_of::>(), size_of::()); /// ``` #[$stability] - #[$deprecation] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct $Ty(NonZero<$Int>); @@ -94,7 +92,6 @@ macro_rules! nonzero_integers { nonzero_integers! { #[unstable(feature = "nonzero", issue = "49137")] - #[allow(deprecated)] // Redundant, works around "error: inconsistent lockstep iteration" NonZeroU8(u8); NonZeroU16(u16); NonZeroU32(u32); @@ -103,19 +100,6 @@ nonzero_integers! { NonZeroUsize(usize); } -nonzero_integers! { - #[unstable(feature = "nonzero", issue = "49137")] - #[rustc_deprecated(since = "1.26.0", reason = "\ - signed non-zero integers are considered for removal due to lack of known use cases. \ - If you’re using them, please comment on https://github.com/rust-lang/rust/issues/49137")] - NonZeroI8(i8); - NonZeroI16(i16); - NonZeroI32(i32); - NonZeroI64(i64); - NonZeroI128(i128); - NonZeroIsize(isize); -} - /// Provides intentionally-wrapped arithmetic on `T`. /// /// Operations like `+` on `u32` values is intended to never overflow, diff --git a/src/libstd/num.rs b/src/libstd/num.rs index 4b975dd912a1a..aa806b947b097 100644 --- a/src/libstd/num.rs +++ b/src/libstd/num.rs @@ -23,10 +23,7 @@ pub use core::num::Wrapping; #[unstable(feature = "nonzero", issue = "49137")] #[allow(deprecated)] -pub use core::num::{ - NonZeroU8, NonZeroI8, NonZeroU16, NonZeroI16, NonZeroU32, NonZeroI32, - NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize, -}; +pub use core::num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize}; #[cfg(test)] use fmt; #[cfg(test)] use ops::{Add, Sub, Mul, Div, Rem}; From 89d9ca9b50e01cbc5dc78a26f15cc8c435bbc5a4 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Wed, 16 May 2018 18:07:35 +0200 Subject: [PATCH 3/3] Stabilize num::NonZeroU* Tracking issue: https://github.com/rust-lang/rust/issues/49137 --- src/liballoc/lib.rs | 1 - src/libcore/num/mod.rs | 16 +++++++--------- src/libcore/tests/lib.rs | 1 - src/librustc/lib.rs | 1 - src/librustc_data_structures/lib.rs | 1 - src/librustc_mir/lib.rs | 1 - src/libstd/lib.rs | 1 - src/libstd/num.rs | 3 +-- .../run-pass/ctfe/tuple-struct-constructors.rs | 1 - src/test/run-pass/enum-null-pointer-opt.rs | 2 -- src/test/ui/print_type_sizes/niche-filling.rs | 1 - 11 files changed, 8 insertions(+), 21 deletions(-) diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index bb78c14b90586..f7dd9d4f010a4 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -102,7 +102,6 @@ #![feature(lang_items)] #![feature(libc)] #![feature(needs_allocator)] -#![feature(nonzero)] #![feature(optin_builtin_traits)] #![feature(pattern)] #![feature(pin)] diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 342f57b50f0d4..ae8b3087240b8 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -21,9 +21,9 @@ use ops; use str::FromStr; macro_rules! impl_nonzero_fmt { - ( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => { + ( ( $( $Trait: ident ),+ ) for $Ty: ident ) => { $( - #[$stability] + #[stable(feature = "nonzero", since = "1.28.0")] impl fmt::$Trait for $Ty { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -35,7 +35,7 @@ macro_rules! impl_nonzero_fmt { } macro_rules! nonzero_integers { - ( #[$stability: meta] $( $Ty: ident($Int: ty); )+ ) => { + ( $( $Ty: ident($Int: ty); )+ ) => { $( /// An integer that is known not to equal zero. /// @@ -46,7 +46,7 @@ macro_rules! nonzero_integers { /// use std::mem::size_of; /// assert_eq!(size_of::>(), size_of::()); /// ``` - #[$stability] + #[stable(feature = "nonzero", since = "1.28.0")] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct $Ty(NonZero<$Int>); @@ -56,14 +56,14 @@ macro_rules! nonzero_integers { /// # Safety /// /// The value must not be zero. - #[$stability] + #[stable(feature = "nonzero", since = "1.28.0")] #[inline] pub const unsafe fn new_unchecked(n: $Int) -> Self { $Ty(NonZero(n)) } /// Create a non-zero if the given value is not zero. - #[$stability] + #[stable(feature = "nonzero", since = "1.28.0")] #[inline] pub fn new(n: $Int) -> Option { if n != 0 { @@ -74,7 +74,7 @@ macro_rules! nonzero_integers { } /// Returns the value as a primitive type. - #[$stability] + #[stable(feature = "nonzero", since = "1.28.0")] #[inline] pub fn get(self) -> $Int { self.0 .0 @@ -83,7 +83,6 @@ macro_rules! nonzero_integers { } impl_nonzero_fmt! { - #[$stability] (Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty } )+ @@ -91,7 +90,6 @@ macro_rules! nonzero_integers { } nonzero_integers! { - #[unstable(feature = "nonzero", issue = "49137")] NonZeroU8(u8); NonZeroU16(u16); NonZeroU32(u32); diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs index 5e98e40e0d5e6..7fb4b503c01ef 100644 --- a/src/libcore/tests/lib.rs +++ b/src/libcore/tests/lib.rs @@ -26,7 +26,6 @@ #![feature(iterator_step_by)] #![feature(iterator_flatten)] #![feature(iterator_repeat_with)] -#![feature(nonzero)] #![feature(pattern)] #![feature(range_is_empty)] #![feature(raw)] diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 26ac9d6ee9ea6..ac6ff6831adb8 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -56,7 +56,6 @@ #![feature(never_type)] #![feature(exhaustive_patterns)] #![feature(non_exhaustive)] -#![feature(nonzero)] #![feature(proc_macro_internals)] #![feature(quote)] #![feature(optin_builtin_traits)] diff --git a/src/librustc_data_structures/lib.rs b/src/librustc_data_structures/lib.rs index b2e7450e76cdf..328ff76c1cdd0 100644 --- a/src/librustc_data_structures/lib.rs +++ b/src/librustc_data_structures/lib.rs @@ -21,7 +21,6 @@ html_root_url = "https://doc.rust-lang.org/nightly/")] #![feature(collections_range)] -#![feature(nonzero)] #![feature(unboxed_closures)] #![feature(fn_traits)] #![feature(unsize)] diff --git a/src/librustc_mir/lib.rs b/src/librustc_mir/lib.rs index fbc0facbc4967..ecced1b81682e 100644 --- a/src/librustc_mir/lib.rs +++ b/src/librustc_mir/lib.rs @@ -30,7 +30,6 @@ Rust MIR: a lowered representation of Rust. Also: an experiment! #![feature(exhaustive_patterns)] #![feature(range_contains)] #![feature(rustc_diagnostic_macros)] -#![feature(nonzero)] #![feature(inclusive_range_methods)] #![feature(crate_visibility_modifier)] #![feature(never_type)] diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index d41739ab02c6a..9cdc6a2162246 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -277,7 +277,6 @@ #![feature(needs_panic_runtime)] #![feature(never_type)] #![feature(exhaustive_patterns)] -#![feature(nonzero)] #![feature(num_bits_bytes)] #![feature(old_wrapping)] #![feature(on_unimplemented)] diff --git a/src/libstd/num.rs b/src/libstd/num.rs index aa806b947b097..3f90c1fa3b1f2 100644 --- a/src/libstd/num.rs +++ b/src/libstd/num.rs @@ -21,8 +21,7 @@ pub use core::num::{FpCategory, ParseIntError, ParseFloatError, TryFromIntError} #[stable(feature = "rust1", since = "1.0.0")] pub use core::num::Wrapping; -#[unstable(feature = "nonzero", issue = "49137")] -#[allow(deprecated)] +#[stable(feature = "nonzero", since = "1.28.0")] pub use core::num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize}; #[cfg(test)] use fmt; diff --git a/src/test/run-pass/ctfe/tuple-struct-constructors.rs b/src/test/run-pass/ctfe/tuple-struct-constructors.rs index ee1ce192fe03c..d5f3e88fd52e8 100644 --- a/src/test/run-pass/ctfe/tuple-struct-constructors.rs +++ b/src/test/run-pass/ctfe/tuple-struct-constructors.rs @@ -10,7 +10,6 @@ // https://github.com/rust-lang/rust/issues/41898 -#![feature(nonzero)] use std::num::NonZeroU64; fn main() { diff --git a/src/test/run-pass/enum-null-pointer-opt.rs b/src/test/run-pass/enum-null-pointer-opt.rs index 12f17a1575e82..34ed589d418c1 100644 --- a/src/test/run-pass/enum-null-pointer-opt.rs +++ b/src/test/run-pass/enum-null-pointer-opt.rs @@ -8,8 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(nonzero, core)] - use std::mem::size_of; use std::num::NonZeroUsize; use std::ptr::NonNull; diff --git a/src/test/ui/print_type_sizes/niche-filling.rs b/src/test/ui/print_type_sizes/niche-filling.rs index 1aad0b760b17b..17e7a21cd02e5 100644 --- a/src/test/ui/print_type_sizes/niche-filling.rs +++ b/src/test/ui/print_type_sizes/niche-filling.rs @@ -22,7 +22,6 @@ // padding and overall computed sizes can be quite different. #![feature(start)] -#![feature(nonzero)] #![allow(dead_code)] use std::num::NonZeroU32;