]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #50808 - SimonSapin:nonzero, r=alexcrichton
authorkennytm <kennytm@gmail.com>
Wed, 16 May 2018 19:07:52 +0000 (03:07 +0800)
committerkennytm <kennytm@gmail.com>
Wed, 16 May 2018 21:18:21 +0000 (05:18 +0800)
Stabilize num::NonZeroU*

Tracking issue: https://github.com/rust-lang/rust/issues/49137

14 files changed:
src/liballoc/lib.rs
src/libcore/lib.rs
src/libcore/nonzero.rs
src/libcore/num/mod.rs
src/libcore/ptr.rs
src/libcore/tests/lib.rs
src/librustc/lib.rs
src/librustc_data_structures/lib.rs
src/librustc_mir/lib.rs
src/libstd/lib.rs
src/libstd/num.rs
src/test/run-pass/ctfe/tuple-struct-constructors.rs
src/test/run-pass/enum-null-pointer-opt.rs
src/test/ui/print_type_sizes/niche-filling.rs

index bb78c14b90586fcb65a93559491662489b83813e..f7dd9d4f010a4235162b6239e93c893314f8fc14 100644 (file)
 #![feature(lang_items)]
 #![feature(libc)]
 #![feature(needs_allocator)]
-#![feature(nonzero)]
 #![feature(optin_builtin_traits)]
 #![feature(pattern)]
 #![feature(pin)]
index 54f35d17974fb2412db5e25bdcb802d350ef96e6..06fbfcecba801d31c2a2c3a34ef12ae10e433884 100644 (file)
 
 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;
 
index 19836d98844e292d7be235601e344664faac182f..ee5230cef8dd9597d89809ec0451bd0397d78fd6 100644 (file)
 // 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<T: ?Sized> 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<T: Zeroable>(pub(crate) T);
-
-impl<T: Zeroable> NonZero<T> {
-    /// 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<Self> {
-        if inner.is_zero() {
-            None
-        } else {
-            Some(NonZero(inner))
-        }
-    }
-
-    /// Gets the inner value.
-    pub fn get(self) -> T {
-        self.0
-    }
-}
-
-impl<T: Zeroable+CoerceUnsized<U>, U: Zeroable> CoerceUnsized<NonZero<U>> for NonZero<T> {}
-
-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<T>(pub(crate) T);
 
-impl<'a, T: ?Sized> From<&'a T> for NonZero<*const T> {
-    fn from(reference: &'a T) -> Self {
-        NonZero(reference)
-    }
-}
+impl<T: CoerceUnsized<U>, U> CoerceUnsized<NonZero<U>> for NonZero<T> {}
index a1168d74e4b52a2ff02b78eda6da1a5414f5e8a3..6df8ca98ba92c45976fa477b31ae53660891a9d3 100644 (file)
 use fmt;
 use intrinsics;
 use mem;
