@@ -757,10 +757,11 @@ macro_rules! int_impl {
757
757
#[ rustc_const_unstable( feature = "const_inherent_unchecked_arith" , issue = "85122" ) ]
758
758
#[ inline( always) ]
759
759
#[ 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 {
761
761
// SAFETY: the caller must uphold the safety contract for
762
762
// `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( ) ) }
764
765
}
765
766
766
767
/// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
@@ -804,10 +805,11 @@ macro_rules! int_impl {
804
805
#[ rustc_const_unstable( feature = "const_inherent_unchecked_arith" , issue = "85122" ) ]
805
806
#[ inline( always) ]
806
807
#[ 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 {
808
809
// SAFETY: the caller must uphold the safety contract for
809
810
// `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( ) ) }
811
813
}
812
814
813
815
/// Checked absolute value. Computes `self.abs()`, returning `None` if
@@ -1354,11 +1356,12 @@ macro_rules! int_impl {
1354
1356
#[ must_use = "this returns the result of the operation, \
1355
1357
without modifying the original"]
1356
1358
#[ inline( always) ]
1359
+ #[ rustc_allow_const_fn_unstable( const_inherent_unchecked_arith) ]
1357
1360
pub const fn wrapping_shl( self , rhs: u32 ) -> Self {
1358
1361
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
1359
1362
// out of bounds
1360
1363
unsafe {
1361
- intrinsics :: unchecked_shl( self , ( rhs & ( $BITS - 1 ) ) as $SelfT )
1364
+ self . unchecked_shl( rhs & ( $BITS - 1 ) )
1362
1365
}
1363
1366
}
1364
1367
@@ -1383,11 +1386,12 @@ macro_rules! int_impl {
1383
1386
#[ must_use = "this returns the result of the operation, \
1384
1387
without modifying the original"]
1385
1388
#[ inline( always) ]
1389
+ #[ rustc_allow_const_fn_unstable( const_inherent_unchecked_arith) ]
1386
1390
pub const fn wrapping_shr( self , rhs: u32 ) -> Self {
1387
1391
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
1388
1392
// out of bounds
1389
1393
unsafe {
1390
- intrinsics :: unchecked_shr( self , ( rhs & ( $BITS - 1 ) ) as $SelfT )
1394
+ self . unchecked_shr( rhs & ( $BITS - 1 ) )
1391
1395
}
1392
1396
}
1393
1397
0 commit comments