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 {
785
785
// SAFETY: the caller must uphold the safety contract for
786
786
// `unchecked_shl`.
787
787
// 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) ) }
790
789
}
791
790
792
791
/// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is
@@ -834,8 +833,7 @@ macro_rules! int_impl {
834
833
// SAFETY: the caller must uphold the safety contract for
835
834
// `unchecked_shr`.
836
835
// 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) ) }
839
837
}
840
838
841
839
/// Checked absolute value. Computes `self.abs()`, returning `None` if
Original file line number Diff line number Diff line change 3
3
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
4
4
5
5
use crate :: ascii;
6
+ use crate :: convert:: TryInto ;
6
7
use crate :: intrinsics;
7
8
use crate :: mem;
8
9
use crate :: ops:: { Add , Mul , Sub } ;
@@ -224,6 +225,23 @@ macro_rules! widening_impl {
224
225
} ;
225
226
}
226
227
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
+
227
245
impl i8 {
228
246
int_impl ! {
229
247
Self = i8 ,
Original file line number Diff line number Diff line change @@ -939,8 +939,7 @@ macro_rules! uint_impl {
939
939
// SAFETY: the caller must uphold the safety contract for
940
940
// `unchecked_shl`.
941
941
// 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) ) }
944
943
}
945
944
946
945
/// Checked shift right. Computes `self >> rhs`, returning `None`
@@ -988,8 +987,7 @@ macro_rules! uint_impl {
988
987
// SAFETY: the caller must uphold the safety contract for
989
988
// `unchecked_shr`.
990
989
// 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) ) }
993
991
}
994
992
995
993
/// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if
You can’t perform that action at this time.
0 commit comments