-#[allow(deprecated)] use nonzero::NonZero;
+use nonzero::NonZero;
 use ops;
 use str::FromStr;
 
 macro_rules! impl_nonzero_fmt {
-    ( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
+    ( ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
         $(
-            #[$stability]
-            #[allow(deprecated)]
+            #[stable(feature = "nonzero", since = "1.28.0")]
             impl fmt::$Trait for $Ty {
                 #[inline]
                 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -36,7 +35,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 }
 
 macro_rules! nonzero_integers {
-    ( #[$stability: meta] #[$deprecation: meta] $( $Ty: ident($Int: ty); )+ ) => {
+    ( $( $Ty: ident($Int: ty); )+ ) => {
         $(
             /// An integer that is known not to equal zero.
             ///
@@ -47,27 +46,24 @@ macro_rules! nonzero_integers {
             /// use std::mem::size_of;
             /// assert_eq!(size_of::<Option<std::num::NonZeroU32>>(), size_of::<u32>());
             /// ```
-            #[$stability]
-            #[$deprecation]
-            #[allow(deprecated)]
+            #[stable(feature = "nonzero", since = "1.28.0")]
             #[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.
                 ///
                 /// # 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<Self> {
                     if n != 0 {
@@ -78,7 +74,7 @@ pub fn new(n: $Int) -> Option<Self> {
                 }
 
                 /// Returns the value as a primitive type.
-                #[$stability]
+                #[stable(feature = "nonzero", since = "1.28.0")]
                 #[inline]
                 pub fn get(self) -> $Int {
                     self.0 .0
@@ -87,7 +83,6 @@ pub fn get(self) -> $Int {
             }
 
             impl_nonzero_fmt! {
-                #[$stability]
                 (Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
             }
         )+
@@ -95,8 +90,6 @@ pub fn get(self) -> $Int {
 }
 
 nonzero_integers! {
-    #[unstable(feature = "nonzero", issue = "49137")]
-    #[allow(deprecated)]  // Redundant, works around "error: inconsistent lockstep iteration"
     NonZeroU8(u8);
     NonZeroU16(u16);
     NonZeroU32(u32);
@@ -105,19 +98,6 @@ pub fn get(self) -> $Int {
     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,
index 83dfac7a3a2ea4f09b5e25aeb4033acc751c40d9..63bcc024020155fe753716f68dc2d6f1e2e2c046 100644 (file)
@@ -23,7 +23,7 @@
 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 @@ fn ge(&self, other: &*mut T) -> bool { *self >= *other }
 #[unstable(feature = "ptr_internals", issue = "0",
            reason = "use NonNull instead and consider PhantomData<T> \
                      (if you also use #[may_dangle]), Send, and/or Sync")]
-#[allow(deprecated)]
 #[doc(hidden)]
 pub struct Unique<T: ?Sized> {
     pointer: NonZero<*const T>,
@@ -2790,7 +2789,6 @@ pub const fn empty() -> Self {
 }
 
 #[unstable(feature = "ptr_internals", issue = "0")]
-#[allow(deprecated)]
 impl<T: ?Sized> Unique<T> {
     /// Creates a new `Unique`.
     ///
@@ -2855,7 +2853,6 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 }
 
 #[unstable(feature = "ptr_internals", issue = "0")]
-#[allow(deprecated)]
 impl<'a, T: ?Sized> From<&'a mut T> for Unique<T> {
     fn from(reference: &'a mut T) -> Self {
         Unique { pointer: NonZero(reference as _), _marker: PhantomData }
@@ -2863,7 +2860,6 @@ fn from(reference: &'a mut T) -> Self {
 }
 
 #[unstable(feature = "ptr_internals", issue = "0")]
-#[allow(deprecated)]
 impl<'a, T: ?Sized> From<&'a T> for Unique<T> {
     fn from(reference: &'a T) -> Self {
         Unique { pointer: NonZero(reference as _), _marker: PhantomData }
@@ -2896,7 +2892,7 @@ fn from(p: NonNull<T>) -> Self {
 /// provide a public API that follows the normal shared XOR mutable rules of Rust.
 #[stable(feature = "nonnull", since = "1.25.0")]
 pub struct NonNull<T: ?Sized> {
-    #[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 @@ pub fn dangling() -> Self {
     }
 }
 
-#[allow(deprecated)]
 impl<T: ?Sized> NonNull<T> {
     /// Creates a new `NonNull`.
     ///
@@ -3054,7 +3049,6 @@ fn from(unique: Unique<T>) -> Self {
 }
 
 #[stable(feature = "nonnull", since = "1.25.0")]
-#[allow(deprecated)]
 impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T> {
     fn from(reference: &'a mut T) -> Self {
         NonNull { pointer: NonZero(reference as _) }
@@ -3062,7 +3056,6 @@ fn from(reference: &'a mut T) -> Self {
 }
 
 #[stable(feature = "nonnull", since = "1.25.0")]
-#[allow(deprecated)]
 impl<'a, T: ?Sized> From<&'a T> for NonNull<T> {
     fn from(reference: &'a T) -> Self {
         NonNull { pointer: NonZero(reference as _) }
index 5e98e40e0d5e6b42f2e431152dd9bd59f89a3e84..7fb4b503c01efea48e8acc5e46bb5efc8c356486 100644 (file)
@@ -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)]
index 26ac9d6ee9ea6798f8cd99d63c62250f018ec84a..ac6ff6831adb804ceb61a80d6a0cbf16abfb13d3 100644 (file)
@@ -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)]
index 5bac1bd9a7b08986b8c6231a77613caf4ee4995c..9a6705fe9cac32b6cad2d276f672abef2e5b6d45 100644 (file)
@@ -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)]
index fbc0facbc49670ca94fedbeef383d422780c83be..ecced1b81682e94d365b34389a4234a4ff868700 100644 (file)
@@ -30,7 +30,6 @@
 #![feature(exhaustive_patterns)]
 #![feature(range_contains)]
 #![feature(rustc_diagnostic_macros)]
-#![feature(nonzero)]
 #![feature(inclusive_range_methods)]
 #![feature(crate_visibility_modifier)]
 #![feature(never_type)]
index d41739ab02c6ad90c19d55dbe7fe0d644a1dbb56..9cdc6a21622467af00052f3b006b99c20b71e7a9 100644 (file)
 #![feature(needs_panic_runtime)]
 #![feature(never_type)]
 #![feature(exhaustive_patterns)]
-#![feature(nonzero)]
 #![feature(num_bits_bytes)]
 #![feature(old_wrapping)]
 #![feature(on_unimplemented)]
index 4b975dd912a1ae0b9c9b940c215a66ad8c0137b3..3f90c1fa3b1f2cf39c1d60b0f4ec1d32e9d5a940 100644 (file)
 #[stable(feature = "rust1", since = "1.0.0")]
 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,
-};
+#[stable(feature = "nonzero", since = "1.28.0")]
+pub use core::num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize};
 
 #[cfg(test)] use fmt;
 #[cfg(test)] use ops::{Add, Sub, Mul, Div, Rem};
index ecc5d3766367949db7f4cc6907fa87b95437cf34..d5f3e88fd52e86f31301503ba0004ee8d0cb9379 100644 (file)
 
 // https://github.com/rust-lang/rust/issues/41898
 
-#![feature(nonzero, const_fn)]
-extern crate core;
-use core::nonzero::NonZero;
+use std::num::NonZeroU64;
 
 fn main() {
-    const FOO: NonZero<u64> = unsafe { NonZero::new_unchecked(2) };
+    const FOO: NonZeroU64 = unsafe { NonZeroU64::new_unchecked(2) };
     if let FOO = FOO {}
 }
index 12f17a1575e8211a09ca43a4ec28e96fad193b49..34ed589d418c16e570d9adadd77b03da8f5e623a 100644 (file)
@@ -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;
index 1aad0b760b17bf2c9484d3fdd9d9f446a49760c6..17e7a21cd02e52ca0305dcae1fdb4dba101fd05e 100644 (file)
@@ -22,7 +22,6 @@
 // padding and overall computed sizes can be quite different.
 
 #![feature(start)]
-#![feature(nonzero)]
 #![allow(dead_code)]
 
 use std::num::NonZeroU32;