@@ -24,7 +24,10 @@ use serde::{Serialize, Serializer};
24
24
use stable_mir:: {
25
25
mir:: mono:: { Instance , InstanceKind , MonoItem } ,
26
26
mir:: { alloc:: AllocId , visit:: MirVisitor , Body , LocalDecl , Rvalue , Terminator , TerminatorKind } ,
27
- ty:: { AdtDef , Allocation , ConstDef , ForeignItemKind , IndexedVal , RigidTy , TyKind , VariantIdx } ,
27
+ ty:: {
28
+ AdtDef , Allocation , ConstDef , ForeignItemKind , IndexedVal , RigidTy , TyConstKind , TyKind ,
29
+ VariantIdx ,
30
+ } ,
28
31
CrateDef , CrateItem , ItemKind ,
29
32
} ;
30
33
@@ -627,7 +630,15 @@ fn collect_ty(val_collector: &mut InternedValueCollector, val: stable_mir::ty::T
627
630
. is_some ( )
628
631
{
629
632
match val. kind ( ) {
630
- RigidTy ( Array ( ty, _) | Pat ( ty, _) | Slice ( ty) | RawPtr ( ty, _) | Ref ( _, ty, _) ) => {
633
+ RigidTy ( Array ( ty, ty_const) ) => {
634
+ collect_ty ( val_collector, ty) ;
635
+ match ty_const. kind ( ) {
636
+ TyConstKind :: Value ( ty, _) => collect_ty ( val_collector, * ty) ,
637
+ TyConstKind :: ZSTValue ( ty) => collect_ty ( val_collector, * ty) ,
638
+ _ => ( ) ,
639
+ }
640
+ }
641
+ RigidTy ( Pat ( ty, _) | Slice ( ty) | RawPtr ( ty, _) | Ref ( _, ty, _) ) => {
631
642
collect_ty ( val_collector, ty)
632
643
}
633
644
RigidTy ( Tuple ( tys) ) => collect_vec_tys ( val_collector, tys) ,
@@ -960,19 +971,32 @@ pub enum TypeMetadata {
960
971
name : String ,
961
972
adt_def : AdtDef ,
962
973
} ,
974
+ UnionType {
975
+ name : String ,
976
+ adt_def : AdtDef ,
977
+ } ,
978
+ ArrayType ( stable_mir:: ty:: Ty , Option < stable_mir:: ty:: TyConst > ) ,
979
+ PtrType ( stable_mir:: ty:: Ty ) ,
980
+ RefType ( stable_mir:: ty:: Ty ) ,
981
+ TupleType {
982
+ types : Vec < stable_mir:: ty:: Ty > ,
983
+ } ,
984
+ FunType ( String ) ,
963
985
}
964
986
965
987
fn mk_type_metadata (
966
988
tcx : TyCtxt < ' _ > ,
967
989
k : stable_mir:: ty:: Ty ,
968
990
t : TyKind ,
969
991
) -> Option < ( stable_mir:: ty:: Ty , TypeMetadata ) > {
992
+ use stable_mir:: ty:: RigidTy :: * ;
993
+ use TyKind :: RigidTy as T ;
970
994
use TypeMetadata :: * ;
971
995
match t {
972
- TyKind :: RigidTy ( prim_type) if t. is_primitive ( ) => Some ( ( k, PrimitiveType ( prim_type) ) ) ,
996
+ T ( prim_type) if t. is_primitive ( ) => Some ( ( k, PrimitiveType ( prim_type) ) ) ,
973
997
// for enums, we need a mapping of variantIdx to discriminant
974
998
// this requires access to the internals and is not provided as an interface function at the moment
975
- TyKind :: RigidTy ( RigidTy :: Adt ( adt_def, _) ) if t. is_enum ( ) => {
999
+ T ( Adt ( adt_def, _) ) if t. is_enum ( ) => {
976
1000
let adt_internal = rustc_internal:: internal ( tcx, adt_def) ;
977
1001
let discriminants = adt_internal
978
1002
. discriminants ( tcx)
@@ -989,11 +1013,35 @@ fn mk_type_metadata(
989
1013
) )
990
1014
}
991
1015
// for structs, we record the name for information purposes
992
- TyKind :: RigidTy ( RigidTy :: Adt ( adt_def, _) ) if t. is_struct ( ) => {
1016
+ T ( Adt ( adt_def, _) ) if t. is_struct ( ) => {
993
1017
let name = adt_def. name ( ) ;
994
1018
Some ( ( k, StructType { name, adt_def } ) )
995
1019
}
996
- _ => None ,
1020
+ // for unions, we only record the name
1021
+ T ( Adt ( adt_def, _) ) if t. is_union ( ) => {
1022
+ let name = adt_def. name ( ) ;
1023
+ Some ( ( k, UnionType { name, adt_def } ) )
1024
+ }
1025
+ // encode str together with primitive types
1026
+ T ( Str ) => Some ( ( k, PrimitiveType ( Str ) ) ) ,
1027
+ // for arrays and slices, record element type and optional size
1028
+ T ( Array ( ty, ty_const) ) => Some ( ( k, ArrayType ( ty, Some ( ty_const) ) ) ) ,
1029
+ T ( Slice ( ty) ) => Some ( ( k, ArrayType ( ty, None ) ) ) ,
1030
+ // for raw pointers and references store the pointee type
1031
+ T ( RawPtr ( ty, _) ) => Some ( ( k, PtrType ( ty) ) ) ,
1032
+ T ( Ref ( _, ty, _) ) => Some ( ( k, RefType ( ty) ) ) ,
1033
+ // for tuples the element types are provided
1034
+ T ( Tuple ( tys) ) => Some ( ( k, TupleType { types : tys } ) ) ,
1035
+ // opaque function types (fun ptrs, closures, FnDef) are only provided to avoid dangling ty references
1036
+ T ( FnDef ( _, _) ) | T ( FnPtr ( _) ) | T ( Closure ( _, _) ) => Some ( ( k, FunType ( format ! ( "{}" , k) ) ) ) ,
1037
+ // other types are not provided either
1038
+ T ( Foreign ( _) )
1039
+ | T ( Pat ( _, _) )
1040
+ | T ( Coroutine ( _, _, _) )
1041
+ | T ( Dynamic ( _, _, _) )
1042
+ | T ( CoroutineWitness ( _, _) ) => None ,
1043
+ TyKind :: Alias ( _, _) | TyKind :: Param ( _) | TyKind :: Bound ( _, _) => None ,
1044
+ _ => None , // redundant because of first 4 cases, but rustc does not understand that
997
1045
}
998
1046
}
999
1047
0 commit comments