Skip to content

Commit d9e2258

Browse files
jbertholdgtrepta
andauthored
More type metadata (#69)
Emit metadata for all types that matter, in particular reference types and array types. Function types are only referred to as strings (elided in the golden tests), some other types are still not considered but they don't seem relevant to our purpose. Also included: * add type of `TyConst` in `RigidTy::Array` case to collected types in `collect_ty` * optionally keep json files after running `test-ui` | RigidTy | Metadata | Information | | |--------------------------------------------------|---------------|-----------------|-----------| | Bool | PrimitiveType | RigidTy | existed | | Char | PrimitiveType | RigidTy | existed | | Int(IntTy) | PrimitiveType | RigidTy | existed | | Uint(UintTy) | PrimitiveType | RigidTy | existed | | Float(FloatTy) | PrimitiveType | RigidTy | existed | | Adt(AdtDef, GenericArgs) | EnumType | | existed | | | StructType | | existed | | | UnionType | | ADDED | | Foreign(ForeignDef) | | | | | Str | PrimitiveType | RigidTy | ADDED | | Array(Ty, TyConst) | ArrayType | ElemType, len | ADDED | | Pat(Ty, Pattern) | | | | | Slice(Ty) | ArrayType | ElemType | ADDED | | RawPtr(Ty, Mutability) | PtrType | PointeeType | ADDED | | Ref(Region, Ty, Mutability) | RefType | PointeeType | ADDED | | FnDef(FnDef, GenericArgs) | FunType | type string only| ADDED | | FnPtr(PolyFnSig) | FunType | type string only| ADDED | | Closure(ClosureDef, GenericArgs) | FunType | type string only| ADDED | | Coroutine(CoroutineDef, GenericArgs, Movability) | | | | | Dynamic(Vec<Binder<Ex-Pred>>, Region, DynKind) | | | | | Never | | | ? | | Tuple(Vec<Ty>) | TupleType | Component types | ADDED | | CoroutineWitness(..) | | | | --------- Co-authored-by: gtrepta <[email protected]>
1 parent a246c48 commit d9e2258

29 files changed

+3380
-274
lines changed

.github/workflows/test.yml

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ jobs:
6666
6767
- name: 'Run smir integration tests'
6868
run: |
69+
which jq
70+
jq --version
6971
make integration-test
7072
7173
ui-tests:

src/printer.rs

+54-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ use serde::{Serialize, Serializer};
2424
use stable_mir::{
2525
mir::mono::{Instance, InstanceKind, MonoItem},
2626
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+
},
2831
CrateDef, CrateItem, ItemKind,
2932
};
3033

@@ -627,7 +630,15 @@ fn collect_ty(val_collector: &mut InternedValueCollector, val: stable_mir::ty::T
627630
.is_some()
628631
{
629632
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, _)) => {
631642
collect_ty(val_collector, ty)
632643
}
633644
RigidTy(Tuple(tys)) => collect_vec_tys(val_collector, tys),
@@ -960,19 +971,32 @@ pub enum TypeMetadata {
960971
name: String,
961972
adt_def: AdtDef,
962973
},
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),
963985
}
964986

965987
fn mk_type_metadata(
966988
tcx: TyCtxt<'_>,
967989
k: stable_mir::ty::Ty,
968990
t: TyKind,
969991
) -> Option<(stable_mir::ty::Ty, TypeMetadata)> {
992+
use stable_mir::ty::RigidTy::*;
993+
use TyKind::RigidTy as T;
970994
use TypeMetadata::*;
971995
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))),
973997
// for enums, we need a mapping of variantIdx to discriminant
974998
// 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() => {
9761000
let adt_internal = rustc_internal::internal(tcx, adt_def);
9771001
let discriminants = adt_internal
9781002
.discriminants(tcx)
@@ -989,11 +1013,35 @@ fn mk_type_metadata(
9891013
))
9901014
}
9911015
// 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() => {
9931017
let name = adt_def.name();
9941018
Some((k, StructType { name, adt_def }))
9951019
}
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
9971045
}
9981046
}
9991047

tests/integration/normalise-filter.jq

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
# Remove the hashes at the end of mangled names
22
.functions = ( [ .functions[] | if .[1].NormalSym then .[1].NormalSym = .[1].NormalSym[:-17] else . end ] )
33
| .items = ( [ .items[] | if .symbol_name then .symbol_name = .symbol_name[:-17] else . end ] )
4+
# delete unstable alloc, function, and type IDs
5+
| .allocs = ( [ .allocs[] ] | map(del(.[0])) )
6+
| .functions = ( [ .functions[] ] | map(del(.[0])) )
7+
| .types = ( [ .types[] ] | map(del(.[0])) )
48
|
59
# Apply the normalisation filter
6-
{ allocs:
7-
( [ .allocs[] ]
8-
# delete unstable alloc ID
9-
| map(del(.[0]))
10-
),
11-
functions:
12-
( [ .functions[] ]
13-
# delete unstable function ID
14-
| map(del(.[0]))
15-
),
16-
items:
17-
( [ .items[] ]
18-
),
19-
types:
20-
( [ .types[] ]
21-
# delete unstable Ty ID (int, first in list)
22-
| map(del(.[0]))
23-
# delete unstable adt_def from Struct and Enum
24-
| map(del(.[0].StructType.adt_def))
25-
| map(del(.[0].EnumType.adt_def))
26-
)
10+
{ allocs: .allocs,
11+
functions: .functions,
12+
items: .items,
13+
types: ( [
14+
# sort by constructors and remove unstable IDs within each
15+
( .types | map(select(.[0].PrimitiveType)) ),
16+
# delete unstable adt_ref IDs
17+
( .types | map(select(.[0].EnumType) | del(.[0].EnumType.adt_def)) ),
18+
( .types | map(select(.[0].StructType) | del(.[0].StructType.adt_def)) ),
19+
( .types | map(select(.[0].UnionType) | del(.[0].UnionType.adt_def)) ),
20+
# delete unstable Ty IDs for arrays and tuples
21+
( .types | map(select(.[0].ArrayType) | del(.[0].ArrayType[0])) ),
22+
( .types | map(select(.[0].TupleType) | .[0].TupleType.types = "elided") ),
23+
# replace unstable Ty IDs for references by zero
24+
( .types | map(select(.[0].PtrType) | .[0].PtrType = "elided") ),
25+
( .types | map(select(.[0].RefType) | .[0].RefType = "elided") ),
26+
# keep function type strings
27+
( .types | map(select(.[0].FunType) | .[0].FunType = "elided") )
28+
] | flatten(1) )
2729
}

0 commit comments

Comments
 (0)