Skip to content

Commit c40d80f

Browse files
committed
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` 🙃
2 parents c487615 + 5030f57 commit c40d80f

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

core/src/num/mod.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#![stable(feature = "rust1", since = "1.0.0")]
44

55
use crate::ascii;
6-
use crate::convert::TryInto;
76
use crate::intrinsics;
87
use crate::mem;
98
use crate::ops::{Add, Mul, Sub};
@@ -278,18 +277,12 @@ macro_rules! widening_impl {
278277

279278
macro_rules! conv_rhs_for_unchecked_shift {
280279
($SelfT:ty, $x:expr) => {{
281-
#[inline]
282-
fn conv(x: u32) -> $SelfT {
283-
// FIXME(const-hack) replace with `.try_into().ok().unwrap_unchecked()`.
284-
// SAFETY: Any legal shift amount must be losslessly representable in the self type.
285-
unsafe { x.try_into().ok().unwrap_unchecked() }
286-
}
287-
#[inline]
288-
const fn const_conv(x: u32) -> $SelfT {
289-
x as _
280+
// If the `as` cast will truncate, ensure we still tell the backend
281+
// that the pre-truncation value was also small.
282+
if <$SelfT>::BITS < 32 {
283+
intrinsics::assume($x <= (<$SelfT>::MAX as u32));
290284
}
291-
292-
intrinsics::const_eval_select(($x,), const_conv, conv)
285+
$x as $SelfT
293286
}};
294287
}
295288

0 commit comments

Comments
 (0)