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