@@ -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
0 commit comments