Skip to content

Commit c3f7048

Browse files
authored
Rollup merge of rust-lang#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 rust-lang#85122, the `unchecked_math` tracking issue
2 parents 80d599c + 2a98db7 commit c3f7048

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
#![feature(const_pin)]
132132
#![feature(const_ptr_sub_ptr)]
133133
#![feature(const_replace)]
134+
#![feature(const_result_drop)]
134135
#![feature(const_ptr_as_ref)]
135136
#![feature(const_ptr_is_null)]
136137
#![feature(const_ptr_read)]

core/src/num/int_macros.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -761,10 +761,11 @@ macro_rules! int_impl {
761761
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
762762
#[inline(always)]
763763
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
764-
pub const unsafe fn unchecked_shl(self, rhs: Self) -> Self {
764+
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
765765
// SAFETY: the caller must uphold the safety contract for
766766
// `unchecked_shl`.
767-
unsafe { intrinsics::unchecked_shl(self, rhs) }
767+
// Any legal shift amount is losslessly representable in the self type.
768+
unsafe { intrinsics::unchecked_shl(self, rhs.try_into().ok().unwrap_unchecked()) }
768769
}
769770

770771
/// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
@@ -808,10 +809,11 @@ macro_rules! int_impl {
808809
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
809810
#[inline(always)]
810811
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
811-
pub const unsafe fn unchecked_shr(self, rhs: Self) -> Self {
812+
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
812813
// SAFETY: the caller must uphold the safety contract for
813814
// `unchecked_shr`.
814-
unsafe { intrinsics::unchecked_shr(self, rhs) }
815+
// Any legal shift amount is losslessly representable in the self type.
816+
unsafe { intrinsics::unchecked_shr(self, rhs.try_into().ok().unwrap_unchecked()) }
815817
}
816818

817819
/// Checked absolute value. Computes `self.abs()`, returning `None` if
@@ -1358,11 +1360,12 @@ macro_rules! int_impl {
13581360
#[must_use = "this returns the result of the operation, \
13591361
without modifying the original"]
13601362
#[inline(always)]
1363+
#[rustc_allow_const_fn_unstable(const_inherent_unchecked_arith)]
13611364
pub const fn wrapping_shl(self, rhs: u32) -> Self {
13621365
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
13631366
// out of bounds
13641367
unsafe {
1365-
intrinsics::unchecked_shl(self, (rhs & ($BITS - 1)) as $SelfT)
1368+
self.unchecked_shl(rhs & ($BITS - 1))
13661369
}
13671370
}
13681371

@@ -1387,11 +1390,12 @@ macro_rules! int_impl {
13871390
#[must_use = "this returns the result of the operation, \
13881391
without modifying the original"]
13891392
#[inline(always)]
1393+
#[rustc_allow_const_fn_unstable(const_inherent_unchecked_arith)]
13901394
pub const fn wrapping_shr(self, rhs: u32) -> Self {
13911395
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
13921396
// out of bounds
13931397
unsafe {
1394-
intrinsics::unchecked_shr(self, (rhs & ($BITS - 1)) as $SelfT)
1398+
self.unchecked_shr(rhs & ($BITS - 1))
13951399
}
13961400
}
13971401

core/src/num/mod.rs

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

55
use crate::ascii;
6+
use crate::convert::TryInto;
67
use crate::error::Error;
78
use crate::intrinsics;
89
use crate::mem;

core/src/num/uint_macros.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -908,10 +908,11 @@ macro_rules! uint_impl {
908908
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
909909
#[inline(always)]
910910
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
911-
pub const unsafe fn unchecked_shl(self, rhs: Self) -> Self {
911+
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
912912
// SAFETY: the caller must uphold the safety contract for
913913
// `unchecked_shl`.
914-
unsafe { intrinsics::unchecked_shl(self, rhs) }
914+
// Any legal shift amount is losslessly representable in the self type.
915+
unsafe { intrinsics::unchecked_shl(self, rhs.try_into().ok().unwrap_unchecked()) }
915916
}
916917

917918
/// Checked shift right. Computes `self >> rhs`, returning `None`
@@ -955,10 +956,11 @@ macro_rules! uint_impl {
955956
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
956957
#[inline(always)]
957958
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
958-
pub const unsafe fn unchecked_shr(self, rhs: Self) -> Self {
959+
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
959960
// SAFETY: the caller must uphold the safety contract for
960961
// `unchecked_shr`.
961-
unsafe { intrinsics::unchecked_shr(self, rhs) }
962+
// Any legal shift amount is losslessly representable in the self type.
963+
unsafe { intrinsics::unchecked_shr(self, rhs.try_into().ok().unwrap_unchecked()) }
962964
}
963965

964966
/// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if
@@ -1374,11 +1376,12 @@ macro_rules! uint_impl {
13741376
#[must_use = "this returns the result of the operation, \
13751377
without modifying the original"]
13761378
#[inline(always)]
1379+
#[rustc_allow_const_fn_unstable(const_inherent_unchecked_arith)]
13771380
pub const fn wrapping_shl(self, rhs: u32) -> Self {
13781381
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
13791382
// out of bounds
13801383
unsafe {
1381-
intrinsics::unchecked_shl(self, (rhs & ($BITS - 1)) as $SelfT)
1384+
self.unchecked_shl(rhs & ($BITS - 1))
13821385
}
13831386
}
13841387

@@ -1406,11 +1409,12 @@ macro_rules! uint_impl {
14061409
#[must_use = "this returns the result of the operation, \
14071410
without modifying the original"]
14081411
#[inline(always)]
1412+
#[rustc_allow_const_fn_unstable(const_inherent_unchecked_arith)]
14091413
pub const fn wrapping_shr(self, rhs: u32) -> Self {
14101414
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
14111415
// out of bounds
14121416
unsafe {
1413-
intrinsics::unchecked_shr(self, (rhs & ($BITS - 1)) as $SelfT)
1417+
self.unchecked_shr(rhs & ($BITS - 1))
14141418
}
14151419
}
14161420

0 commit comments

Comments
 (0)