Skip to content

Commit 27762b6

Browse files
committed
refactor: unify Tables implementation with bridge types
define bridge types for `***Def`s. consolidate scattered `Tables` implementations into single inherent impl.
1 parent 151d336 commit 27762b6

File tree

8 files changed

+266
-177
lines changed

8 files changed

+266
-177
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ use std::ops::Index;
1010

1111
use rustc_data_structures::fx;
1212
use rustc_data_structures::fx::FxIndexMap;
13-
use rustc_middle::mir::interpret::AllocId;
14-
use rustc_middle::ty;
1513
use rustc_middle::ty::TyCtxt;
16-
use rustc_span::Span;
17-
use rustc_span::def_id::{CrateNum, DefId};
14+
use rustc_span::def_id::CrateNum;
1815
use scoped_tls::scoped_thread_local;
1916
use stable_mir::Error;
2017
use stable_mir::convert::{RustcInternal, Stable};
@@ -58,37 +55,6 @@ where
5855
with_container(|tables, cx| item.internal(tables, cx))
5956
}
6057

61-
impl<'tcx, B: Bridge> Index<B::DefId> for Tables<'tcx, B> {
62-
type Output = DefId;
63-
64-
#[inline(always)]
65-
fn index(&self, index: B::DefId) -> &Self::Output {
66-
&self.def_ids[index]
67-
}
68-
}
69-
70-
impl<'tcx, B: Bridge> Tables<'tcx, B> {
71-
pub fn create_def_id(&mut self, did: DefId) -> B::DefId {
72-
self.def_ids.create_or_fetch(did)
73-
}
74-
75-
pub fn create_alloc_id(&mut self, aid: AllocId) -> B::AllocId {
76-
self.alloc_ids.create_or_fetch(aid)
77-
}
78-
79-
pub fn create_span(&mut self, span: Span) -> B::Span {
80-
self.spans.create_or_fetch(span)
81-
}
82-
83-
pub fn instance_def(&mut self, instance: ty::Instance<'tcx>) -> B::InstanceDef {
84-
self.instances.create_or_fetch(instance)
85-
}
86-
87-
pub fn layout_id(&mut self, layout: rustc_abi::Layout<'tcx>) -> B::Layout {
88-
self.layouts.create_or_fetch(layout)
89-
}
90-
}
91-
9258
pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
9359
item.id.into()
9460
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::fmt::Debug;
2+
3+
use super::Bridge;
4+
5+
pub trait SmirError {
6+
fn new(msg: String) -> Self;
7+
fn from_internal<T: Debug>(err: T) -> Self;
8+
}
9+
10+
macro_rules! make_def_trait {
11+
($name:ident) => {
12+
pub trait $name<B: Bridge> {
13+
fn new(did: B::DefId) -> Self;
14+
}
15+
};
16+
}
17+
18+
make_def_trait!(CrateItem);
19+
make_def_trait!(AdtDef);
20+
make_def_trait!(ForeignModuleDef);
21+
make_def_trait!(ForeignDef);
22+
make_def_trait!(FnDef);
23+
make_def_trait!(ClosureDef);
24+
make_def_trait!(CoroutineDef);
25+
make_def_trait!(CoroutineClosureDef);
26+
make_def_trait!(AliasDef);
27+
make_def_trait!(ParamDef);
28+
make_def_trait!(BrNamedDef);
29+
make_def_trait!(TraitDef);
30+
make_def_trait!(GenericDef);
31+
make_def_trait!(ConstDef);
32+
make_def_trait!(ImplDef);
33+
make_def_trait!(RegionDef);
34+
make_def_trait!(CoroutineWitnessDef);
35+
make_def_trait!(AssocDef);
36+
make_def_trait!(OpaqueDef);
37+
make_def_trait!(StaticDef);
38+
39+
pub trait Prov<B: Bridge> {
40+
fn new(aid: B::AllocId) -> Self;
41+
}

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 157 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@
99
1010
use std::cell::RefCell;
1111
use std::fmt::Debug;
12+
use std::ops::Index;
1213

