@@ -23,7 +23,8 @@ use stable_mir::mir::Body;
23
23
use stable_mir:: target:: { MachineInfo , MachineSize } ;
24
24
use stable_mir:: ty:: {
25
25
AdtDef , AdtKind , Allocation , ClosureDef , ClosureKind , Const , FieldDef , FnDef , ForeignDef ,
26
- ForeignItemKind , GenericArgs , LineInfo , PolyFnSig , RigidTy , Span , Ty , TyKind , VariantDef ,
26
+ ForeignItemKind , GenericArgs , LineInfo , PolyFnSig , RigidTy , Span , Ty , TyKind , UintTy ,
27
+ VariantDef ,
27
28
} ;
28
29
use stable_mir:: { Crate , CrateDef , CrateItem , CrateNum , DefId , Error , Filename , ItemKind , Symbol } ;
29
30
use std:: cell:: RefCell ;
@@ -341,15 +342,56 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
341
342
. ok_or_else ( || Error :: new ( format ! ( "Const `{cnst:?}` cannot be encoded as u64" ) ) )
342
343
}
343
344
344
- fn usize_to_const ( & self , val : u64 ) -> Result < Const , Error > {
345
+ fn try_new_const_zst ( & self , ty : Ty ) -> Result < Const , Error > {
345
346
let mut tables = self . 0 . borrow_mut ( ) ;
346
- let ty = tables. tcx . types . usize ;
347
+ let tcx = tables. tcx ;
348
+ let ty_internal = ty. internal ( & mut * tables, tcx) ;
349
+ let size = tables
350
+ . tcx
351
+ . layout_of ( ParamEnv :: empty ( ) . and ( ty_internal) )
352
+ . map_err ( |err| {
353
+ Error :: new ( format ! (
354
+ "Cannot create a zero-sized constant for type `{ty_internal}`: {err}"
355
+ ) )
356
+ } ) ?
357
+ . size ;
358
+ if size. bytes ( ) != 0 {
359
+ return Err ( Error :: new ( format ! (
360
+ "Cannot create a zero-sized constant for type `{ty_internal}`: \
361
+ Type `{ty_internal}` has {} bytes",
362
+ size. bytes( )
363
+ ) ) ) ;
364
+ }
365
+
366
+ Ok ( ty:: Const :: zero_sized ( tables. tcx , ty_internal) . stable ( & mut * tables) )
367
+ }
368
+
369
+ fn new_const_str ( & self , value : & str ) -> Const {
370
+ let mut tables = self . 0 . borrow_mut ( ) ;
371
+ let tcx = tables. tcx ;
372
+ let ty = ty:: Ty :: new_static_str ( tcx) ;
373
+ let bytes = value. as_bytes ( ) ;
374
+ let val_tree = ty:: ValTree :: from_raw_bytes ( tcx, bytes) ;
375
+
376
+ ty:: Const :: new_value ( tcx, val_tree, ty) . stable ( & mut * tables)
377
+ }
378
+
379
+ fn new_const_bool ( & self , value : bool ) -> Const {
380
+ let mut tables = self . 0 . borrow_mut ( ) ;
381
+ ty:: Const :: from_bool ( tables. tcx , value) . stable ( & mut * tables)
382
+ }
383
+
384
+ fn try_new_const_uint ( & self , value : u128 , uint_ty : UintTy ) -> Result < Const , Error > {
385
+ let mut tables = self . 0 . borrow_mut ( ) ;
386
+ let tcx = tables. tcx ;
387
+ let ty = ty:: Ty :: new_uint ( tcx, uint_ty. internal ( & mut * tables, tcx) ) ;
347
388
let size = tables. tcx . layout_of ( ParamEnv :: empty ( ) . and ( ty) ) . unwrap ( ) . size ;
348
389
349
- let scalar = ScalarInt :: try_from_uint ( val, size) . ok_or_else ( || {
350
- Error :: new ( format ! ( "Value overflow: cannot convert `{val}` to usize." ) )
390
+ // We don't use Const::from_bits since it doesn't have any error checking.
391
+ let scalar = ScalarInt :: try_from_uint ( value, size) . ok_or_else ( || {
392
+ Error :: new ( format ! ( "Value overflow: cannot convert `{value}` to `{ty}`." ) )
351
393
} ) ?;
352
- Ok ( rustc_middle :: ty:: Const :: new_value ( tables. tcx , ValTree :: from_scalar_int ( scalar) , ty)
394
+ Ok ( ty:: Const :: new_value ( tables. tcx , ValTree :: from_scalar_int ( scalar) , ty)
353
395
. stable ( & mut * tables) )
354
396
}
355
397
@@ -556,7 +598,9 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
556
598
global_alloc : & GlobalAlloc ,
557
599
) -> Option < stable_mir:: mir:: alloc:: AllocId > {
558
600
let mut tables = self . 0 . borrow_mut ( ) ;
559
- let GlobalAlloc :: VTable ( ty, trait_ref) = global_alloc else { return None } ;
601
+ let GlobalAlloc :: VTable ( ty, trait_ref) = global_alloc else {
602
+ return None ;
603
+ } ;
560
604
let tcx = tables. tcx ;
561
605
let alloc_id = tables. tcx . vtable_allocation ( (
562
606
ty. internal ( & mut * tables, tcx) ,
0 commit comments