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,47 @@ 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_internal, ty:: Const :: zero_sized ( tables. tcx , ty_internal) )
402+ . stable ( & mut * tables) )
394403 }
395404
396- fn new_const_str ( & self , value : & str ) -> Const {
405+ fn new_const_str ( & self , value : & str ) -> MirConst {
397406 let mut tables = self . 0 . borrow_mut ( ) ;
398407 let tcx = tables. tcx ;
399408 let ty = ty:: Ty :: new_static_str ( tcx) ;
400409 let bytes = value. as_bytes ( ) ;
401410 let val_tree = ty:: ValTree :: from_raw_bytes ( tcx, bytes) ;
402411
403- ty :: Const :: new_value ( tcx, val_tree, ty) . stable ( & mut * tables)
412+ mir :: Const :: Ty ( ty , ty :: Const :: new_value ( tcx, val_tree, ty) ) . stable ( & mut * tables)
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+ mir:: Const :: Ty ( tables. tcx . types . bool , ty:: Const :: from_bool ( tables. tcx , value) )
418+ . stable ( & mut * tables)
409419 }
410420
411- fn try_new_const_uint ( & self , value : u128 , uint_ty : UintTy ) -> Result < Const , Error > {
421+ fn try_new_const_uint ( & self , value : u128 , uint_ty : UintTy ) -> Result < MirConst , Error > {
422+ let mut tables = self . 0 . borrow_mut ( ) ;
423+ let tcx = tables. tcx ;
424+ let ty = ty:: Ty :: new_uint ( tcx, uint_ty. internal ( & mut * tables, tcx) ) ;
425+ let size = tables. tcx . layout_of ( ParamEnv :: empty ( ) . and ( ty) ) . unwrap ( ) . size ;
426+
427+ // We don't use Const::from_bits since it doesn't have any error checking.
428+ let scalar = ScalarInt :: try_from_uint ( value, size) . ok_or_else ( || {
429+ Error :: new ( format ! ( "Value overflow: cannot convert `{value}` to `{ty}`." ) )
430+ } ) ?;
431+ Ok ( mir:: Const :: Ty (
432+ ty,
433+ ty:: Const :: new_value ( tables. tcx , ValTree :: from_scalar_int ( scalar) , ty) ,
434+ )
435+ . stable ( & mut * tables) )
436+ }
437+ fn try_new_ty_const_uint (
438+ & self ,
439+ value : u128 ,
440+ uint_ty : UintTy ,
441+ ) -> Result < stable_mir:: ty:: TyConst , Error > {
412442 let mut tables = self . 0 . borrow_mut ( ) ;
413443 let tcx = tables. tcx ;
414444 let ty = ty:: Ty :: new_uint ( tcx, uint_ty. internal ( & mut * tables, tcx) ) ;
@@ -453,7 +483,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
453483 . stable ( & mut * tables)
454484 }
455485
456- fn const_pretty ( & self , cnst : & stable_mir:: ty:: Const ) -> String {
486+ fn mir_const_pretty ( & self , cnst : & stable_mir:: ty:: MirConst ) -> String {
457487 let mut tables = self . 0 . borrow_mut ( ) ;
458488 let tcx = tables. tcx ;
459489 cnst. internal ( & mut * tables, tcx) . to_string ( )
@@ -474,6 +504,11 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
474504 tables. types [ ty] . kind ( ) . stable ( & mut * tables)
475505 }
476506
507+ fn ty_const_pretty ( & self , ct : stable_mir:: ty:: TyConstId ) -> String {
508+ let tables = self . 0 . borrow_mut ( ) ;
509+ tables. ty_consts [ ct] . to_string ( )
510+ }
511+
477512 fn rigid_ty_discriminant_ty ( & self , ty : & RigidTy ) -> stable_mir:: ty:: Ty {
478513 let mut tables = self . 0 . borrow_mut ( ) ;
479514 let tcx = tables. tcx ;
0 commit comments