14+
use bridge::*;
1315
use context::SmirCtxt;
1416
use rustc_middle::mir;
1517
use rustc_middle::mir::interpret::AllocId;
1618
use rustc_middle::ty::{self, Ty, TyCtxt};
19+
use rustc_span::Span;
1720
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
1821

1922
use crate::rustc_internal::IndexMap;
2023

2124
pub mod alloc;
25+
pub mod bridge;
2226
mod builder;
2327
pub mod context;
2428

@@ -29,14 +33,14 @@ pub struct SmirContainer<'tcx, B: Bridge> {
2933
}
3034

3135
pub struct Tables<'tcx, B: Bridge> {
32-
pub(crate) def_ids: IndexMap<DefId, B::DefId>,
33-
pub(crate) alloc_ids: IndexMap<AllocId, B::AllocId>,
34-
pub(crate) spans: IndexMap<rustc_span::Span, B::Span>,
35-
pub(crate) types: IndexMap<Ty<'tcx>, B::Ty>,
36-
pub(crate) instances: IndexMap<ty::Instance<'tcx>, B::InstanceDef>,
37-
pub(crate) ty_consts: IndexMap<ty::Const<'tcx>, B::TyConstId>,
38-
pub(crate) mir_consts: IndexMap<mir::Const<'tcx>, B::MirConstId>,
39-
pub(crate) layouts: IndexMap<rustc_abi::Layout<'tcx>, B::Layout>,
36+
pub def_ids: IndexMap<DefId, B::DefId>,
37+
pub alloc_ids: IndexMap<AllocId, B::AllocId>,
38+
pub spans: IndexMap<rustc_span::Span, B::Span>,
39+
pub types: IndexMap<Ty<'tcx>, B::Ty>,
40+
pub instances: IndexMap<ty::Instance<'tcx>, B::InstanceDef>,
41+
pub ty_consts: IndexMap<ty::Const<'tcx>, B::TyConstId>,
42+
pub mir_consts: IndexMap<mir::Const<'tcx>, B::MirConstId>,
43+
pub layouts: IndexMap<rustc_abi::Layout<'tcx>, B::Layout>,
4044
}
4145

4246
impl<'tcx, B: Bridge> Default for Tables<'tcx, B> {
@@ -54,23 +58,142 @@ impl<'tcx, B: Bridge> Default for Tables<'tcx, B> {
5458
}
5559
}
5660

