@@ -761,10 +761,11 @@ macro_rules! int_impl {
761
761
#[ rustc_const_unstable( feature = "const_inherent_unchecked_arith" , issue = "85122" ) ]
762
762
#[ inline( always) ]
763
763
#[ 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 {
765
765
// SAFETY: the caller must uphold the safety contract for
766
766
// `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( ) ) }
768
769
}
769
770
770
771
/// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
@@ -808,10 +809,11 @@ macro_rules! int_impl {
808
809
#[ rustc_const_unstable( feature = "const_inherent_unchecked_arith" , issue = "85122" ) ]
809
810
#[ inline( always) ]
810
811
#[ 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 {
812
813
// SAFETY: the caller must uphold the safety contract for
813
814
// `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( ) ) }
815
817
}
816
818
817
819
/// Checked absolute value. Computes `self.abs()`, returning `None` if
@@ -1358,11 +1360,12 @@ macro_rules! int_impl {
1358
1360
#[ must_use = "this returns the result of the operation, \
1359
1361
without modifying the original"]
1360
1362
#[ inline( always) ]
1363
+ #[ rustc_allow_const_fn_unstable( const_inherent_unchecked_arith) ]
1361
1364
pub const fn wrapping_shl( self , rhs: u32 ) -> Self {
1362
1365
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
1363
1366
// out of bounds
1364
1367
unsafe {
1365
- intrinsics :: unchecked_shl( self , ( rhs & ( $BITS - 1 ) ) as $SelfT )
1368
+ self . unchecked_shl( rhs & ( $BITS - 1 ) )
1366
1369
}
1367
1370
}
1368
1371
@@ -1387,11 +1390,12 @@ macro_rules! int_impl {
1387
1390
#[ must_use = "this returns the result of the operation, \
1388
1391
without modifying the original"]
1389
1392
#[ inline( always) ]
1393
+ #[ rustc_allow_const_fn_unstable( const_inherent_unchecked_arith) ]
1390
1394
pub const fn wrapping_shr( self , rhs: u32 ) -> Self {
1391
1395
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
1392
1396
// out of bounds
1393
1397
unsafe {
1394
- intrinsics :: unchecked_shr( self , ( rhs & ( $BITS - 1 ) ) as $SelfT )
1398
+ self . unchecked_shr( rhs & ( $BITS - 1 ) )
1395
1399
}
1396
1400
}
1397
1401
0 commit comments