Skip to content
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

Add module information to FullDef #1063

Merged
merged 2 commits into from
Oct 29, 2024
Merged
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
199 changes: 123 additions & 76 deletions frontend/exporter/src/types/new/full_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ where
#[derive_group(Serializers)]
#[derive(Clone, Debug, JsonSchema)]
pub enum FullDefKind<Body> {
// Type namespace
Mod,
// Types
/// Refers to the struct definition, [`DefKind::Ctor`] refers to its constructor if it exists.
Struct {
#[value(s.base().tcx.generics_of(s.owner_id()).sinto(s))]
Expand All @@ -119,39 +118,6 @@ pub enum FullDefKind<Body> {
#[value(s.base().tcx.adt_def(s.owner_id()).sinto(s))]
def: AdtDef,
},
/// Refers to the variant definition, [`DefKind::Ctor`] refers to its constructor if it exists.
Variant,
Trait {
#[value(s.base().tcx.generics_of(s.owner_id()).sinto(s))]
generics: TyGenerics,
#[value(get_generic_predicates(s, s.owner_id()))]
predicates: GenericPredicates,
/// The special `Self: Trait` clause.
#[value({
use ty::Upcast;
let tcx = s.base().tcx;
let pred: ty::TraitPredicate =
crate::traits::self_predicate(tcx, s.owner_id())
.unwrap()
.no_bound_vars()
.unwrap()
.upcast(tcx);
pred.sinto(s)
})]
self_predicate: TraitPredicate,
/// Associated items, in definition order.
#[value(
s
.base()
.tcx
.associated_items(s.owner_id())
.in_definition_order()
.map(|assoc| (assoc, assoc.def_id))
.collect::<Vec<_>>()
.sinto(s)
)]
items: Vec<(AssocItem, Arc<FullDef>)>,
},
/// Type alias: `type Foo = Bar;`
TyAlias {
#[value(s.base().tcx.generics_of(s.owner_id()).sinto(s))]
Expand All @@ -170,8 +136,6 @@ pub enum FullDefKind<Body> {
},
/// Type from an `extern` block.
ForeignTy,
/// Trait alias: `trait IntIterator = Iterator<Item = i32>;`
TraitAlias,
/// Associated type: `trait MyTrait { type Assoc; }`
AssocTy {
#[value(s.base().tcx.parent(s.owner_id()).sinto(s))]
Expand All @@ -192,10 +156,65 @@ pub enum FullDefKind<Body> {
})]
value: Option<Ty>,
},
/// Type parameter: the `T` in `struct Vec<T> { ... }`
TyParam,
/// Opaque type, aka `impl Trait`.
OpaqueTy,

// Value namespace
// Traits
Trait {
#[value(s.base().tcx.generics_of(s.owner_id()).sinto(s))]
generics: TyGenerics,
#[value(get_generic_predicates(s, s.owner_id()))]
predicates: GenericPredicates,
/// The special `Self: Trait` clause.
#[value({
use ty::Upcast;
let tcx = s.base().tcx;
let pred: ty::TraitPredicate =
crate::traits::self_predicate(tcx, s.owner_id())
.unwrap()
.no_bound_vars()
.unwrap()
.upcast(tcx);
pred.sinto(s)
})]
self_predicate: TraitPredicate,
/// Associated items, in definition order.
#[value(
s
.base()
.tcx
.associated_items(s.owner_id())
.in_definition_order()
.map(|assoc| (assoc, assoc.def_id))
.collect::<Vec<_>>()
.sinto(s)
)]
items: Vec<(AssocItem, Arc<FullDef>)>,
},
/// Trait alias: `trait IntIterator = Iterator<Item = i32>;`
TraitAlias,
Impl {
#[value(s.base().tcx.generics_of(s.owner_id()).sinto(s))]
generics: TyGenerics,
#[value(get_generic_predicates(s, s.owner_id()))]
predicates: GenericPredicates,
#[value(s.base().tcx.impl_subject(s.owner_id()).instantiate_identity().sinto(s))]
impl_subject: ImplSubject,
/// Associated items, in definition order.
#[value(
s
.base()
.tcx
.associated_items(s.owner_id())
.in_definition_order()
.map(|assoc| (assoc, assoc.def_id))
.collect::<Vec<_>>()
.sinto(s)
)]
items: Vec<(AssocItem, Arc<FullDef>)>,
},

// Functions
Fn {
#[value(s.base().tcx.generics_of(s.owner_id()).sinto(s))]
generics: TyGenerics,
Expand Down Expand Up @@ -245,6 +264,8 @@ pub enum FullDefKind<Body> {
})]
args: ClosureArgs,
},

