Skip to content

Commit 93f7583

Browse files
authored
Rollup merge of rust-lang#138826 - makai410:assoc-items, r=celinval
StableMIR: Add `associated_items`. Resolves: rust-lang/project-stable-mir#87
2 parents 338b878 + f9ef456 commit 93f7583

File tree

9 files changed

+330
-7
lines changed

9 files changed

+330
-7
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

+8
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

+15
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,21 @@ 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::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+
Vec::new()
832+
} else {
833+
tcx.associated_item_def_ids(def_id)
834+
.iter()
835+
.map(|did| tcx.associated_item(*did).stable(&mut *tables))
836+
.collect()
837+
};
838+
assoc_items
839+
}
825840
}
826841

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

compiler/rustc_smir/src/rustc_smir/convert/ty.rs

+60
Original file line numberDiff line numberDiff line change
@@ -890,3 +890,63 @@ impl<'tcx> Stable<'tcx> for rustc_session::cstore::ForeignModule {
890890
}
891891
}
892892
}
893+
894+
impl<'tcx> Stable<'tcx> for ty::AssocKind {
895+
type T = stable_mir::ty::AssocKind;
896+
897+
fn stable(&self, _tables: &mut Tables<'_>) -> Self::T {
898+
use stable_mir::ty::AssocKind;
899+
match self {
900+
ty::AssocKind::Const => AssocKind::Const,
901+
ty::AssocKind::Fn => AssocKind::Fn,
902+
ty::AssocKind::Type => AssocKind::Type,
903+
}
904+
}
905+
}
906+
907+
impl<'tcx> Stable<'tcx> for ty::AssocItemContainer {
908+
type T = stable_mir::ty::AssocItemContainer;
909+
910+
fn stable(&self, _tables: &mut Tables<'_>) -> Self::T {
911+
use stable_mir::ty::AssocItemContainer;
912+
match self {
913+
ty::AssocItemContainer::Trait => AssocItemContainer::Trait,
914+
ty::AssocItemContainer::Impl => AssocItemContainer::Impl,
915+
}
916+
}
917+
}
918+
919+
impl<'tcx> Stable<'tcx> for ty::AssocItem {
920+
type T = stable_mir::ty::AssocItem;
921+
922+
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
923+
stable_mir::ty::AssocItem {
924+
def_id: tables.assoc_def(self.def_id),
925+
name: self.name.to_string(),
926+
kind: self.kind.stable(tables),
927+
container: self.container.stable(tables),
928+
trait_item_def_id: self.trait_item_def_id.map(|did| tables.assoc_def(did)),
929+
fn_has_self_parameter: self.fn_has_self_parameter,
930+
opt_rpitit_info: self.opt_rpitit_info.map(|rpitit| rpitit.stable(tables)),
931+
}
932+
}
933+
}
934+
935+
impl<'tcx> Stable<'tcx> for ty::ImplTraitInTraitData {
936+
type T = stable_mir::ty::ImplTraitInTraitData;
937+
938+
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
939+
use stable_mir::ty::ImplTraitInTraitData;
940+
match self {
941+
ty::ImplTraitInTraitData::Trait { fn_def_id, opaque_def_id } => {
942+
ImplTraitInTraitData::Trait {
943+
fn_def_id: tables.fn_def(*fn_def_id),
944+
opaque_def_id: tables.opaque_def(*opaque_def_id),
945+
}
946+
}
947+
ty::ImplTraitInTraitData::Impl { fn_def_id } => {
948+
ImplTraitInTraitData::Impl { fn_def_id: tables.fn_def(*fn_def_id) }
949+
}
950+
}
951+
}
952+
}

compiler/stable_mir/src/compiler_interface.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use crate::ty::{
1818
TraitDef, Ty, TyConst, TyConstId, TyKind, UintTy, VariantDef,
1919
};
2020
use crate::{
21-
Crate, CrateItem, CrateItems, CrateNum, DefId, Error, Filename, ImplTraitDecls, ItemKind,
22-
Symbol, TraitDecls, mir,
21+
AssocItems, Crate, CrateItem, CrateItems, CrateNum, DefId, Error, Filename, ImplTraitDecls,
22+
ItemKind, Symbol, TraitDecls, mir,
2323
};
2424

2525
/// This trait defines the interface between stable_mir and the Rust compiler.
@@ -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

+15-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use serde::Serialize;
55

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

99
/// A unique identification number for each item accessible for the current compilation unit.
1010
#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize)]
@@ -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/lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ use std::{fmt, io};
2323
use serde::Serialize;
2424

2525
use crate::compiler_interface::with;
26-
pub use crate::crate_def::{CrateDef, CrateDefType, DefId};
26+
pub use crate::crate_def::{CrateDef, CrateDefItems, CrateDefType, DefId};
2727
pub use crate::error::*;
2828
use crate::mir::mono::StaticDef;
2929
use crate::mir::{Body, Mutability};
30-
use crate::ty::{FnDef, ForeignModuleDef, ImplDef, IndexedVal, Span, TraitDef, Ty};
30+
use crate::ty::{AssocItem, FnDef, ForeignModuleDef, ImplDef, IndexedVal, Span, TraitDef, Ty};
3131

3232
pub mod abi;
3333
#[macro_use]
@@ -71,6 +71,9 @@ pub type TraitDecls = Vec<TraitDef>;
7171
/// A list of impl trait decls.
7272
pub type ImplTraitDecls = Vec<ImplDef>;
7373

74+
/// A list of associated items.
75+
pub type AssocItems = Vec<AssocItem>;
76+
7477
/// Holds information about a crate.
7578
#[derive(Clone, PartialEq, Eq, Debug, Serialize)]
7679
pub struct Crate {

compiler/stable_mir/src/mir/pretty.rs

+11-1
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

+66-1
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;
@@ -910,6 +910,10 @@ crate_def! {
910910
pub TraitDef;
911911
}
912912

913+
impl_crate_def_items! {
914+
TraitDef;
915+
}
916+
913917
impl TraitDef {
914918
pub fn declaration(trait_def: &TraitDef) -> TraitDecl {
915919
with(|cx| cx.trait_decl(trait_def))
@@ -932,6 +936,10 @@ crate_def! {
932936
pub ImplDef;
933937
}
934938

939+
impl_crate_def_items! {
940+
ImplDef;
941+
}
942+
935943
impl ImplDef {
936944
/// Retrieve information about this implementation.
937945
pub fn trait_impl(&self) -> ImplTrait {
@@ -1555,3 +1563,60 @@ index_impl!(Span);
15551563
pub struct VariantIdx(usize);
15561564

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

0 commit comments

Comments
 (0)