File tree Expand file tree Collapse file tree 3 files changed +22
-8
lines changed
Expand file tree Collapse file tree 3 files changed +22
-8
lines changed Original file line number Diff line number Diff line change @@ -785,8 +785,7 @@ macro_rules! int_impl {
785785 // SAFETY: the caller must uphold the safety contract for
786786 // `unchecked_shl`.
787787 // Any legal shift amount is losslessly representable in the self type.
788- // FIXME(const-hack) replace with `.try_into().ok().unwrap_unchecked()`.
789- unsafe { intrinsics:: unchecked_shl( self , rhs as _) }
788+ unsafe { intrinsics:: unchecked_shl( self , conv_rhs_for_unchecked_shift!( $SelfT, rhs) ) }
790789 }
791790
792791 /// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
@@ -834,8 +833,7 @@ macro_rules! int_impl {
834833 // SAFETY: the caller must uphold the safety contract for
835834 // `unchecked_shr`.
836835 // Any legal shift amount is losslessly representable in the self type.
837- // FIXME(const-hack) replace with `.try_into().ok().unwrap_unchecked()`.
838- unsafe { intrinsics:: unchecked_shr( self , rhs as _) }
836+ unsafe { intrinsics:: unchecked_shr( self , conv_rhs_for_unchecked_shift!( $SelfT, rhs) ) }
839837 }
840838
841839 /// Checked absolute value. Computes `self.abs()`, returning `None` if
Original file line number Diff line number Diff line change 33#![ stable( feature = "rust1" , since = "1.0.0" ) ]
44
55use crate :: ascii;
6+ use crate :: convert:: TryInto ;
67use crate :: intrinsics;
78use crate :: mem;
89use crate :: ops:: { Add , Mul , Sub } ;
@@ -224,6 +225,23 @@ macro_rules! widening_impl {
224225 } ;
225226}
226227
228+ macro_rules! conv_rhs_for_unchecked_shift {
229+ ( $SelfT: ty, $x: expr) => { {
230+ #[ inline]
231+ fn conv( x: u32 ) -> $SelfT {
232+ // FIXME(const-hack) replace with `.try_into().ok().unwrap_unchecked()`.
233+ // SAFETY: Any legal shift amount must be losslessly representable in the self type.
234+ unsafe { x. try_into( ) . ok( ) . unwrap_unchecked( ) }
235+ }
236+ #[ inline]
237+ const fn const_conv( x: u32 ) -> $SelfT {
238+ x as _
239+ }
240+
241+ intrinsics:: const_eval_select( ( $x, ) , const_conv, conv)
242+ } } ;
243+ }
244+
227245impl i8 {
228246 int_impl ! {
229247 Self = i8 ,
Original file line number Diff line number Diff line change @@ -939,8 +939,7 @@ macro_rules! uint_impl {
939939 // SAFETY: the caller must uphold the safety contract for
940940 // `unchecked_shl`.
941941 // Any legal shift amount is losslessly representable in the self type.
942- // FIXME(const-hack) replace with `.try_into().ok().unwrap_unchecked()`.
943- unsafe { intrinsics:: unchecked_shl( self , rhs as _) }
942+ unsafe { intrinsics:: unchecked_shl( self , conv_rhs_for_unchecked_shift!( $SelfT, rhs) ) }
944943 }
945944
946945 /// Checked shift right. Computes `self >> rhs`, returning `None`
@@ -988,8 +987,7 @@ macro_rules! uint_impl {
988987 // SAFETY: the caller must uphold the safety contract for
989988 // `unchecked_shr`.
990989 // Any legal shift amount is losslessly representable in the self type.
991- // FIXME(const-hack) replace with `.try_into().ok().unwrap_unchecked()`.
992- unsafe { intrinsics:: unchecked_shr( self , rhs as _) }
990+ unsafe { intrinsics:: unchecked_shr( self , conv_rhs_for_unchecked_shift!( $SelfT, rhs) ) }
993991 }
994992
995993 /// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if
You can’t perform that action at this time.
0 commit comments