// Constants
Const {
#[value(s.base().tcx.generics_of(s.owner_id()).sinto(s))]
generics: TyGenerics,
Expand All @@ -270,6 +291,10 @@ pub enum FullDefKind<Body> {
#[value(s.owner_id().as_local().map(|ldid| Body::body(ldid, s)))]
body: Option<Body>,
},
/// Anonymous constant, e.g. the `1 + 2` in `[u8; 1 + 2]`
AnonConst,
/// An inline constant, e.g. `const { 1 + 2 }`
InlineConst,
Static {
/// Whether it's a `unsafe static`, `safe static` (inside extern only) or just a `static`.
safety: Safety,
Expand All @@ -286,51 +311,41 @@ pub enum FullDefKind<Body> {
#[value(s.owner_id().as_local().map(|ldid| Body::body(ldid, s)))]
body: Option<Body>,
},
/// Constant generic parameter: `struct Foo<const N: usize> { ... }`
ConstParam,
/// Refers to the struct or enum variant's constructor.
Ctor(CtorOf, CtorKind),

// Macro namespace
Macro(MacroKind),

// Not namespaced (or they are, but we don't treat them so)
// Crates and modules
ExternCrate,
Use,
Mod {
#[value(get_mod_children(s.base().tcx, s.owner_id()).sinto(s))]
items: Vec<DefId>,
},
/// An `extern` block.
ForeignMod,
/// Anonymous constant, e.g. the `1 + 2` in `[u8; 1 + 2]`
AnonConst,
/// An inline constant, e.g. `const { 1 + 2 }`
InlineConst,
/// Opaque type, aka `impl Trait`.
OpaqueTy,
Impl {
#[value(s.base().tcx.generics_of(s.owner_id()).sinto(s))]
generics: TyGenerics,
#[value(get_generic_predicates(s, s.owner_id()))]
predicates: GenericPredicates,
#[value(s.base().tcx.impl_subject(s.owner_id()).instantiate_identity().sinto(s))]
impl_subject: ImplSubject,
/// Associated items, in definition order.
#[value(
s
.base()
.tcx
.associated_items(s.owner_id())
.in_definition_order()
.map(|assoc| (assoc, assoc.def_id))
.collect::<Vec<_>>()
.sinto(s)
)]
items: Vec<(AssocItem, Arc<FullDef>)>,
ForeignMod {
#[value(get_mod_children(s.base().tcx, s.owner_id()).sinto(s))]
items: Vec<DefId>,
},

// Type-level parameters
/// Type parameter: the `T` in `struct Vec<T> { ... }`
TyParam,
/// Constant generic parameter: `struct Foo<const N: usize> { ... }`
ConstParam,
/// Lifetime parameter: the `'a` in `struct Foo<'a> { ... }`
LifetimeParam,

// ADT parts
/// Refers to the variant definition, [`DefKind::Ctor`] refers to its constructor if it exists.
Variant,
/// Refers to the struct or enum variant's constructor.
Ctor(CtorOf, CtorKind),
/// A field in a struct, enum or union. e.g.
/// - `bar` in `struct Foo { bar: u8 }`
/// - `Foo::Bar::0` in `enum Foo { Bar(u8) }`
Field,
/// Lifetime parameter: the `'a` in `struct Foo<'a> { ... }`
LifetimeParam,

// Others
/// Macros
Macro(MacroKind),
/// A use of `global_asm!`.
GlobalAsm,
/// A synthetic coroutine body created by the lowering of a coroutine-closure, such as an async
Expand Down Expand Up @@ -492,6 +507,38 @@ fn get_def_attrs<'tcx>(
}
}

/// Gets the children of a module.
#[cfg(feature = "rustc")]
fn get_mod_children<'tcx>(tcx: ty::TyCtxt<'tcx>, def_id: RDefId) -> Vec<RDefId> {
match def_id.as_local() {
Some(ldid) => match tcx.hir_node_by_def_id(ldid) {
rustc_hir::Node::Crate(m)
| rustc_hir::Node::Item(&rustc_hir::Item {
kind: rustc_hir::ItemKind::Mod(m),
..
}) => m
.item_ids
.iter()
.map(|item_id| item_id.owner_id.to_def_id())
.collect(),

rustc_hir::Node::Item(rustc_hir::Item {
kind: rustc_hir::ItemKind::ForeignMod { items, .. },
..
}) => items
.iter()
.map(|foreign_item_ref| foreign_item_ref.id.owner_id.to_def_id())
.collect(),
node => panic!("DefKind::Module is an unexpected node: {node:?}"),
},
None => tcx
.module_children(def_id)
.iter()
.map(|child| child.res.def_id())
.collect(),
}
}

/// This normalizes trait clauses before calling `sinto` on them. This is a bit of a hack required
/// by charon for now. We can't normalize all clauses as this would lose region information in
/// outlives clauses.
Expand Down
Loading