Skip to content

Commit acc8370

Browse files
committed
refactor: change Tables<'tcx> to Tables<'tcx, B: Bridge>
1 parent d7df5bd commit acc8370

File tree

3 files changed

+80
-130
lines changed

3 files changed

+80
-130
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

Lines changed: 8 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -62,129 +62,33 @@ where
6262
with_tables(|tables| item.internal(tables, tcx))
6363
}
6464

65-
impl<'tcx> Index<stable_mir::DefId> for Tables<'tcx> {
65+
impl<'tcx, B: Bridge> Index<B::DefId> for Tables<'tcx, B> {
6666
type Output = DefId;
6767

6868
#[inline(always)]
69-
fn index(&self, index: stable_mir::DefId) -> &Self::Output {
69+
fn index(&self, index: B::DefId) -> &Self::Output {
7070
&self.def_ids[index]
7171
}
7272
}
7373

74-
impl<'tcx> Index<stable_mir::ty::Span> for Tables<'tcx> {
75-
type Output = Span;
76-
77-
#[inline(always)]
78-
fn index(&self, index: stable_mir::ty::Span) -> &Self::Output {
79-
&self.spans[index]
80-
}
81-
}
82-
83-
impl<'tcx> Tables<'tcx> {
84-
pub fn crate_item(&mut self, did: DefId) -> stable_mir::CrateItem {
85-
stable_mir::CrateItem(self.create_def_id(did))
86-
}
87-
88-
pub fn adt_def(&mut self, did: DefId) -> stable_mir::ty::AdtDef {
89-
stable_mir::ty::AdtDef(self.create_def_id(did))
90-
}
91-
92-
pub fn foreign_module_def(&mut self, did: DefId) -> stable_mir::ty::ForeignModuleDef {
93-
stable_mir::ty::ForeignModuleDef(self.create_def_id(did))
94-
}
95-
96-
pub fn foreign_def(&mut self, did: DefId) -> stable_mir::ty::ForeignDef {
97-
stable_mir::ty::ForeignDef(self.create_def_id(did))
98-
}
99-
100-
pub fn fn_def(&mut self, did: DefId) -> stable_mir::ty::FnDef {
101-
stable_mir::ty::FnDef(self.create_def_id(did))
102-
}
103-
104-
pub fn closure_def(&mut self, did: DefId) -> stable_mir::ty::ClosureDef {
105-
stable_mir::ty::ClosureDef(self.create_def_id(did))
106-
}
107-
108-
pub fn coroutine_def(&mut self, did: DefId) -> stable_mir::ty::CoroutineDef {
109-
stable_mir::ty::CoroutineDef(self.create_def_id(did))
110-
}
111-
112-
pub fn coroutine_closure_def(&mut self, did: DefId) -> stable_mir::ty::CoroutineClosureDef {
113-
stable_mir::ty::CoroutineClosureDef(self.create_def_id(did))
114-
}
115-
116-
pub fn alias_def(&mut self, did: DefId) -> stable_mir::ty::AliasDef {
117-
stable_mir::ty::AliasDef(self.create_def_id(did))
118-
}
119-
120-
pub fn param_def(&mut self, did: DefId) -> stable_mir::ty::ParamDef {
121-
stable_mir::ty::ParamDef(self.create_def_id(did))
122-
}
123-
124-
pub fn br_named_def(&mut self, did: DefId) -> stable_mir::ty::BrNamedDef {
125-
stable_mir::ty::BrNamedDef(self.create_def_id(did))
126-
}
127-
128-
pub fn trait_def(&mut self, did: DefId) -> stable_mir::ty::TraitDef {
129-
stable_mir::ty::TraitDef(self.create_def_id(did))
130-
}
131-
132-
pub fn generic_def(&mut self, did: DefId) -> stable_mir::ty::GenericDef {
133-
stable_mir::ty::GenericDef(self.create_def_id(did))
134-
}
135-
136-
pub fn const_def(&mut self, did: DefId) -> stable_mir::ty::ConstDef {
137-
stable_mir::ty::ConstDef(self.create_def_id(did))
138-
}
139-
140-
pub fn impl_def(&mut self, did: DefId) -> stable_mir::ty::ImplDef {
141-
stable_mir::ty::ImplDef(self.create_def_id(did))
142-
}
143-
144-
pub fn region_def(&mut self, did: DefId) -> stable_mir::ty::RegionDef {
145-
stable_mir::ty::RegionDef(self.create_def_id(did))
146-
}
147-
148-
pub fn coroutine_witness_def(&mut self, did: DefId) -> stable_mir::ty::CoroutineWitnessDef {
149-
stable_mir::ty::CoroutineWitnessDef(self.create_def_id(did))
150-
}
151-
152-
pub fn assoc_def(&mut self, did: DefId) -> stable_mir::ty::AssocDef {
153-
stable_mir::ty::AssocDef(self.create_def_id(did))
154-
}
155-
156-
pub fn opaque_def(&mut self, did: DefId) -> stable_mir::ty::OpaqueDef {
157-
stable_mir::ty::OpaqueDef(self.create_def_id(did))
158-
}
159-
160-
pub fn prov(&mut self, aid: AllocId) -> stable_mir::ty::Prov {
161-
stable_mir::ty::Prov(self.create_alloc_id(aid))
162-
}
163-
164-
pub(crate) fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
74+
impl<'tcx, B: Bridge> Tables<'tcx, B> {
75+
pub fn create_def_id(&mut self, did: DefId) -> B::DefId {
16576
self.def_ids.create_or_fetch(did)
16677
}
16778

168-
pub(crate) fn create_alloc_id(&mut self, aid: AllocId) -> stable_mir::mir::alloc::AllocId {
79+
pub fn create_alloc_id(&mut self, aid: AllocId) -> B::AllocId {
16980
self.alloc_ids.create_or_fetch(aid)
17081
}
17182

172-
pub(crate) fn create_span(&mut self, span: Span) -> stable_mir::ty::Span {
83+
pub fn create_span(&mut self, span: Span) -> B::Span {
17384
self.spans.create_or_fetch(span)
17485
}
17586

176-
pub(crate) fn instance_def(
177-
&mut self,
178-
instance: ty::Instance<'tcx>,
179-
) -> stable_mir::mir::mono::InstanceDef {
87+
pub fn instance_def(&mut self, instance: ty::Instance<'tcx>) -> B::InstanceDef {
18088
self.instances.create_or_fetch(instance)
18189
}
18290

183-
pub(crate) fn static_def(&mut self, did: DefId) -> stable_mir::mir::mono::StaticDef {
184-
stable_mir::mir::mono::StaticDef(self.create_def_id(did))
185-
}
186-
187-
pub(crate) fn layout_id(&mut self, layout: rustc_abi::Layout<'tcx>) -> Layout {
91+
pub fn layout_id(&mut self, layout: rustc_abi::Layout<'tcx>) -> B::Layout {
18892
self.layouts.create_or_fetch(layout)
18993
}
19094
}

compiler/rustc_smir/src/rustc_smir/builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use rustc_middle::mir;
99
use rustc_middle::mir::visit::MutVisitor;
1010
use rustc_middle::ty::{self, TyCtxt};
1111

12-
use crate::rustc_smir::{Stable, Tables};
13-
use crate::stable_mir;
12+
use crate::rustc_smir::{Bridge, Tables};
1413

1514
/// Builds a monomorphic body for a given instance.
1615
pub(crate) struct BodyBuilder<'tcx> {
@@ -31,7 +30,7 @@ impl<'tcx> BodyBuilder<'tcx> {
3130
/// Build a stable monomorphic body for a given instance based on the MIR body.
3231
///
3332
/// All constants are also evaluated.
34-
pub(crate) fn build(mut self, tables: &mut Tables<'tcx>) -> stable_mir::mir::Body {
33+
pub(crate) fn build<B: Bridge>(mut self, tables: &mut Tables<'tcx, B>) -> mir::Body<'tcx> {
3534
let body = tables.tcx.instance_mir(self.instance.def).clone();
3635
let mono_body = if !self.instance.args.is_empty()
3736
// Without the `generic_const_exprs` feature gate, anon consts in signatures do not
@@ -50,7 +49,8 @@ impl<'tcx> BodyBuilder<'tcx> {
5049
// Already monomorphic.
5150
body
5251
};
53-
mono_body.stable(tables)
52+
53+
mono_body
5454
}
5555
}
5656

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,51 @@ use crate::stable_mir;
2626
mod alloc;
2727
mod builder;
2828
pub mod context;
29-
mod convert;
30-
31-
pub struct Tables<'tcx> {
32-
pub(crate) tcx: TyCtxt<'tcx>,
33-
pub(crate) def_ids: IndexMap<DefId, stable_mir::DefId>,
34-
pub(crate) alloc_ids: IndexMap<AllocId, stable_mir::mir::alloc::AllocId>,
35-
pub(crate) spans: IndexMap<rustc_span::Span, Span>,
36-
pub(crate) types: IndexMap<Ty<'tcx>, stable_mir::ty::Ty>,
37-
pub(crate) instances: IndexMap<ty::Instance<'tcx>, InstanceDef>,
38-
pub(crate) ty_consts: IndexMap<ty::Const<'tcx>, TyConstId>,
39-
pub(crate) mir_consts: IndexMap<mir::Const<'tcx>, MirConstId>,
40-
pub(crate) layouts: IndexMap<rustc_abi::Layout<'tcx>, Layout>,
41-
}
42-
43-
impl<'tcx> Tables<'tcx> {
44-
pub(crate) fn intern_ty(&mut self, ty: Ty<'tcx>) -> stable_mir::ty::Ty {
29+
30+
/// A container which is used for TLS.
31+
pub struct SmirContainer<'tcx, B: Bridge> {
32+
pub tables: RefCell<Tables<'tcx, B>>,
33+
pub cx: RefCell<SmirCtxt<'tcx, B>>,
34+
}
35+
36+
pub struct Tables<'tcx, B: Bridge> {
37+
tcx: TyCtxt<'tcx>,
38+
pub(crate) def_ids: IndexMap<DefId, B::DefId>,
39+
pub(crate) alloc_ids: IndexMap<AllocId, B::AllocId>,
40+
pub(crate) spans: IndexMap<rustc_span::Span, B::Span>,
41+
pub(crate) types: IndexMap<Ty<'tcx>, B::Ty>,
42+
pub(crate) instances: IndexMap<ty::Instance<'tcx>, B::InstanceDef>,
43+
pub(crate) ty_consts: IndexMap<ty::Const<'tcx>, B::TyConstId>,
44+
pub(crate) mir_consts: IndexMap<mir::Const<'tcx>, B::MirConstId>,
45+
pub(crate) layouts: IndexMap<rustc_abi::Layout<'tcx>, B::Layout>,
46+
}
47+
48+
impl<'tcx, B: Bridge> Tables<'tcx, B> {
49+
pub(crate) fn new(tcx: TyCtxt<'tcx>) -> Self {
50+
Self {
51+
tcx,
52+
def_ids: IndexMap::default(),
53+
alloc_ids: IndexMap::default(),
54+
spans: IndexMap::default(),
55+
types: IndexMap::default(),
56+
instances: IndexMap::default(),
57+
ty_consts: IndexMap::default(),
58+
mir_consts: IndexMap::default(),
59+
layouts: IndexMap::default(),
60+
}
61+
}
62+
}
63+
64+
impl<'tcx, B: Bridge> Tables<'tcx, B> {
65+
pub(crate) fn intern_ty(&mut self, ty: Ty<'tcx>) -> B::Ty {
4566
self.types.create_or_fetch(ty)
4667
}
4768

48-
pub(crate) fn intern_ty_const(&mut self, ct: ty::Const<'tcx>) -> TyConstId {
69+
pub(crate) fn intern_ty_const(&mut self, ct: ty::Const<'tcx>) -> B::TyConstId {
4970
self.ty_consts.create_or_fetch(ct)
5071
}
5172

52-
pub(crate) fn intern_mir_const(&mut self, constant: mir::Const<'tcx>) -> MirConstId {
73+
pub(crate) fn intern_mir_const(&mut self, constant: mir::Const<'tcx>) -> B::MirConstId {
5374
self.mir_consts.create_or_fetch(constant)
5475
}
5576

@@ -81,19 +102,44 @@ impl<'tcx> Tables<'tcx> {
81102
!must_override && self.tcx.is_mir_available(def_id)
82103
}
83104

84-
fn to_fn_def(&mut self, def_id: DefId) -> Option<FnDef> {
105+
fn filter_fn_def(&mut self, def_id: DefId) -> Option<DefId> {
85106
if matches!(self.tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
86-
Some(self.fn_def(def_id))
107+
Some(def_id)
87108
} else {
88109
None
89110
}
90111
}
91112

92-
fn to_static(&mut self, def_id: DefId) -> Option<StaticDef> {
93-
matches!(self.tcx.def_kind(def_id), DefKind::Static { .. }).then(|| self.static_def(def_id))
113+
fn filter_static_def(&mut self, def_id: DefId) -> Option<DefId> {
114+
matches!(self.tcx.def_kind(def_id), DefKind::Static { .. }).then(|| def_id)
94115
}
95116
}
96117

118+
/// A trait defining types that are used to emulate StableMIR components, which is really
119+
/// useful when programming in stable_mir-agnostic settings.
120+
pub trait Bridge {
121+
type DefId: Copy + Debug + PartialEq + IndexedVal;
122+
type AllocId: Copy + Debug + PartialEq + IndexedVal;
123+
type Span: Copy + Debug + PartialEq + IndexedVal;
124+
type Ty: Copy + Debug + PartialEq + IndexedVal;
125+
type InstanceDef: Copy + Debug + PartialEq + IndexedVal;
126+
type TyConstId: Copy + Debug + PartialEq + IndexedVal;
127+
type MirConstId: Copy + Debug + PartialEq + IndexedVal;
128+
type Layout: Copy + Debug + PartialEq + IndexedVal;
129+
type Error: SmirError;
130+
}
131+
132+
pub trait SmirError {
133+
fn new(msg: String) -> Self;
134+
fn from_internal<T: Debug>(err: T) -> Self;
135+
}
136+
137+
pub trait IndexedVal {
138+
fn to_val(index: usize) -> Self;
139+
140+
fn to_index(&self) -> usize;
141+
}
142+
97143
/// Iterate over the definitions of the given crate.
98144
pub(crate) fn filter_def_ids<F, T>(tcx: TyCtxt<'_>, krate: CrateNum, mut func: F) -> Vec<T>
99145
where

0 commit comments

Comments
 (0)