diff options
| author | bors <bors@rust-lang.org> | 2023-06-19 04:48:35 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-06-19 04:48:35 +0000 |
| commit | 8d1fa473dddd12efb2430302e5f5dfcc3eb73f8b (patch) | |
| tree | 13667f96de6a9b4b688b2f98f67cd8425d81cd7c /library/core/src | |
| parent | c911e085144324110b4b51b333e71444861b0d17 (diff) | |
| parent | 3ec4eeddefcf022af3aafab708d36830f40d8a47 (diff) | |
| download | rust-8d1fa473dddd12efb2430302e5f5dfcc3eb73f8b.tar.gz rust-8d1fa473dddd12efb2430302e5f5dfcc3eb73f8b.zip | |
Auto merge of #112724 - scottmcm:simpler-unchecked-shifts, r=Mark-Simulacrum
[libs] Simplify `unchecked_{shl,shr}`
There's no need for the `const_eval_select` dance here. And while I originally wrote the `.try_into().unwrap_unchecked()` implementation here, it's kinda a mess in MIR -- this new one is substantially simpler, as shown by the old one being above the inlining threshold but the new one being below it in the `mir-opt/inline/unchecked_shifts` tests.
We don't need `u32::checked_shl` doing a dance through both `Result` *and* `Option` 🙃
Diffstat (limited to 'library/core/src')
| -rw-r--r-- | library/core/src/num/mod.rs | 17 |
1 files changed, 5 insertions, 12 deletions
diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs index c9baa09f407..95dcaf5dd73 100644 --- a/library/core/src/num/mod.rs +++ b/library/core/src/num/mod.rs @@ -3,7 +3,6 @@ #![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}; @@ -278,18 +277,12 @@ 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 _ + // If the `as` cast will truncate, ensure we still tell the backend + // that the pre-truncation value was also small. + if <$SelfT>::BITS < 32 { + intrinsics::assume($x <= (<$SelfT>::MAX as u32)); } - - intrinsics::const_eval_select(($x,), const_conv, conv) + $x as $SelfT }}; } |
