Skip to content

Commit eda722c

Browse files
committed
Add associated_items api.
- Add `AssocItem` and related things to `stable_mir::ty`. - Implement `Display` for `AssocKind`.
1 parent eda7820 commit eda722c

File tree

6 files changed

+139
-4
lines changed

6 files changed

+139
-4
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ impl<'tcx> Tables<'tcx> {
147147
stable_mir::ty::CoroutineWitnessDef(self.create_def_id(did))
148148
}
149149

150+
pub fn assoc_def(&mut self, did: DefId) -> stable_mir::ty::AssocDef {
151+
stable_mir::ty::AssocDef(self.create_def_id(did))
152+
}
153+
154+
pub fn opaque_def(&mut self, did: DefId) -> stable_mir::ty::OpaqueDef {
155+
stable_mir::ty::OpaqueDef(self.create_def_id(did))
156+
}
157+
150158
pub fn prov(&mut self, aid: AllocId) -> stable_mir::ty::Prov {
151159
stable_mir::ty::Prov(self.create_alloc_id(aid))
152160
}

compiler/rustc_smir/src/rustc_smir/context.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,27 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
822822
let ty = un_op.internal(&mut *tables, tcx).ty(tcx, arg_internal);
823823
ty.stable(&mut *tables)
824824
}
825+
826+
fn associated_items(&self, def_id: stable_mir::DefId) -> stable_mir::ty::AssocItems {
827+
let mut tables = self.0.borrow_mut();
828+
let tcx = tables.tcx;
829+
let def_id = tables[def_id];
830+
let assoc_items = if tcx.is_trait_alias(def_id) {
831+
stable_mir::ty::AssocItems { items: Vec::new() }
832+
} else {
833+
let items: Vec<(Symbol, stable_mir::ty::AssocItem)> = tcx
834+
.associated_item_def_ids(def_id)
835+
.iter()
836+
.map(|did| {
837+
let item = tcx.associated_item(*did).stable(&mut *tables);
838+
(item.name.clone(), item)
839+
})
840+
.collect();
841+
842+
stable_mir::ty::AssocItems { items }
843+
};
844+
assoc_items
845+
}
825846
}
826847

827848
pub(crate) struct TablesWrapper<'tcx>(pub RefCell<Tables<'tcx>>);

compiler/stable_mir/src/compiler_interface.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::mir::mono::{Instance, InstanceDef, StaticDef};
1212
use crate::mir::{BinOp, Body, Place, UnOp};
1313
use crate::target::MachineInfo;
1414
use crate::ty::{
15-
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, FieldDef, FnDef, ForeignDef,
15+
AdtDef, AdtKind, Allocation, AssocItems, ClosureDef, ClosureKind, FieldDef, FnDef, ForeignDef,
1616
ForeignItemKind, ForeignModule, ForeignModuleDef, GenericArgs, GenericPredicates, Generics,
1717
ImplDef, ImplTrait, IntrinsicDef, LineInfo, MirConst, PolyFnSig, RigidTy, Span, TraitDecl,
1818
TraitDef, Ty, TyConst, TyConstId, TyKind, UintTy, VariantDef,
@@ -251,6 +251,9 @@ pub trait Context {
251251

252252
/// Get the resulting type of unary operation.
253253
fn unop_ty(&self, un_op: UnOp, arg: Ty) -> Ty;
254+
255+
/// Get all associated items of a definition.
256+
fn associated_items(&self, def_id: DefId) -> AssocItems;
254257
}
255258

256259
// A thread local variable that stores a pointer to the tables mapping between TyCtxt

compiler/stable_mir/src/crate_def.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use serde::Serialize;
55

6-
use crate::ty::{GenericArgs, Span, Ty};
6+
use crate::ty::{AssocItems, GenericArgs, Span, Ty};
77
use crate::{Crate, Symbol, with};
88

99
/// A unique identification number for each item accessible for the current compilation unit.
@@ -103,6 +103,14 @@ pub trait CrateDefType: CrateDef {
103103
}
104104
}
105105

106+
/// A trait for retrieving all items from a definition within a crate.
107+
pub trait CrateDefItems: CrateDef {
108+
/// Retrieve all associated items from a definition.
109+
fn associated_items(&self) -> AssocItems {
110+
with(|cx| cx.associated_items(self.def_id()))
111+
}
112+
}
113+
106114
#[derive(Clone, Debug, PartialEq, Eq)]
107115
pub struct Attribute {
108116
value: String,
@@ -158,3 +166,9 @@ macro_rules! crate_def_with_ty {
158166
impl CrateDefType for $name {}
159167
};
160168
}
169+
170+
macro_rules! impl_crate_def_items {
171+
( $name:ident $(;)? ) => {
172+
impl CrateDefItems for $name {}
173+
};
174+
}

