@@ -18,6 +18,7 @@ use crate::rustc::ty::{self, Ty, TyCtxt, Instance};
18
18
use crate :: rustc:: ty:: subst:: { Subst , Substs } ;
19
19
use std:: cmp:: Ordering :: { self , Equal } ;
20
20
use std:: cmp:: PartialOrd ;
21
+ use std:: convert:: TryInto ;
21
22
use std:: hash:: { Hash , Hasher } ;
22
23
use std:: mem;
23
24
use std:: rc:: Rc ;
@@ -341,8 +342,12 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
341
342
BinOpKind :: Mul => l. checked_mul ( r) . map ( zext) ,
342
343
BinOpKind :: Div if r != 0 => l. checked_div ( r) . map ( zext) ,
343
344
BinOpKind :: Rem if r != 0 => l. checked_rem ( r) . map ( zext) ,
344
- BinOpKind :: Shr => l. checked_shr ( r as u128 as u32 ) . map ( zext) ,
345
- BinOpKind :: Shl => l. checked_shl ( r as u128 as u32 ) . map ( zext) ,
345
+ BinOpKind :: Shr => l. checked_shr (
346
+ ( r as u128 ) . try_into ( ) . expect ( "shift too large" )
347
+ ) . map ( zext) ,
348
+ BinOpKind :: Shl => l. checked_shl (
349
+ ( r as u128 ) . try_into ( ) . expect ( "shift too large" )
350
+ ) . map ( zext) ,
346
351
BinOpKind :: BitXor => Some ( zext ( l ^ r) ) ,
347
352
BinOpKind :: BitOr => Some ( zext ( l | r) ) ,
348
353
BinOpKind :: BitAnd => Some ( zext ( l & r) ) ,
@@ -362,8 +367,12 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
362
367
BinOpKind :: Mul => l. checked_mul ( r) . map ( Constant :: Int ) ,
363
368
BinOpKind :: Div => l. checked_div ( r) . map ( Constant :: Int ) ,
364
369
BinOpKind :: Rem => l. checked_rem ( r) . map ( Constant :: Int ) ,
365
- BinOpKind :: Shr => l. checked_shr ( r as u32 ) . map ( Constant :: Int ) ,
366
- BinOpKind :: Shl => l. checked_shl ( r as u32 ) . map ( Constant :: Int ) ,
370
+ BinOpKind :: Shr => l. checked_shr (
371
+ r. try_into ( ) . expect ( "shift too large" )
372
+ ) . map ( Constant :: Int ) ,
373
+ BinOpKind :: Shl => l. checked_shl (
374
+ r. try_into ( ) . expect ( "shift too large" )
375
+ ) . map ( Constant :: Int ) ,
367
376
BinOpKind :: BitXor => Some ( Constant :: Int ( l ^ r) ) ,
368
377
BinOpKind :: BitOr => Some ( Constant :: Int ( l | r) ) ,
369
378
BinOpKind :: BitAnd => Some ( Constant :: Int ( l & r) ) ,
@@ -426,8 +435,12 @@ pub fn miri_to_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, result: &ty::Const<'
426
435
ConstValue :: Scalar ( Scalar :: Bits { bits : b, ..} ) => match result. ty . sty {
427
436
ty:: Bool => Some ( Constant :: Bool ( b == 1 ) ) ,
428
437
ty:: Uint ( _) | ty:: Int ( _) => Some ( Constant :: Int ( b) ) ,
429
- ty:: Float ( FloatTy :: F32 ) => Some ( Constant :: F32 ( f32:: from_bits ( b as u32 ) ) ) ,
430
- ty:: Float ( FloatTy :: F64 ) => Some ( Constant :: F64 ( f64:: from_bits ( b as u64 ) ) ) ,
438
+ ty:: Float ( FloatTy :: F32 ) => Some ( Constant :: F32 ( f32:: from_bits (
439
+ b. try_into ( ) . expect ( "invalid f32 bit representation" )
440
+ ) ) ) ,
441
+ ty:: Float ( FloatTy :: F64 ) => Some ( Constant :: F64 ( f64:: from_bits (
442
+ b. try_into ( ) . expect ( "invalid f64 bit representation" )
443
+ ) ) ) ,
431
444
// FIXME: implement other conversion
432
445
_ => None ,
433
446
} ,
@@ -439,7 +452,7 @@ pub fn miri_to_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, result: &ty::Const<'
439
452
. alloc_map
440
453
. lock ( )
441
454
. unwrap_memory ( ptr. alloc_id ) ;
442
- let offset = ptr. offset . bytes ( ) as usize ;
455
+ let offset = ptr. offset . bytes ( ) . try_into ( ) . expect ( "too-large pointer offset" ) ;
443
456
let n = n as usize ;
444
457
String :: from_utf8 ( alloc. bytes [ offset..( offset + n) ] . to_owned ( ) ) . ok ( ) . map ( Constant :: Str )
445
458
} ,
0 commit comments