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,45 @@ 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:: Const :: zero_sized ( tables. tcx , ty_internal) ) . stable ( & mut * tables) )
394
402
}
395
403
396
- fn new_const_str ( & self , value : & str ) -> Const {
404
+ fn new_const_str ( & self , value : & str ) -> MirConst {
397
405
let mut tables = self . 0 . borrow_mut ( ) ;
398
406
let tcx = tables. tcx ;
399
407
let ty = ty:: Ty :: new_static_str ( tcx) ;
400
408
let bytes = value. as_bytes ( ) ;
401
409
let val_tree = ty:: ValTree :: from_raw_bytes ( tcx, bytes) ;
402
410
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)
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
+ 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)
409
420
}
410
421
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 > {
412
440
let mut tables = self . 0 . borrow_mut ( ) ;
413
441
let tcx = tables. tcx ;
414
442
let ty = ty:: Ty :: new_uint ( tcx, uint_ty. internal ( & mut * tables, tcx) ) ;
@@ -453,7 +481,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
453
481
. stable ( & mut * tables)
454
482
}
455
483
456
- fn const_pretty ( & self , cnst : & stable_mir:: ty:: Const ) -> String {
484
+ fn mir_const_pretty ( & self , cnst : & stable_mir:: ty:: MirConst ) -> String {
457
485
let mut tables = self . 0 . borrow_mut ( ) ;
458
486
let tcx = tables. tcx ;
459
487
cnst. internal ( & mut * tables, tcx) . to_string ( )
@@ -474,6 +502,11 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
474
502
tables. types [ ty] . kind ( ) . stable ( & mut * tables)
475
503
}
476
504
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
+
477
510
fn rigid_ty_discriminant_ty ( & self , ty : & RigidTy ) -> stable_mir:: ty:: Ty {
478
511
let mut tables = self . 0 . borrow_mut ( ) ;
479
512
let tcx = tables. tcx ;
0 commit comments