1
Fork 0

Implement associated_items api.

This commit is contained in:
makai410 2025-04-01 17:11:53 +08:00
parent eda7820be5
commit f9ef4563c2
9 changed files with 330 additions and 7 deletions

View file

@ -147,6 +147,14 @@ impl<'tcx> Tables<'tcx> {
stable_mir::ty::CoroutineWitnessDef(self.create_def_id(did))
}
pub fn assoc_def(&mut self, did: DefId) -> stable_mir::ty::AssocDef {
stable_mir::ty::AssocDef(self.create_def_id(did))
}
pub fn opaque_def(&mut self, did: DefId) -> stable_mir::ty::OpaqueDef {
stable_mir::ty::OpaqueDef(self.create_def_id(did))
}
pub fn prov(&mut self, aid: AllocId) -> stable_mir::ty::Prov {
stable_mir::ty::Prov(self.create_alloc_id(aid))
}

View file

@ -822,6 +822,21 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
let ty = un_op.internal(&mut *tables, tcx).ty(tcx, arg_internal);
ty.stable(&mut *tables)
}
fn associated_items(&self, def_id: stable_mir::DefId) -> stable_mir::AssocItems {
let mut tables = self.0.borrow_mut();
let tcx = tables.tcx;
let def_id = tables[def_id];
let assoc_items = if tcx.is_trait_alias(def_id) {
Vec::new()
} else {
tcx.associated_item_def_ids(def_id)
.iter()
.map(|did| tcx.associated_item(*did).stable(&mut *tables))
.collect()
};
assoc_items
}
}
pub(crate) struct TablesWrapper<'tcx>(pub RefCell<Tables<'tcx>>);

View file

@ -890,3 +890,63 @@ impl<'tcx> Stable<'tcx> for rustc_session::cstore::ForeignModule {
}
}
}
impl<'tcx> Stable<'tcx> for ty::AssocKind {
type T = stable_mir::ty::AssocKind;
fn stable(&self, _tables: &mut Tables<'_>) -> Self::T {
use stable_mir::ty::AssocKind;
match self {
ty::AssocKind::Const => AssocKind::Const,
ty::AssocKind::Fn => AssocKind::Fn,
ty::AssocKind::Type => AssocKind::Type,
}
}
}
impl<'tcx> Stable<'tcx> for ty::AssocItemContainer {
type T = stable_mir::ty::AssocItemContainer;
fn stable(&self, _tables: &mut Tables<'_>) -> Self::T {
use stable_mir::ty::AssocItemContainer;
match self {
ty::AssocItemContainer::Trait => AssocItemContainer::Trait,
ty::AssocItemContainer::Impl => AssocItemContainer::Impl,
}
}
}
impl<'tcx> Stable<'tcx> for ty::AssocItem {
type T = stable_mir::ty::AssocItem;
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
stable_mir::ty::AssocItem {
def_id: tables.assoc_def(self.def_id),
name: self.name.to_string(),
kind: self.kind.stable(tables),
container: self.container.stable(tables),
trait_item_def_id: self.trait_item_def_id.map(|did| tables.assoc_def(did)),
fn_has_self_parameter: self.fn_has_self_parameter,
opt_rpitit_info: self.opt_rpitit_info.map(|rpitit| rpitit.stable(tables)),
}
}
}
impl<'tcx> Stable<'tcx> for ty::ImplTraitInTraitData {
type T = stable_mir::ty::ImplTraitInTraitData;
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
use stable_mir::ty::ImplTraitInTraitData;
match self {
ty::ImplTraitInTraitData::Trait { fn_def_id, opaque_def_id } => {
ImplTraitInTraitData::Trait {
fn_def_id: tables.fn_def(*fn_def_id),
opaque_def_id: tables.opaque_def(*opaque_def_id),
}
}
ty::ImplTraitInTraitData::Impl { fn_def_id } => {
ImplTraitInTraitData::Impl { fn_def_id: tables.fn_def(*fn_def_id) }
}
}
}
}