From dd025c3b562bf00aae462b0c77b7d2ba1f8fad93 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Mon, 17 Apr 2023 08:22:16 +0000 Subject: fix codegen difference --- library/core/src/num/int_macros.rs | 6 ++---- library/core/src/num/mod.rs | 18 ++++++++++++++++++ library/core/src/num/uint_macros.rs | 6 ++---- 3 files changed, 22 insertions(+), 8 deletions(-) (limited to 'library/core/src/num') 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 -- cgit 1.4.1-3-g733a5