about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2021-10-21 14:11:10 +0900
committerGitHub <noreply@github.com>2021-10-21 14:11:10 +0900
commite4cfaa1a7ed336311e281ff188e704d7ef60b1bf (patch)
tree4a1f89b3d93090d2e8adf7169c9c886fda8c2f78
parent371fd4f1c05bd0cf2cd6c4912e0cc83460075370 (diff)
parent2fc780638e069155c87555da10ff234d1018fcaa (diff)
downloadrust-e4cfaa1a7ed336311e281ff188e704d7ef60b1bf.tar.gz
rust-e4cfaa1a7ed336311e281ff188e704d7ef60b1bf.zip
Rollup merge of #90077 - woppopo:const_nonzero_from, r=oli-obk
Make `From` impls of NonZero integer const.

I also changed the feature gate added to `From` impls of Atomic integer to `const_num_from_num` from `const_convert`.

Tracking issue: #87852
-rw-r--r--library/core/src/convert/num.rs5
-rw-r--r--library/core/src/lib.rs1
-rw-r--r--library/core/src/num/nonzero.rs2
-rw-r--r--library/core/src/sync/atomic.rs2
-rw-r--r--library/core/tests/nonzero.rs11
5 files changed, 13 insertions, 8 deletions
diff --git a/library/core/src/convert/num.rs b/library/core/src/convert/num.rs
index 75ef873abc9..2b6ea90bf04 100644
--- a/library/core/src/convert/num.rs
+++ b/library/core/src/convert/num.rs
@@ -390,7 +390,8 @@ use crate::num::NonZeroUsize;
 macro_rules! nzint_impl_from {
     ($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
         #[$attr]
-        impl From<$Small> for $Large {
+        #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
+        impl const From<$Small> for $Large {
             // Rustdocs on the impl block show a "[+] show undocumented items" toggle.
             // Rustdocs on functions do not.
             #[doc = $doc]
@@ -398,7 +399,7 @@ macro_rules! nzint_impl_from {
             fn from(small: $Small) -> Self {
                 // SAFETY: input type guarantees the value is non-zero
                 unsafe {
-                    Self::new_unchecked(small.get().into())
+                    Self::new_unchecked(From::from(small.get()))
                 }
             }
         }
diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs
index 13b80c05dbb..58a170401e7 100644
--- a/library/core/src/lib.rs
+++ b/library/core/src/lib.rs
@@ -115,6 +115,7 @@
 #![feature(const_likely)]
 #![feature(const_maybe_uninit_as_ptr)]
 #![feature(const_maybe_uninit_assume_init)]
+#![feature(const_num_from_num)]
 #![feature(const_option)]
 #![feature(const_pin)]
 #![feature(const_replace)]
diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs
index d28474c2923..9b1a4de5d80 100644
--- a/library/core/src/num/nonzero.rs
+++ b/library/core/src/num/nonzero.rs
@@ -82,7 +82,7 @@ macro_rules! nonzero_integers {
             }
 
             #[stable(feature = "from_nonzero", since = "1.31.0")]
-            #[rustc_const_unstable(feature = "const_convert", issue = "88674")]
+            #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
             impl const From<$Ty> for $Int {
                 #[doc = concat!("Converts a `", stringify!($Ty), "` into an `", stringify!($Int), "`")]
                 #[inline]
diff --git a/library/core/src/sync/atomic.rs b/library/core/src/sync/atomic.rs
index 0915dcffe6e..1dd3b2d8e3c 100644
--- a/library/core/src/sync/atomic.rs
+++ b/library/core/src/sync/atomic.rs
@@ -1365,7 +1365,7 @@ macro_rules! atomic_int {
         }
 
         #[$stable_from]
-        #[rustc_const_unstable(feature = "const_convert", issue = "88674")]
+        #[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
         impl const From<$int_type> for $atomic_type {
             #[doc = concat!("Converts an `", stringify!($int_type), "` into an `", stringify!($atomic_type), "`.")]
             #[inline]
diff --git a/library/core/tests/nonzero.rs b/library/core/tests/nonzero.rs
index 4817d86ca6e..a0ca919a851 100644
--- a/library/core/tests/nonzero.rs
+++ b/library/core/tests/nonzero.rs
@@ -204,9 +204,9 @@ fn nonzero_const() {
     // test that the methods of `NonZeroX>` are usable in a const context
     // Note: only tests NonZero8
 
-    const NONZERO: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(5) };
+    const NONZERO_U8: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(5) };
 
-    const GET: u8 = NONZERO.get();
+    const GET: u8 = NONZERO_U8.get();
     assert_eq!(GET, 5);
 
     const ZERO: Option<NonZeroU8> = NonZeroU8::new(0);
@@ -215,8 +215,11 @@ fn nonzero_const() {
     const ONE: Option<NonZeroU8> = NonZeroU8::new(1);
     assert!(ONE.is_some());
 
-    const FROM_NONZERO: u8 = u8::from(NONZERO);
-    assert_eq!(FROM_NONZERO, 5);
+    const FROM_NONZERO_U8: u8 = u8::from(NONZERO_U8);
+    assert_eq!(FROM_NONZERO_U8, 5);
+
+    const NONZERO_CONVERT: NonZeroU32 = NonZeroU32::from(NONZERO_U8);
+    assert_eq!(NONZERO_CONVERT.get(), 5);
 }
 
 #[test]