compiler/stable_mir/src/mir/pretty.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::{AggregateKind, AssertMessage, BinOp, BorrowKind, FakeBorrowKind, Ter
99
use crate::mir::{
1010
Operand, Place, RawPtrKind, Rvalue, StatementKind, UnwindAction, VarDebugInfoContents,
1111
};
12-
use crate::ty::{AdtKind, IndexedVal, MirConst, Ty, TyConst};
12+
use crate::ty::{AdtKind, AssocKind, IndexedVal, MirConst, Ty, TyConst};
1313
use crate::{Body, CrateDef, Mutability, with};
1414

1515
impl Display for Ty {
@@ -18,6 +18,16 @@ impl Display for Ty {
1818
}
1919
}
2020

21+
impl Display for AssocKind {
22+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
23+
match self {
24+
AssocKind::Fn => write!(f, "method"),
25+
AssocKind::Const => write!(f, "associated const"),
26+
AssocKind::Type => write!(f, "associated type"),
27+
}
28+
}
29+
}
30+
2131
impl Debug for Place {
2232
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
2333
with(|ctx| write!(f, "{}", ctx.place_pretty(self)))

compiler/stable_mir/src/ty.rs

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::Serialize;
66
use super::mir::{Body, Mutability, Safety};
77
use super::{DefId, Error, Symbol, with};
88
use crate::abi::{FnAbi, Layout};
9-
use crate::crate_def::{CrateDef, CrateDefType};
9+
use crate::crate_def::{CrateDef, CrateDefItems, CrateDefType};
1010
use crate::mir::alloc::{AllocId, read_target_int, read_target_uint};
1111
use crate::mir::mono::StaticDef;
1212
use crate::target::MachineInfo;
@@ -773,6 +773,15 @@ pub enum AdtKind {
773773
Struct,
774774
}
775775

776+
impl CrateDefItems for AdtDef {
777+
fn associated_items(&self) -> AssocItems {
778+
match &self.kind() {
779+
AdtKind::Enum | AdtKind::Struct => with(|cx| cx.associated_items(self.def_id())),
780+
AdtKind::Union => AssocItems { items: Vec::new() },
781+
}
782+
}
783+
}
784+
776785
impl AdtDef {
777786
pub fn kind(&self) -> AdtKind {
778787
with(|cx| cx.adt_kind(*self))
@@ -910,6 +919,10 @@ crate_def! {
910919
pub TraitDef;
911920
}
912921

922+
impl_crate_def_items! {
923+
TraitDef;
924+
}
925+
913926
impl TraitDef {
914927
pub fn declaration(trait_def: &TraitDef) -> TraitDecl {
915928
with(|cx| cx.trait_decl(trait_def))
@@ -932,6 +945,10 @@ crate_def! {
932945
pub ImplDef;
933946
}
934947

948+
impl_crate_def_items! {
949+
ImplDef;
950+
}
951+
935952
impl ImplDef {
936953
/// Retrieve information about this implementation.
937954
pub fn trait_impl(&self) -> ImplTrait {
@@ -1555,3 +1572,65 @@ index_impl!(Span);
15551572
pub struct VariantIdx(usize);
15561573

15571574
index_impl!(VariantIdx);
1575+
1576+
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
1577+
pub struct AssocItems {
1578+
pub items: Vec<(Symbol, AssocItem)>,
1579+
}
1580+
1581+
crate_def! {
1582+
/// Hold infomation about an Opaque definition, particularly useful in `RPITIT`.
1583+
#[derive(Serialize)]
1584+
pub OpaqueDef;
1585+
}
1586+
1587+
crate_def! {
1588+
#[derive(Serialize)]
1589+
pub AssocDef;
1590+
}
1591+
1592+
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
1593+
pub struct AssocItem {
1594+
pub def_id: AssocDef,
1595+
pub name: Symbol,
1596+
pub kind: AssocKind,
1597+
pub container: AssocItemContainer,
1598+
1599+
/// If this is an item in an impl of a trait then this is the `DefId` of
1600+
/// the associated item on the trait that this implements.
1601+
pub trait_item_def_id: Option<AssocDef>,
1602+
1603+
/// Whether this is a method with an explicit self
1604+
/// as its first parameter, allowing method calls.
1605+
pub fn_has_self_parameter: bool,
1606+
1607+
/// `Some` if the associated item (an associated type) comes from the
1608+
/// return-position `impl Trait` in trait desugaring. The `ImplTraitInTraitData`
1609+
/// provides additional information about its source.
1610+
pub opt_rpitit_info: Option<ImplTraitInTraitData>,
1611+
}
1612+
1613+
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
1614+
pub enum AssocKind {
1615+
Const,
1616+
Fn,
1617+
Type,
1618+
}
1619+
1620+
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
1621+
pub enum AssocItemContainer {
1622+
Trait,
1623+
Impl,
1624+
}
1625+
1626+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, Serialize)]
1627+
pub enum ImplTraitInTraitData {
1628+
Trait { fn_def_id: FnDef, opaque_def_id: OpaqueDef },
1629+
Impl { fn_def_id: FnDef },
1630+
}
1631+
1632+
impl AssocItem {
1633+
pub fn is_impl_trait_in_trait(&self) -> bool {
1634+
self.opt_rpitit_info.is_some()
1635+
}
1636+
}

0 commit comments

Comments
 (0)