66#![ allow( rustc:: usage_of_qualified_ty) ]
77
88use rustc_abi:: HasDataLayout ;
9- use rustc_middle:: ty;
109use rustc_middle:: ty:: layout:: {
1110 FnAbiOf , FnAbiOfHelpers , HasParamEnv , HasTyCtxt , LayoutOf , LayoutOfHelpers ,
1211} ;
1312use rustc_middle:: ty:: print:: { with_forced_trimmed_paths, with_no_trimmed_paths} ;
1413use rustc_middle:: ty:: {
1514 GenericPredicates , Instance , List , ParamEnv , ScalarInt , TyCtxt , TypeVisitableExt , ValTree ,
1615} ;
16+ use rustc_middle:: { mir, ty} ;
1717use rustc_span:: def_id:: LOCAL_CRATE ;
1818use stable_mir:: abi:: { FnAbi , Layout , LayoutShape } ;
1919use stable_mir:: compiler_interface:: Context ;
@@ -22,9 +22,9 @@ use stable_mir::mir::mono::{InstanceDef, StaticDef};
2222use stable_mir:: mir:: { BinOp , Body , Place , UnOp } ;
2323use stable_mir:: target:: { MachineInfo , MachineSize } ;
2424use stable_mir:: ty:: {
25- AdtDef , AdtKind , Allocation , ClosureDef , ClosureKind , Const , FieldDef , FnDef , ForeignDef ,
26- ForeignItemKind , GenericArgs , IntrinsicDef , LineInfo , PolyFnSig , RigidTy , Span , Ty , TyKind ,
27- UintTy , VariantDef ,
25+ AdtDef , AdtKind , Allocation , ClosureDef , ClosureKind , FieldDef , FnDef , ForeignDef ,
26+ ForeignItemKind , GenericArgs , IntrinsicDef , LineInfo , MirConst , PolyFnSig , RigidTy , Span , Ty ,
27+ TyConst , TyKind , UintTy , VariantDef ,
2828} ;
2929use stable_mir:: { Crate , CrateDef , CrateItem , CrateNum , DefId , Error , Filename , ItemKind , Symbol } ;
3030use std:: cell:: RefCell ;
@@ -360,7 +360,15 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
360360 def. internal ( & mut * tables, tcx) . fields . iter ( ) . map ( |f| f. stable ( & mut * tables) ) . collect ( )
361361 }
362362
363- fn eval_target_usize ( & self , cnst : & Const ) -> Result < u64 , Error > {
363+ fn eval_target_usize ( & self , cnst : & MirConst ) -> Result < u64 , Error > {
364+ let mut tables = self . 0 . borrow_mut ( ) ;
365+ let tcx = tables. tcx ;
366+ let mir_const = cnst. internal ( & mut * tables, tcx) ;
367+ mir_const
368+ . try_eval_target_usize ( tables. tcx , ParamEnv :: empty ( ) )
369+ . ok_or_else ( || Error :: new ( format ! ( "Const `{cnst:?}` cannot be encoded as u64" ) ) )
370+ }
371+ fn eval_target_usize_ty ( & self , cnst : & TyConst ) -> Result < u64 , Error > {
364372 let mut tables = self . 0 . borrow_mut ( ) ;
365373 let tcx = tables. tcx ;
366374 let mir_const = cnst. internal ( & mut * tables, tcx) ;
@@ -369,7 +377,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
369377 . ok_or_else ( || Error :: new ( format ! ( "Const `{cnst:?}` cannot be encoded as u64" ) ) )
370378 }
371379
372- fn try_new_const_zst ( & self , ty : Ty ) -> Result < Const , Error > {
380+ fn try_new_const_zst ( & self , ty : Ty ) -> Result < MirConst , Error > {
373381 let mut tables = self . 0 . borrow_mut ( ) ;
374382 let tcx = tables. tcx ;
375383 let ty_internal = ty. internal ( & mut * tables, tcx) ;
@@ -390,25 +398,45 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
390398 ) ) ) ;
391399 }
392400
393- Ok ( ty:: Const :: zero_sized ( tables. tcx , ty_internal) . stable ( & mut * tables) )
401+ Ok ( mir :: Const :: Ty ( ty:: Const :: zero_sized ( tables. tcx , ty_internal) ) . stable ( & mut * tables) )
394402 }
395403
396- fn new_const_str ( & self , value : & str ) -> Const {
404+ fn new_const_str ( & self , value : & str ) -> MirConst {
397405 let mut tables = self . 0 . borrow_mut ( ) ;
398406 let tcx = tables. tcx ;
399407 let ty = ty:: Ty :: new_static_str ( tcx) ;
400408 let bytes = value. as_bytes ( ) ;
401409 let val_tree = ty:: ValTree :: from_raw_bytes ( tcx, bytes) ;
402410
403- ty:: Const :: new_value ( tcx, val_tree, ty) . stable ( & mut * tables)
411+ let ct = ty:: Const :: new_value ( tcx, val_tree, ty) ;
412+ super :: convert:: mir_const_from_ty_const ( & mut * tables, ct, ty)
404413 }
405414
406- fn new_const_bool ( & self , value : bool ) -> Const {
415+ fn new_const_bool ( & self , value : bool ) -> MirConst {
407416 let mut tables = self . 0 . borrow_mut ( ) ;
408- ty:: Const :: from_bool ( tables. tcx , value) . stable ( & mut * tables)
417+ let ct = ty:: Const :: from_bool ( tables. tcx , value) ;
418+ let ty = tables. tcx . types . bool ;
419+ super :: convert:: mir_const_from_ty_const ( & mut * tables, ct, ty)
409420 }
410421
411- fn try_new_const_uint ( & self , value : u128 , uint_ty : UintTy ) -> Result < Const , Error > {
422+ fn try_new_const_uint ( & self , value : u128 , uint_ty : UintTy ) -> Result < MirConst , Error > {
423+ let mut tables = self . 0 . borrow_mut ( ) ;
424+ let tcx = tables. tcx ;
425+ let ty = ty:: Ty :: new_uint ( tcx, uint_ty. internal ( & mut * tables, tcx) ) ;
426+ let size = tables. tcx . layout_of ( ParamEnv :: empty ( ) . and ( ty) ) . unwrap ( ) . size ;
427+
428+ // We don't use Const::from_bits since it doesn't have any error checking.
429+ let scalar = ScalarInt :: try_from_uint ( value, size) . ok_or_else ( || {
430+ Error :: new ( format ! ( "Value overflow: cannot convert `{value}` to `{ty}`." ) )
431+ } ) ?;
432+ let ct = ty:: Const :: new_value ( tables. tcx , ValTree :: from_scalar_int ( scalar) , ty) ;
433+ Ok ( super :: convert:: mir_const_from_ty_const ( & mut * tables, ct, ty) )
434+ }
435+ fn try_new_ty_const_uint (
436+ & self ,
437+ value : u128 ,
438+ uint_ty : UintTy ,
439+ ) -> Result < stable_mir:: ty:: TyConst , Error > {
412440 let mut tables = self . 0 . borrow_mut ( ) ;
413441 let tcx = tables. tcx ;
414442 let ty = ty:: Ty :: new_uint ( tcx, uint_ty. internal ( & mut * tables, tcx) ) ;
@@ -453,7 +481,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
453481 . stable ( & mut * tables)
454482 }
455483
456- fn const_pretty ( & self , cnst : & stable_mir:: ty:: Const ) -> String {
484+ fn mir_const_pretty ( & self , cnst : & stable_mir:: ty:: MirConst ) -> String {
457485 let mut tables = self . 0 . borrow_mut ( ) ;
458486 let tcx = tables. tcx ;
459487 cnst. internal ( & mut * tables, tcx) . to_string ( )
@@ -474,6 +502,11 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
474502 tables. types [ ty] . kind ( ) . stable ( & mut * tables)
475503 }
476504
505+ fn ty_const_pretty ( & self , ct : stable_mir:: ty:: TyConstId ) -> String {
506+ let tables = self . 0 . borrow_mut ( ) ;
507+ tables. ty_consts [ ct] . to_string ( )
508+ }
509+
477510 fn rigid_ty_discriminant_ty ( & self , ty : & RigidTy ) -> stable_mir:: ty:: Ty {
478511 let mut tables = self . 0 . borrow_mut ( ) ;
479512 let tcx = tables. tcx ;
0 commit comments