Skip to content

More type metadata #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ jobs:

- name: 'Run smir integration tests'
run: |
which jq
jq --version
make integration-test

ui-tests:
Expand Down
60 changes: 54 additions & 6 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ use serde::{Serialize, Serializer};
use stable_mir::{
mir::mono::{Instance, InstanceKind, MonoItem},
mir::{alloc::AllocId, visit::MirVisitor, Body, LocalDecl, Rvalue, Terminator, TerminatorKind},
ty::{AdtDef, Allocation, ConstDef, ForeignItemKind, IndexedVal, RigidTy, TyKind, VariantIdx},
ty::{
AdtDef, Allocation, ConstDef, ForeignItemKind, IndexedVal, RigidTy, TyConstKind, TyKind,
VariantIdx,
},
CrateDef, CrateItem, ItemKind,
};

Expand Down Expand Up @@ -627,7 +630,15 @@ fn collect_ty(val_collector: &mut InternedValueCollector, val: stable_mir::ty::T
.is_some()
{
match val.kind() {
RigidTy(Array(ty, _) | Pat(ty, _) | Slice(ty) | RawPtr(ty, _) | Ref(_, ty, _)) => {
RigidTy(Array(ty, ty_const)) => {
collect_ty(val_collector, ty);
match ty_const.kind() {
TyConstKind::Value(ty, _) => collect_ty(val_collector, *ty),
TyConstKind::ZSTValue(ty) => collect_ty(val_collector, *ty),
_ => (),
}
}
RigidTy(Pat(ty, _) | Slice(ty) | RawPtr(ty, _) | Ref(_, ty, _)) => {
collect_ty(val_collector, ty)
}
RigidTy(Tuple(tys)) => collect_vec_tys(val_collector, tys),
Expand Down Expand Up @@ -960,19 +971,32 @@ pub enum TypeMetadata {
name: String,
adt_def: AdtDef,
},
UnionType {
name: String,
adt_def: AdtDef,
},
ArrayType(stable_mir::ty::Ty, Option<stable_mir::ty::TyConst>),
PtrType(stable_mir::ty::Ty),
RefType(stable_mir::ty::Ty),
TupleType {
types: Vec<stable_mir::ty::Ty>,
},
FunType(String),
}

fn mk_type_metadata(
tcx: TyCtxt<'_>,
k: stable_mir::ty::Ty,
t: TyKind,
) -> Option<(stable_mir::ty::Ty, TypeMetadata)> {
use stable_mir::ty::RigidTy::*;
use TyKind::RigidTy as T;
use TypeMetadata::*;
match t {
TyKind::RigidTy(prim_type) if t.is_primitive() => Some((k, PrimitiveType(prim_type))),
T(prim_type) if t.is_primitive() => Some((k, PrimitiveType(prim_type))),
// for enums, we need a mapping of variantIdx to discriminant
// this requires access to the internals and is not provided as an interface function at the moment
TyKind::RigidTy(RigidTy::Adt(adt_def, _)) if t.is_enum() => {
T(Adt(adt_def, _)) if t.is_enum() => {
let adt_internal = rustc_internal::internal(tcx, adt_def);
let discriminants = adt_internal
.discriminants(tcx)
Expand All @@ -989,11 +1013,35 @@ fn mk_type_metadata(
))
}
// for structs, we record the name for information purposes
TyKind::RigidTy(RigidTy::Adt(adt_def, _)) if t.is_struct() => {
T(Adt(adt_def, _)) if t.is_struct() => {
let name = adt_def.name();
Some((k, StructType { name, adt_def }))
}
_ => None,
// for unions, we only record the name
T(Adt(adt_def, _)) if t.is_union() => {
let name = adt_def.name();
Some((k, UnionType { name, adt_def }))
}
// encode str together with primitive types
T(Str) => Some((k, PrimitiveType(Str))),
// for arrays and slices, record element type and optional size
T(Array(ty, ty_const)) => Some((k, ArrayType(ty, Some(ty_const)))),
T(Slice(ty)) => Some((k, ArrayType(ty, None))),
// for raw pointers and references store the pointee type
T(RawPtr(ty, _)) => Some((k, PtrType(ty))),
T(Ref(_, ty, _)) => Some((k, RefType(ty))),
// for tuples the element types are provided
T(Tuple(tys)) => Some((k, TupleType { types: tys })),
// opaque function types (fun ptrs, closures, FnDef) are only provided to avoid dangling ty references
T(FnDef(_, _)) | T(FnPtr(_)) | T(Closure(_, _)) => Some((k, FunType(format!("{}", k)))),
// other types are not provided either
T(Foreign(_))
| T(Pat(_, _))
| T(Coroutine(_, _, _))
| T(Dynamic(_, _, _))
| T(CoroutineWitness(_, _)) => None,
TyKind::Alias(_, _) | TyKind::Param(_) | TyKind::Bound(_, _) => None,
_ => None, // redundant because of first 4 cases, but rustc does not understand that
}
}

Expand Down
44 changes: 23 additions & 21 deletions tests/integration/normalise-filter.jq
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
# Remove the hashes at the end of mangled names
.functions = ( [ .functions[] | if .[1].NormalSym then .[1].NormalSym = .[1].NormalSym[:-17] else . end ] )
| .items = ( [ .items[] | if .symbol_name then .symbol_name = .symbol_name[:-17] else . end ] )
# delete unstable alloc, function, and type IDs
| .allocs = ( [ .allocs[] ] | map(del(.[0])) )
| .functions = ( [ .functions[] ] | map(del(.[0])) )
| .types = ( [ .types[] ] | map(del(.[0])) )
|
# Apply the normalisation filter
{ allocs:
( [ .allocs[] ]
# delete unstable alloc ID
| map(del(.[0]))
),
functions:
( [ .functions[] ]
# delete unstable function ID
| map(del(.[0]))
),
items:
( [ .items[] ]
),
types:
( [ .types[] ]
# delete unstable Ty ID (int, first in list)
| map(del(.[0]))
# delete unstable adt_def from Struct and Enum
| map(del(.[0].StructType.adt_def))
| map(del(.[0].EnumType.adt_def))
)
{ allocs: .allocs,
functions: .functions,
items: .items,
types: ( [
# sort by constructors and remove unstable IDs within each
( .types | map(select(.[0].PrimitiveType)) ),
# delete unstable adt_ref IDs
( .types | map(select(.[0].EnumType) | del(.[0].EnumType.adt_def)) ),
( .types | map(select(.[0].StructType) | del(.[0].StructType.adt_def)) ),
( .types | map(select(.[0].UnionType) | del(.[0].UnionType.adt_def)) ),
# delete unstable Ty IDs for arrays and tuples
( .types | map(select(.[0].ArrayType) | del(.[0].ArrayType[0])) ),
( .types | map(select(.[0].TupleType) | .[0].TupleType.types = "elided") ),
# replace unstable Ty IDs for references by zero
( .types | map(select(.[0].PtrType) | .[0].PtrType = "elided") ),
( .types | map(select(.[0].RefType) | .[0].RefType = "elided") ),
# keep function type strings
( .types | map(select(.[0].FunType) | .[0].FunType = "elided") )
] | flatten(1) )
}
Loading