61+
impl<'tcx, B: Bridge> Index<B::DefId> for Tables<'tcx, B> {
62+
type Output = DefId;
63+
64+
#[inline(always)]
65+
fn index(&self, index: B::DefId) -> &Self::Output {
66+
&self.def_ids[index]
67+
}
68+
}
69+
5770
impl<'tcx, B: Bridge> Tables<'tcx, B> {
58-
pub(crate) fn intern_ty(&mut self, ty: Ty<'tcx>) -> B::Ty {
71+
pub fn intern_ty(&mut self, ty: Ty<'tcx>) -> B::Ty {
5972
self.types.create_or_fetch(ty)
6073
}
6174

62-
pub(crate) fn intern_ty_const(&mut self, ct: ty::Const<'tcx>) -> B::TyConstId {
75+
pub fn intern_ty_const(&mut self, ct: ty::Const<'tcx>) -> B::TyConstId {
6376
self.ty_consts.create_or_fetch(ct)
6477
}
6578

66-
pub(crate) fn intern_mir_const(&mut self, constant: mir::Const<'tcx>) -> B::MirConstId {
79+
pub fn intern_mir_const(&mut self, constant: mir::Const<'tcx>) -> B::MirConstId {
6780
self.mir_consts.create_or_fetch(constant)
6881
}
82+
83+
pub fn create_def_id(&mut self, did: DefId) -> B::DefId {
84+
self.def_ids.create_or_fetch(did)
85+
}
86+
87+
pub fn create_alloc_id(&mut self, aid: AllocId) -> B::AllocId {
88+
self.alloc_ids.create_or_fetch(aid)
89+
}
90+
91+
pub fn create_span(&mut self, span: Span) -> B::Span {
92+
self.spans.create_or_fetch(span)
93+
}
94+
95+
pub fn instance_def(&mut self, instance: ty::Instance<'tcx>) -> B::InstanceDef {
96+
self.instances.create_or_fetch(instance)
97+
}
98+
99+
pub fn layout_id(&mut self, layout: rustc_abi::Layout<'tcx>) -> B::Layout {
100+
self.layouts.create_or_fetch(layout)
101+
}
102+
103+
pub fn crate_item(&mut self, did: rustc_span::def_id::DefId) -> B::CrateItem {
104+
B::CrateItem::new(self.create_def_id(did))
105+
}
106+
107+
pub fn adt_def(&mut self, did: rustc_span::def_id::DefId) -> B::AdtDef {
108+
B::AdtDef::new(self.create_def_id(did))
109+
}
110+
111+
pub fn foreign_module_def(&mut self, did: rustc_span::def_id::DefId) -> B::ForeignModuleDef {
112+
B::ForeignModuleDef::new(self.create_def_id(did))
113+
}
114+
115+
pub fn foreign_def(&mut self, did: rustc_span::def_id::DefId) -> B::ForeignDef {
116+
B::ForeignDef::new(self.create_def_id(did))
117+
}
118+
119+
pub fn fn_def(&mut self, did: rustc_span::def_id::DefId) -> B::FnDef {
120+
B::FnDef::new(self.create_def_id(did))
121+
}
122+
123+
pub fn closure_def(&mut self, did: rustc_span::def_id::DefId) -> B::ClosureDef {
124+
B::ClosureDef::new(self.create_def_id(did))
125+
}
126+
127+
pub fn coroutine_def(&mut self, did: rustc_span::def_id::DefId) -> B::CoroutineDef {
128+
B::CoroutineDef::new(self.create_def_id(did))
129+
}
130+
131+
pub fn coroutine_closure_def(
132+
&mut self,
133+
did: rustc_span::def_id::DefId,
134+
) -> B::CoroutineClosureDef {
135+
B::CoroutineClosureDef::new(self.create_def_id(did))
136+
}
137+
138+
pub fn alias_def(&mut self, did: rustc_span::def_id::DefId) -> B::AliasDef {
139+
B::AliasDef::new(self.create_def_id(did))
140+
}
141+
142+
pub fn param_def(&mut self, did: rustc_span::def_id::DefId) -> B::ParamDef {
143+
B::ParamDef::new(self.create_def_id(did))
144+
}
145+
146+
pub fn br_named_def(&mut self, did: rustc_span::def_id::DefId) -> B::BrNamedDef {
147+
B::BrNamedDef::new(self.create_def_id(did))
148+
}
149+
150+
pub fn trait_def(&mut self, did: rustc_span::def_id::DefId) -> B::TraitDef {
151+
B::TraitDef::new(self.create_def_id(did))
152+
}
153+
154+
pub fn generic_def(&mut self, did: rustc_span::def_id::DefId) -> B::GenericDef {
155+
B::GenericDef::new(self.create_def_id(did))
156+
}
157+
158+
pub fn const_def(&mut self, did: rustc_span::def_id::DefId) -> B::ConstDef {
159+
B::ConstDef::new(self.create_def_id(did))
160+
}
161+
162+
pub fn impl_def(&mut self, did: rustc_span::def_id::DefId) -> B::ImplDef {
163+
B::ImplDef::new(self.create_def_id(did))
164+
}
165+
166+
pub fn region_def(&mut self, did: rustc_span::def_id::DefId) -> B::RegionDef {
167+
B::RegionDef::new(self.create_def_id(did))
168+
}
169+
170+
pub fn coroutine_witness_def(
171+
&mut self,
172+
did: rustc_span::def_id::DefId,
173+
) -> B::CoroutineWitnessDef {
174+
B::CoroutineWitnessDef::new(self.create_def_id(did))
175+
}
176+
177+
pub fn assoc_def(&mut self, did: rustc_span::def_id::DefId) -> B::AssocDef {
178+
B::AssocDef::new(self.create_def_id(did))
179+
}
180+
181+
pub fn opaque_def(&mut self, did: rustc_span::def_id::DefId) -> B::OpaqueDef {
182+
B::OpaqueDef::new(self.create_def_id(did))
183+
}
184+
185+
pub fn prov(&mut self, aid: rustc_middle::mir::interpret::AllocId) -> B::Prov {
186+
B::Prov::new(self.create_alloc_id(aid))
187+
}
188+
189+
pub fn static_def(&mut self, did: rustc_span::def_id::DefId) -> B::StaticDef {
190+
B::StaticDef::new(self.create_def_id(did))
191+
}
69192
}
70193

71194
/// A trait defining types that are used to emulate StableMIR components, which is really
72195
/// useful when programming in stable_mir-agnostic settings.
73-
pub trait Bridge {
196+
pub trait Bridge: Sized {
74197
type DefId: Copy + Debug + PartialEq + IndexedVal;
75198
type AllocId: Copy + Debug + PartialEq + IndexedVal;
76199
type Span: Copy + Debug + PartialEq + IndexedVal;
@@ -79,12 +202,29 @@ pub trait Bridge {
79202
type TyConstId: Copy + Debug + PartialEq + IndexedVal;
80203
type MirConstId: Copy + Debug + PartialEq + IndexedVal;
81204
type Layout: Copy + Debug + PartialEq + IndexedVal;
82-
type Error: SmirError;
83-
}
84205

85-
pub trait SmirError {
86-
fn new(msg: String) -> Self;
87-
fn from_internal<T: Debug>(err: T) -> Self;
206+
type Error: SmirError;
207+
type CrateItem: CrateItem<Self>;
208+
type AdtDef: AdtDef<Self>;
209+
type ForeignModuleDef: ForeignModuleDef<Self>;
210+
type ForeignDef: ForeignDef<Self>;
211+
type FnDef: FnDef<Self>;
212+
type ClosureDef: ClosureDef<Self>;
213+
type CoroutineDef: CoroutineDef<Self>;
214+
type CoroutineClosureDef: CoroutineClosureDef<Self>;
215+
type AliasDef: AliasDef<Self>;
216+
type ParamDef: ParamDef<Self>;
217+
type BrNamedDef: BrNamedDef<Self>;
218+
type TraitDef: TraitDef<Self>;
219+
type GenericDef: GenericDef<Self>;
220+
type ConstDef: ConstDef<Self>;
221+
type ImplDef: ImplDef<Self>;
222+
type RegionDef: RegionDef<Self>;
223+
type CoroutineWitnessDef: CoroutineWitnessDef<Self>;
224+
type AssocDef: AssocDef<Self>;
225+
type OpaqueDef: OpaqueDef<Self>;
226+
type Prov: Prov<Self>;
227+
type StaticDef: StaticDef<Self>;
88228
}
89229

90230
pub trait IndexedVal {

compiler/rustc_smir/src/stable_mir/alloc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
use rustc_abi::Align;
88
use rustc_middle::mir::ConstValue;
99
use rustc_middle::mir::interpret::AllocRange;
10+
use rustc_smir::bridge::SmirError;
1011
use rustc_smir::context::SmirCtxt;
11-
use rustc_smir::{SmirError, Tables, alloc};
12+
use rustc_smir::{Tables, alloc};
1213

1314
use super::Error;
1415
use super::compiler_interface::BridgeTys;

0 commit comments

Comments
 (0)