Skip to content

Commit 2a98db7

Browse files
committed
unchecked_{shl|shr} should use u32 as the RHS
1 parent ac8c02f commit 2a98db7

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
@@ -130,6 +130,7 @@
130130
#![feature(const_pin)]
131131
#![feature(const_ptr_sub_ptr)]
132132
#![feature(const_replace)]
133+
#![feature(const_result_drop)]
133134
#![feature(const_ptr_as_ref)]
134135
#![feature(const_ptr_is_null)]
135136
#![feature(const_ptr_read)]

core/src/num/int_macros.rs

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

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

813815
/// Checked absolute value. Computes `self.abs()`, returning `None` if
@@ -1354,11 +1356,12 @@ macro_rules! int_impl {
13541356
#[must_use = "this returns the result of the operation, \
13551357
without modifying the original"]
13561358
#[inline(always)]
1359+
#[rustc_allow_const_fn_unstable(const_inherent_unchecked_arith)]
13571360
pub const fn wrapping_shl(self, rhs: u32) -> Self {
13581361
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
13591362
// out of bounds
13601363
unsafe {
1361-
intrinsics::unchecked_shl(self, (rhs & ($BITS - 1)) as $SelfT)
1364+
self.unchecked_shl(rhs & ($BITS - 1))
13621365
}
13631366
}
13641367

@@ -1383,11 +1386,12 @@ macro_rules! int_impl {
13831386
#[must_use = "this returns the result of the operation, \
13841387
without modifying the original"]
13851388
#[inline(always)]
1389+
#[rustc_allow_const_fn_unstable(const_inherent_unchecked_arith)]
13861390
pub const fn wrapping_shr(self, rhs: u32) -> Self {
13871391
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
13881392
// out of bounds
13891393
unsafe {
1390-
intrinsics::unchecked_shr(self, (rhs & ($BITS - 1)) as $SelfT)
1394+
self.unchecked_shr(rhs & ($BITS - 1))
13911395
}
13921396
}
13931397

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
@@ -901,10 +901,11 @@ macro_rules! uint_impl {
901901
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
902902
#[inline(always)]
903903
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
904-
pub const unsafe fn unchecked_shl(self, rhs: Self) -> Self {
904+
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
905905
// SAFETY: the caller must uphold the safety contract for
906906
// `unchecked_shl`.
907-
unsafe { intrinsics::unchecked_shl(self, rhs) }
907+
// Any legal shift amount is losslessly representable in the self type.
908+
unsafe { intrinsics::unchecked_shl(self, rhs.try_into().ok().unwrap_unchecked()) }
908909
}
909910

910911
/// Checked shift right. Computes `self >> rhs`, returning `None`
@@ -948,10 +949,11 @@ macro_rules! uint_impl {
948949
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
949950
#[inline(always)]
950951
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
951-
pub const unsafe fn unchecked_shr(self, rhs: Self) -> Self {
952+
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
952953
// SAFETY: the caller must uphold the safety contract for
953954
// `unchecked_shr`.
954-
unsafe { intrinsics::unchecked_shr(self, rhs) }
955+
// Any legal shift amount is losslessly representable in the self type.
956+
unsafe { intrinsics::unchecked_shr(self, rhs.try_into().ok().unwrap_unchecked()) }
955957
}
956958

957959
/// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if
@@ -1367,11 +1369,12 @@ macro_rules! uint_impl {
13671369
#[must_use = "this returns the result of the operation, \
13681370
without modifying the original"]
13691371
#[inline(always)]
1372+
#[rustc_allow_const_fn_unstable(const_inherent_unchecked_arith)]
13701373
pub const fn wrapping_shl(self, rhs: u32) -> Self {
13711374
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
13721375
// out of bounds
13731376
unsafe {
1374-
intrinsics::unchecked_shl(self, (rhs & ($BITS - 1)) as $SelfT)
1377+
self.unchecked_shl(rhs & ($BITS - 1))
13751378
}
13761379
}
13771380

@@ -1399,11 +1402,12 @@ macro_rules! uint_impl {
13991402
#[must_use = "this returns the result of the operation, \
14001403
without modifying the original"]
14011404
#[inline(always)]
1405+
#[rustc_allow_const_fn_unstable(const_inherent_unchecked_arith)]
14021406
pub const fn wrapping_shr(self, rhs: u32) -> Self {
14031407
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
14041408
// out of bounds
14051409
unsafe {
1406-
intrinsics::unchecked_shr(self, (rhs & ($BITS - 1)) as $SelfT)
1410+
self.unchecked_shr(rhs & ($BITS - 1))
14071411
}
14081412
}
14091413

0 commit comments

Comments
 (0)