about summary refs log tree commit diff
path: root/library/core/src
diff options
context:
space:
mode:
authorDeadbeef <ent3rm4n@gmail.com>2023-04-17 08:22:16 +0000
committerDeadbeef <ent3rm4n@gmail.com>2023-04-17 09:27:07 +0000
commitdd025c3b562bf00aae462b0c77b7d2ba1f8fad93 (patch)
tree7133c2d1e47b6acc90ccce6443a8d8e2701d31be /library/core/src
parent99851c48cf0f1942d9c4d0c22df1d83fc3a61814 (diff)
downloadrust-dd025c3b562bf00aae462b0c77b7d2ba1f8fad93.tar.gz
rust-dd025c3b562bf00aae462b0c77b7d2ba1f8fad93.zip
fix codegen difference
Diffstat (limited to 'library/core/src')
-rw-r--r--library/core/src/num/int_macros.rs6
-rw-r--r--library/core/src/num/mod.rs18
-rw-r--r--library/core/src/num/uint_macros.rs6
3 files changed, 22 insertions, 8 deletions
diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs
index 1e82d4d1ff0..17715c9291f 100644
--- a/library/core/src/num/int_macros.rs
+++ b/library/core/src/num/int_macros.rs
@@ -785,8 +785,7 @@ macro_rules! int_impl {
             // SAFETY: the caller must uphold the safety contract for
             // `unchecked_shl`.
             // Any legal shift amount is losslessly representable in the self type.
-            // FIXME(const-hack) replace with `.try_into().ok().unwrap_unchecked()`.
-            unsafe { intrinsics::unchecked_shl(self, rhs as _) }
+            unsafe { intrinsics::unchecked_shl(self, conv_rhs_for_unchecked_shift!($SelfT, rhs)) }
         }
 
         /// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
@@ -834,8 +833,7 @@ macro_rules! int_impl {
             // SAFETY: the caller must uphold the safety contract for
             // `unchecked_shr`.
             // Any legal shift amount is losslessly representable in the self type.
-            // FIXME(const-hack) replace with `.try_into().ok().unwrap_unchecked()`.
-            unsafe { intrinsics::unchecked_shr(self, rhs as _) }
+            unsafe { intrinsics::unchecked_shr(self, conv_rhs_for_unchecked_shift!($SelfT, rhs)) }
         }
 
         /// Checked absolute value. Computes `self.abs()`, returning `None` if
diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs
index b0488dc069b..fdd7be625ed 100644
--- a/library/core/src/num/mod.rs
+++ b/library/core/src/num/mod.rs
@@ -3,6 +3,7 @@
 #![stable(feature = "rust1", since = "1.0.0")]
 
 use crate::ascii;
+use crate::convert::TryInto;
 use crate::intrinsics;
 use crate::mem;
 use crate::ops::{Add, Mul, Sub};
@@ -224,6 +225,23 @@ macro_rules! widening_impl {
     };
 }
 
+macro_rules! conv_rhs_for_unchecked_shift {
+    ($SelfT:ty, $x:expr) => {{
+        #[inline]
+        fn conv(x: u32) -> $SelfT {
+            // FIXME(const-hack) replace with `.try_into().ok().unwrap_unchecked()`.
+            // SAFETY: Any legal shift amount must be losslessly representable in the self type.
+            unsafe { x.try_into().ok().unwrap_unchecked() }
+        }
+        #[inline]
+        const fn const_conv(x: u32) -> $SelfT {
+            x as _
+        }
+
+        intrinsics::const_eval_select(($x,), const_conv, conv)
+    }};
+}
+
 impl i8 {
     int_impl! {
         Self = i8,
diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs
index 795645b8b7b..6f6b6dbb80b 100644
--- a/library/core/src/num/uint_macros.rs
+++ b/library/core/src/num/uint_macros.rs
@@ -939,8 +939,7 @@ macro_rules! uint_impl {
             // SAFETY: the caller must uphold the safety contract for
             // `unchecked_shl`.
             // Any legal shift amount is losslessly representable in the self type.
-            // FIXME(const-hack) replace with `.try_into().ok().unwrap_unchecked()`.
-            unsafe { intrinsics::unchecked_shl(self, rhs as _) }
+            unsafe { intrinsics::unchecked_shl(self, conv_rhs_for_unchecked_shift!($SelfT, rhs)) }
         }
 
         /// Checked shift right. Computes `self >> rhs`, returning `None`
@@ -988,8 +987,7 @@ macro_rules! uint_impl {
             // SAFETY: the caller must uphold the safety contract for
             // `unchecked_shr`.
             // Any legal shift amount is losslessly representable in the self type.
-            // FIXME(const-hack) replace with `.try_into().ok().unwrap_unchecked()`.
-            unsafe { intrinsics::unchecked_shr(self, rhs as _) }
+            unsafe { intrinsics::unchecked_shr(self, conv_rhs_for_unchecked_shift!($SelfT, rhs)) }
         }
 
         /// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if