@@ -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} ;
@@ -354,7 +354,7 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
354354 None ,
355355 )
356356 . ok ( )
357- . map ( |val| rustc_middle :: ty :: Const :: from_value ( self . lcx . tcx , val, ty) ) ?;
357+ . map ( |val| mir :: ConstantKind :: Val ( val, ty) ) ?;
358358 let result = miri_to_const ( result) ;
359359 if result. is_some ( ) {
360360 self . needed_resolution = true ;
@@ -505,44 +505,38 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
505505 }
506506}
507507
508- pub fn miri_to_const ( result : & ty :: Const < ' _ > ) -> Option < Constant > {
508+ pub fn miri_to_const ( result : mir :: ConstantKind < ' tcx > ) -> Option < Constant > {
509509 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+ }
515516 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" ) ,
517518 ) ) ) ,
518519 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" ) ,
520521 ) ) ) ,
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 ( ) ) ) )
524525 }
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 ( ) {
532526 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 ( ) ,
536530 )
537531 . ok ( )
538532 . map ( Constant :: Str ) ,
539533 _ => None ,
540534 } ,
541535 _ => None ,
542536 } ,
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 ( ) ) {
546540 Some ( Constant :: Int ( len) ) => alloc
547541 . inspect_with_uninit_and_ptr_outside_interpreter ( 0 ..( 4 * len as usize ) )
548542 . to_owned ( )
@@ -556,7 +550,7 @@ pub fn miri_to_const(result: &ty::Const<'_>) -> Option<Constant> {
556550 . map ( Constant :: Vec ) ,
557551 _ => None ,
558552 } ,
559- ty:: Float ( FloatTy :: F64 ) => match miri_to_const ( len) {
553+ ty:: Float ( FloatTy :: F64 ) => match miri_to_const ( len. into ( ) ) {
560554 Some ( Constant :: Int ( len) ) => alloc
561555 . inspect_with_uninit_and_ptr_outside_interpreter ( 0 ..( 8 * len as usize ) )
562556 . to_owned ( )
0 commit comments