6
6
#![ allow( rustc:: usage_of_qualified_ty) ]
7
7
8
8
use rustc_abi:: HasDataLayout ;
9
- use rustc_middle:: ty;
10
9
use rustc_middle:: ty:: layout:: {
11
10
FnAbiOf , FnAbiOfHelpers , HasParamEnv , HasTyCtxt , LayoutOf , LayoutOfHelpers ,
12
11
} ;
13
12
use rustc_middle:: ty:: print:: { with_forced_trimmed_paths, with_no_trimmed_paths} ;
14
13
use rustc_middle:: ty:: {
15
14
GenericPredicates , Instance , List , ParamEnv , ScalarInt , TyCtxt , TypeVisitableExt , ValTree ,
16
15
} ;
16
+ use rustc_middle:: { mir, ty} ;
17
17
use rustc_span:: def_id:: LOCAL_CRATE ;
18
18
use stable_mir:: abi:: { FnAbi , Layout , LayoutShape } ;
19
19
use stable_mir:: compiler_interface:: Context ;
@@ -22,9 +22,9 @@ use stable_mir::mir::mono::{InstanceDef, StaticDef};
22
22
use stable_mir:: mir:: { BinOp , Body , Place , UnOp } ;
23
23
use stable_mir:: target:: { MachineInfo , MachineSize } ;
24
24
use 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 ,
28
28
} ;
29
29
use stable_mir:: { Crate , CrateDef , CrateItem , CrateNum , DefId , Error , Filename , ItemKind , Symbol } ;
30
30
use std:: cell:: RefCell ;
@@ -360,7 +360,15 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
360
360
def. internal ( & mut * tables, tcx) . fields . iter ( ) . map ( |f| f. stable ( & mut * tables) ) . collect ( )
361
361
}
362
362
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 > {
364
372
let mut tables = self . 0 . borrow_mut ( ) ;
365
373
let tcx = tables. tcx ;
366
374
let mir_const = cnst. internal ( & mut * tables, tcx) ;
@@ -369,7 +377,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
369
377
. ok_or_else ( || Error :: new ( format ! ( "Const `{cnst:?}` cannot be encoded as u64" ) ) )
370
378
}
371
379
372
- fn try_new_const_zst ( & self , ty : Ty ) -> Result < Const , Error > {
380
+ fn try_new_const_zst ( & self , ty : Ty ) -> Result < MirConst , Error > {
373
381
let mut tables = self . 0 . borrow_mut ( ) ;
374
382
let tcx = tables. tcx ;
375
383
let ty_internal = ty. internal ( & mut * tables, tcx) ;
@@ -390,25 +398,47 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
390
398
) ) ) ;
391
399
}
392
400
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) )
394
403
}
395
404
396
- fn new_const_str ( & self , value : & str ) -> Const {
405
+ fn new_const_str ( & self , value : & str ) -> MirConst {
397
406
let mut tables = self . 0 . borrow_mut ( ) ;
398
407
let tcx = tables. tcx ;
399
408
let ty = ty:: Ty :: new_static_str ( tcx) ;
400
409
let bytes = value. as_bytes ( ) ;
401
410
let val_tree = ty:: ValTree :: from_raw_bytes ( tcx, bytes) ;
402
411
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)
404
413
}
405
414
406
- fn new_const_bool ( & self , value : bool ) -> Const {
415
+ fn new_const_bool ( & self , value : bool ) -> MirConst {
407
416
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)
409
419
}
410
420
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 > {
412
442
let mut tables = self . 0 . borrow_mut ( ) ;
413
443
let tcx = tables. tcx ;
414
444
let ty = ty:: Ty :: new_uint ( tcx, uint_ty. internal ( & mut * tables, tcx) ) ;
@@ -453,7 +483,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
453
483
. stable ( & mut * tables)
454
484
}
455
485
456
- fn const_pretty ( & self , cnst : & stable_mir:: ty:: Const ) -> String {
486
+ fn mir_const_pretty ( & self , cnst : & stable_mir:: ty:: MirConst ) -> String {
457
487
let mut tables = self . 0 . borrow_mut ( ) ;
458
488
let tcx = tables. tcx ;
459
489
cnst. internal ( & mut * tables, tcx) . to_string ( )
@@ -474,6 +504,11 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
474
504
tables. types [ ty] . kind ( ) . stable ( & mut * tables)
475
505
}
476
506
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
+
477
512
fn rigid_ty_discriminant_ty ( & self , ty : & RigidTy ) -> stable_mir:: ty:: Ty {
478
513
let mut tables = self . 0 . borrow_mut ( ) ;
479
514
let tcx = tables. tcx ;
0 commit comments