Rollup merge of #103456 - scottmcm:fix-unchecked-shifts, r=scottmcm
`unchecked_{shl|shr}` should use `u32` as the RHS The other shift methods, such as https://doc.rust-lang.org/nightly/std/primitive.u64.html#method.checked_shr and https://doc.rust-lang.org/nightly/std/primitive.i16.html#method.wrapping_shl, use `u32` for the shift amount. That's consistent with other things, like `count_ones`, which also always use `u32` for a bit count, regardless of the size of the type. This PR changes `unchecked_shl` and `unchecked_shr` to also use `u32` for the shift amount (rather than Self). cc #85122, the `unchecked_math` tracking issue
This commit is contained in:
commit
e2301154e3
5 changed files with 88 additions and 12 deletions
|
@ -761,10 +761,11 @@ macro_rules! int_impl {
|
|||
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
|
||||
#[inline(always)]
|
||||
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
|
||||
pub const unsafe fn unchecked_shl(self, rhs: Self) -> Self {
|
||||
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
|
||||
// SAFETY: the caller must uphold the safety contract for
|
||||
// `unchecked_shl`.
|
||||
unsafe { intrinsics::unchecked_shl(self, rhs) }
|
||||
// Any legal shift amount is losslessly representable in the self type.
|
||||
unsafe { intrinsics::unchecked_shl(self, rhs.try_into().ok().unwrap_unchecked()) }
|
||||
}
|
||||
|
||||
/// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
|
||||
|
@ -808,10 +809,11 @@ macro_rules! int_impl {
|
|||
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
|
||||
#[inline(always)]
|
||||
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
|
||||
pub const unsafe fn unchecked_shr(self, rhs: Self) -> Self {
|
||||
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
|
||||
// SAFETY: the caller must uphold the safety contract for
|
||||
// `unchecked_shr`.
|
||||
unsafe { intrinsics::unchecked_shr(self, rhs) }
|
||||
// Any legal shift amount is losslessly representable in the self type.
|
||||
unsafe { intrinsics::unchecked_shr(self, rhs.try_into().ok().unwrap_unchecked()) }
|
||||
}
|
||||
|
||||
/// Checked absolute value. Computes `self.abs()`, returning `None` if
|
||||
|
@ -1358,11 +1360,12 @@ macro_rules! int_impl {
|
|||
#[must_use = "this returns the result of the operation, \
|
||||
without modifying the original"]
|
||||
#[inline(always)]
|
||||
#[rustc_allow_const_fn_unstable(const_inherent_unchecked_arith)]
|
||||
pub const fn wrapping_shl(self, rhs: u32) -> Self {
|
||||
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
|
||||
// out of bounds
|
||||
unsafe {
|
||||
intrinsics::unchecked_shl(self, (rhs & ($BITS - 1)) as $SelfT)
|
||||
self.unchecked_shl(rhs & ($BITS - 1))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1387,11 +1390,12 @@ macro_rules! int_impl {
|
|||
#[must_use = "this returns the result of the operation, \
|
||||
without modifying the original"]
|
||||
#[inline(always)]
|
||||
#[rustc_allow_const_fn_unstable(const_inherent_unchecked_arith)]
|
||||
pub const fn wrapping_shr(self, rhs: u32) -> Self {
|
||||
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
|
||||
// out of bounds
|
||||
unsafe {
|
||||
intrinsics::unchecked_shr(self, (rhs & ($BITS - 1)) as $SelfT)
|
||||
self.unchecked_shr(rhs & ($BITS - 1))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue