@@ -7,7 +7,7 @@ use rustc_data_structures::sync::Lrc;
7
7
use rustc_hir:: def:: { DefKind , Res } ;
8
8
use rustc_hir:: { BinOp , BinOpKind , Block , Expr , ExprKind , HirId , QPath , UnOp } ;
9
9
use rustc_lint:: LateContext ;
10
- use rustc_middle:: mir:: interpret :: Scalar ;
10
+ use rustc_middle:: mir;
11
11
use rustc_middle:: ty:: subst:: { Subst , SubstsRef } ;
12
12
use rustc_middle:: ty:: { self , FloatTy , ScalarInt , Ty , TyCtxt } ;
13
13
use rustc_middle:: { bug, span_bug} ;
@@ -354,7 +354,7 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
354
354
None ,
355
355
)
356
356
. ok ( )
357
- . map ( |val| rustc_middle :: ty :: Const :: from_value ( self . lcx . tcx , val, ty) ) ?;
357
+ . map ( |val| mir :: ConstantKind :: Val ( val, ty) ) ?;
358
358
let result = miri_to_const ( result) ;
359
359
if result. is_some ( ) {
360
360
self . needed_resolution = true ;
@@ -505,44 +505,38 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
505
505
}
506
506
}
507
507
508
- pub fn miri_to_const ( result : & ty :: Const < ' _ > ) -> Option < Constant > {
508
+ pub fn miri_to_const ( result : mir :: ConstantKind < ' tcx > ) -> Option < Constant > {
509
509
use rustc_middle:: mir:: interpret:: ConstValue ;
510
- match result. val {
511
- ty:: ConstKind :: Value ( ConstValue :: Scalar ( Scalar :: Int ( int) ) ) => {
512
- match result. ty . kind ( ) {
513
- ty:: Bool => Some ( Constant :: Bool ( int == ScalarInt :: TRUE ) ) ,
514
- ty:: Uint ( _) | ty:: Int ( _) => Some ( Constant :: Int ( int. assert_bits ( int. size ( ) ) ) ) ,
510
+ match * result. ty ( ) . kind ( ) {
511
+ ty:: Bool => Some ( Constant :: Bool ( result. try_to_scalar_int ( ) ? == ScalarInt :: TRUE ) ) ,
512
+ ty:: Uint ( _) | ty:: Int ( _) => {
513
+ let int = result. try_to_scalar_int ( ) ?;
514
+ Some ( Constant :: Int ( int. assert_bits ( int. size ( ) ) ) )
515
+ }
515
516
ty:: Float ( FloatTy :: F32 ) => Some ( Constant :: F32 ( f32:: from_bits (
516
- int . try_into ( ) . expect ( "invalid f32 bit representation" ) ,
517
+ result . try_to_scalar_int ( ) ? . try_into ( ) . expect ( "invalid f32 bit representation" ) ,
517
518
) ) ) ,
518
519
ty:: Float ( FloatTy :: F64 ) => Some ( Constant :: F64 ( f64:: from_bits (
519
- int . try_into ( ) . expect ( "invalid f64 bit representation" ) ,
520
+ result . try_to_scalar_int ( ) ? . try_into ( ) . expect ( "invalid f64 bit representation" ) ,
520
521
) ) ) ,
521
- ty:: RawPtr ( type_and_mut ) => {
522
- if let ty :: Uint ( _ ) = type_and_mut . ty . kind ( ) {
523
- return Some ( Constant :: RawPtr ( int. assert_bits ( int. size ( ) ) ) ) ;
522
+ ty:: RawPtr ( _ ) => {
523
+ let int = result . try_to_scalar_int ( ) ? ;
524
+ Some ( Constant :: RawPtr ( int. assert_bits ( int. size ( ) ) ) )
524
525
}
525
- None
526
- } ,
527
- // FIXME: implement other conversions.
528
- _ => None ,
529
- }
530
- } ,
531
- ty:: ConstKind :: Value ( ConstValue :: Slice { data, start, end } ) => match result. ty . kind ( ) {
532
526
ty:: Ref ( _, tam, _) => match tam. kind ( ) {
533
- ty:: Str => String :: from_utf8 (
534
- data. inspect_with_uninit_and_ptr_outside_interpreter ( start.. end)
535
- . to_owned ( ) ,
527
+ ty:: Str => match result . try_to_value ( ) ? {
528
+ ConstValue :: Slice { data, start, end } => String :: from_utf8 (
529
+ data . inspect_with_uninit_and_ptr_outside_interpreter ( start..end ) . to_owned ( ) ,
536
530
)
537
531
. ok ( )
538
532
. map ( Constant :: Str ) ,
539
533
_ => None ,
540
534
} ,
541
535
_ => None ,
542
536
} ,
543
- ty:: ConstKind :: Value ( ConstValue :: ByRef { alloc , offset : _ } ) => match result. ty . kind ( ) {
544
- ty :: Array ( sub_type , len ) => match sub_type. kind ( ) {
545
- ty:: Float ( FloatTy :: F32 ) => match miri_to_const ( len) {
537
+ ty:: Array ( sub_type , len ) => match result. try_to_value ( ) ? {
538
+ ConstValue :: ByRef { alloc , offset : _ } => match sub_type. kind ( ) {
539
+ ty:: Float ( FloatTy :: F32 ) => match miri_to_const ( len. into ( ) ) {
546
540
Some ( Constant :: Int ( len) ) => alloc
547
541
. inspect_with_uninit_and_ptr_outside_interpreter ( 0 ..( 4 * len as usize ) )
548
542
. to_owned ( )
@@ -556,7 +550,7 @@ pub fn miri_to_const(result: &ty::Const<'_>) -> Option<Constant> {
556
550
. map ( Constant :: Vec ) ,
557
551
_ => None ,
558
552
} ,
559
- ty:: Float ( FloatTy :: F64 ) => match miri_to_const ( len) {
553
+ ty:: Float ( FloatTy :: F64 ) => match miri_to_const ( len. into ( ) ) {
560
554
Some ( Constant :: Int ( len) ) => alloc
561
555
. inspect_with_uninit_and_ptr_outside_interpreter ( 0 ..( 8 * len as usize ) )
562
556
. to_owned ( )
0 commit comments