@@ -7,7 +7,7 @@ use rustc_data_structures::sync::Lrc;
77use rustc_hir:: def:: { DefKind , Res } ;
88use rustc_hir:: { BinOp , BinOpKind , Block , Expr , ExprKind , HirId , QPath , UnOp } ;
99use rustc_lint:: LateContext ;
10- use rustc_middle:: mir:: interpret :: Scalar ;
10+ use rustc_middle:: mir;
1111use rustc_middle:: ty:: subst:: { Subst , SubstsRef } ;
1212use rustc_middle:: ty:: { self , FloatTy , ScalarInt , Ty , TyCtxt } ;
1313use rustc_middle:: { bug, span_bug} ;
@@ -350,8 +350,8 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
350350 None ,
351351 )
352352 . ok ( )
353- . map ( |val| rustc_middle :: ty :: Const :: from_value ( self . lcx . tcx , val, ty) ) ?;
354- let result = miri_to_const ( & result) ;
353+ . map ( |val| mir :: ConstantKind :: Val ( val, ty) ) ?;
354+ let result = miri_to_const ( result) ;
355355 if result. is_some ( ) {
356356 self . needed_resolution = true ;
357357 }
@@ -501,44 +501,38 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
501501 }
502502}
503503
504- pub fn miri_to_const ( result : & ty :: Const < ' _ > ) -> Option < Constant > {
504+ pub fn miri_to_const ( result : mir :: ConstantKind < ' tcx > ) -> Option < Constant > {
505505 use rustc_middle:: mir:: interpret:: ConstValue ;
506- match result. val {
507- ty:: ConstKind :: Value ( ConstValue :: Scalar ( Scalar :: Int ( int) ) ) => {
508- match result. ty . kind ( ) {
509- ty:: Bool => Some ( Constant :: Bool ( int == ScalarInt :: TRUE ) ) ,
510- ty:: Uint ( _) | ty:: Int ( _) => Some ( Constant :: Int ( int. assert_bits ( int. size ( ) ) ) ) ,
506+ match * result. ty ( ) . kind ( ) {
507+ ty:: Bool => Some ( Constant :: Bool ( result. try_to_scalar_int ( ) ? == ScalarInt :: TRUE ) ) ,
508+ ty:: Uint ( _) | ty:: Int ( _) => {
509+ let int = result. try_to_scalar_int ( ) ?;
510+ Some ( Constant :: Int ( int. assert_bits ( int. size ( ) ) ) )
511+ }
511512 ty:: Float ( FloatTy :: F32 ) => Some ( Constant :: F32 ( f32:: from_bits (
512- int . try_into ( ) . expect ( "invalid f32 bit representation" ) ,
513+ result . try_to_scalar_int ( ) ? . try_into ( ) . expect ( "invalid f32 bit representation" ) ,
513514 ) ) ) ,
514515 ty:: Float ( FloatTy :: F64 ) => Some ( Constant :: F64 ( f64:: from_bits (
515- int . try_into ( ) . expect ( "invalid f64 bit representation" ) ,
516+ result . try_to_scalar_int ( ) ? . try_into ( ) . expect ( "invalid f64 bit representation" ) ,
516517 ) ) ) ,
517- ty:: RawPtr ( type_and_mut ) => {
518- if let ty :: Uint ( _ ) = type_and_mut . ty . kind ( ) {
519- return Some ( Constant :: RawPtr ( int. assert_bits ( int. size ( ) ) ) ) ;
518+ ty:: RawPtr ( _ ) => {
519+ let int = result . try_to_scalar_int ( ) ? ;
520+ Some ( Constant :: RawPtr ( int. assert_bits ( int. size ( ) ) ) )
520521 }
521- None
522- } ,
523- // FIXME: implement other conversions.
524- _ => None ,
525- }
526- } ,
527- ty:: ConstKind :: Value ( ConstValue :: Slice { data, start, end } ) => match result. ty . kind ( ) {
528522 ty:: Ref ( _, tam, _) => match tam. kind ( ) {
529- ty:: Str => String :: from_utf8 (
530- data. inspect_with_uninit_and_ptr_outside_interpreter ( start.. end)
531- . to_owned ( ) ,
523+ ty:: Str => match result . try_to_value ( ) ? {
524+ ConstValue :: Slice { data, start, end } => String :: from_utf8 (
525+ data . inspect_with_uninit_and_ptr_outside_interpreter ( start..end ) . to_owned ( ) ,
532526 )
533527 . ok ( )
534528 . map ( Constant :: Str ) ,
535529 _ => None ,
536530 } ,
537531 _ => None ,
538532 } ,
539- ty:: ConstKind :: Value ( ConstValue :: ByRef { alloc , offset : _ } ) => match result. ty . kind ( ) {
540- ty :: Array ( sub_type , len ) => match sub_type. kind ( ) {
541- ty:: Float ( FloatTy :: F32 ) => match miri_to_const ( len) {
533+ ty:: Array ( sub_type , len ) => match result. try_to_value ( ) ? {
534+ ConstValue :: ByRef { alloc , offset : _ } => match sub_type. kind ( ) {
535+ ty:: Float ( FloatTy :: F32 ) => match miri_to_const ( len. into ( ) ) {
542536 Some ( Constant :: Int ( len) ) => alloc
543537 . inspect_with_uninit_and_ptr_outside_interpreter ( 0 ..( 4 * len as usize ) )
544538 . to_owned ( )
@@ -552,7 +546,7 @@ pub fn miri_to_const(result: &ty::Const<'_>) -> Option<Constant> {
552546 . map ( Constant :: Vec ) ,
553547 _ => None ,
554548 } ,
555- ty:: Float ( FloatTy :: F64 ) => match miri_to_const ( len) {
549+ ty:: Float ( FloatTy :: F64 ) => match miri_to_const ( len. into ( ) ) {
556550 Some ( Constant :: Int ( len) ) => alloc
557551 . inspect_with_uninit_and_ptr_outside_interpreter ( 0 ..( 8 * len as usize ) )
558552 . to_owned ( )
0 commit comments