@@ -23,27 +23,31 @@ use stable_mir::ty::{
23
23
FloatTy , GenericParamDef , IntTy , LineInfo , Movability , RigidTy , Span , TyKind , UintTy ,
24
24
} ;
25
25
use stable_mir:: { self , opaque, Context , Filename } ;
26
+ use std:: cell:: RefCell ;
26
27
use tracing:: debug;
27
28
28
29
mod alloc;
29
30
mod builder;
30
31
31
- impl < ' tcx > Context for Tables < ' tcx > {
32
+ impl < ' tcx > Context for TablesWrapper < ' tcx > {
32
33
fn local_crate ( & self ) -> stable_mir:: Crate {
33
- smir_crate ( self . tcx , LOCAL_CRATE )
34
+ let tables = self . 0 . borrow ( ) ;
35
+ smir_crate ( tables. tcx , LOCAL_CRATE )
34
36
}
35
37
36
38
fn external_crates ( & self ) -> Vec < stable_mir:: Crate > {
37
- self . tcx . crates ( ( ) ) . iter ( ) . map ( |crate_num| smir_crate ( self . tcx , * crate_num) ) . collect ( )
39
+ let tables = self . 0 . borrow ( ) ;
40
+ tables. tcx . crates ( ( ) ) . iter ( ) . map ( |crate_num| smir_crate ( tables. tcx , * crate_num) ) . collect ( )
38
41
}
39
42
40
43
fn find_crates ( & self , name : & str ) -> Vec < stable_mir:: Crate > {
44
+ let tables = self . 0 . borrow ( ) ;
41
45
let crates: Vec < stable_mir:: Crate > = [ LOCAL_CRATE ]
42
46
. iter ( )
43
- . chain ( self . tcx . crates ( ( ) ) . iter ( ) )
47
+ . chain ( tables . tcx . crates ( ( ) ) . iter ( ) )
44
48
. map ( |crate_num| {
45
- let crate_name = self . tcx . crate_name ( * crate_num) . to_string ( ) ;
46
- ( name == crate_name) . then ( || smir_crate ( self . tcx , * crate_num) )
49
+ let crate_name = tables . tcx . crate_name ( * crate_num) . to_string ( ) ;
50
+ ( name == crate_name) . then ( || smir_crate ( tables . tcx , * crate_num) )
47
51
} )
48
52
. into_iter ( )
49
53
. filter_map ( |c| c)
@@ -52,163 +56,197 @@ impl<'tcx> Context for Tables<'tcx> {
52
56
}
53
57
54
58
fn name_of_def_id ( & self , def_id : stable_mir:: DefId ) -> String {
55
- self . tcx . def_path_str ( self [ def_id] )
59
+ let tables = self . 0 . borrow ( ) ;
60
+ tables. tcx . def_path_str ( tables[ def_id] )
56
61
}
57
62
58
63
fn span_to_string ( & self , span : stable_mir:: ty:: Span ) -> String {
59
- self . tcx . sess . source_map ( ) . span_to_diagnostic_string ( self [ span] )
64
+ let tables = self . 0 . borrow ( ) ;
65
+ tables. tcx . sess . source_map ( ) . span_to_diagnostic_string ( tables[ span] )
60
66
}
61
67
62
68
fn get_filename ( & self , span : & Span ) -> Filename {
69
+ let tables = self . 0 . borrow ( ) ;
63
70
opaque (
64
- & self
71
+ & tables
65
72
. tcx
66
73
. sess
67
74
. source_map ( )
68
- . span_to_filename ( self [ * span] )
75
+ . span_to_filename ( tables [ * span] )
69
76
. display ( rustc_span:: FileNameDisplayPreference :: Local )
70
77
. to_string ( ) ,
71
78
)
72
79
}
73
80
74
81
fn get_lines ( & self , span : & Span ) -> LineInfo {
75
- let lines = & self . tcx . sess . source_map ( ) . span_to_location_info ( self [ * span] ) ;
82
+ let tables = self . 0 . borrow ( ) ;
83
+ let lines = & tables. tcx . sess . source_map ( ) . span_to_location_info ( tables[ * span] ) ;
76
84
LineInfo { start_line : lines. 1 , start_col : lines. 2 , end_line : lines. 3 , end_col : lines. 4 }
77
85
}
78
86
79
- fn def_kind ( & mut self , def_id : stable_mir:: DefId ) -> stable_mir:: DefKind {
80
- self . tcx . def_kind ( self [ def_id] ) . stable ( self )
87
+ fn def_kind ( & self , def_id : stable_mir:: DefId ) -> stable_mir:: DefKind {
88
+ let mut tables = self . 0 . borrow_mut ( ) ;
89
+ tables. tcx . def_kind ( tables[ def_id] ) . stable ( & mut * tables)
81
90
}
82
91
83
- fn span_of_an_item ( & mut self , def_id : stable_mir:: DefId ) -> Span {
84
- self . tcx . def_span ( self [ def_id] ) . stable ( self )
92
+ fn span_of_an_item ( & self , def_id : stable_mir:: DefId ) -> Span {
93
+ let mut tables = self . 0 . borrow_mut ( ) ;
94
+ tables. tcx . def_span ( tables[ def_id] ) . stable ( & mut * tables)
85
95
}
86
96
87
- fn all_local_items ( & mut self ) -> stable_mir:: CrateItems {
88
- self . tcx . mir_keys ( ( ) ) . iter ( ) . map ( |item| self . crate_item ( item. to_def_id ( ) ) ) . collect ( )
97
+ fn all_local_items ( & self ) -> stable_mir:: CrateItems {
98
+ let mut tables = self . 0 . borrow_mut ( ) ;
99
+ tables. tcx . mir_keys ( ( ) ) . iter ( ) . map ( |item| tables. crate_item ( item. to_def_id ( ) ) ) . collect ( )
89
100
}
90
101
91
- fn entry_fn ( & mut self ) -> Option < stable_mir:: CrateItem > {
92
- Some ( self . crate_item ( self . tcx . entry_fn ( ( ) ) ?. 0 ) )
102
+ fn entry_fn ( & self ) -> Option < stable_mir:: CrateItem > {
103
+ let mut tables = self . 0 . borrow_mut ( ) ;
104
+ let tcx = tables. tcx ;
105
+ Some ( tables. crate_item ( tcx. entry_fn ( ( ) ) ?. 0 ) )
93
106
}
94
107
95
- fn all_trait_decls ( & mut self ) -> stable_mir:: TraitDecls {
96
- self . tcx
108
+ fn all_trait_decls ( & self ) -> stable_mir:: TraitDecls {
109
+ let mut tables = self . 0 . borrow_mut ( ) ;
110
+ tables
111
+ . tcx
97
112
. traits ( LOCAL_CRATE )
98
113
. iter ( )
99
- . map ( |trait_def_id| self . trait_def ( * trait_def_id) )
114
+ . map ( |trait_def_id| tables . trait_def ( * trait_def_id) )
100
115
. collect ( )
101
116
}
102
117
103
- fn trait_decl ( & mut self , trait_def : & stable_mir:: ty:: TraitDef ) -> stable_mir:: ty:: TraitDecl {
104
- let def_id = self [ trait_def. 0 ] ;
105
- let trait_def = self . tcx . trait_def ( def_id) ;
106
- trait_def. stable ( self )
118
+ fn trait_decl ( & self , trait_def : & stable_mir:: ty:: TraitDef ) -> stable_mir:: ty:: TraitDecl {
119
+ let mut tables = self . 0 . borrow_mut ( ) ;
120
+ let def_id = tables[ trait_def. 0 ] ;
121
+ let trait_def = tables. tcx . trait_def ( def_id) ;
122
+ trait_def. stable ( & mut * tables)
107
123
}
108
124
109
- fn all_trait_impls ( & mut self ) -> stable_mir:: ImplTraitDecls {
110
- self . tcx
125
+ fn all_trait_impls ( & self ) -> stable_mir:: ImplTraitDecls {
126
+ let mut tables = self . 0 . borrow_mut ( ) ;
127
+ tables
128
+ . tcx
111
129
. trait_impls_in_crate ( LOCAL_CRATE )
112
130
. iter ( )
113
- . map ( |impl_def_id| self . impl_def ( * impl_def_id) )
131
+ . map ( |impl_def_id| tables . impl_def ( * impl_def_id) )
114
132
. collect ( )
115
133
}
116
134
117
- fn trait_impl ( & mut self , impl_def : & stable_mir:: ty:: ImplDef ) -> stable_mir:: ty:: ImplTrait {
118
- let def_id = self [ impl_def. 0 ] ;
119
- let impl_trait = self . tcx . impl_trait_ref ( def_id) . unwrap ( ) ;
120
- impl_trait. stable ( self )
135
+ fn trait_impl ( & self , impl_def : & stable_mir:: ty:: ImplDef ) -> stable_mir:: ty:: ImplTrait {
136
+ let mut tables = self . 0 . borrow_mut ( ) ;
137
+ let def_id = tables[ impl_def. 0 ] ;
138
+ let impl_trait = tables. tcx . impl_trait_ref ( def_id) . unwrap ( ) ;
139
+ impl_trait. stable ( & mut * tables)
121
140
}
122
141
123
- fn mir_body ( & mut self , item : stable_mir:: DefId ) -> stable_mir:: mir:: Body {
124
- let def_id = self [ item] ;
125
- self . tcx . instance_mir ( ty:: InstanceDef :: Item ( def_id) ) . stable ( self )
142
+ fn mir_body ( & self , item : stable_mir:: DefId ) -> stable_mir:: mir:: Body {
143
+ let mut tables = self . 0 . borrow_mut ( ) ;
144
+ let def_id = tables[ item] ;
145
+ tables. tcx . instance_mir ( ty:: InstanceDef :: Item ( def_id) ) . stable ( & mut tables)
126
146
}
127
147
128
- fn ty_kind ( & mut self , ty : stable_mir:: ty:: Ty ) -> TyKind {
129
- self . types [ ty. 0 ] . clone ( ) . stable ( self )
148
+ fn ty_kind ( & self , ty : stable_mir:: ty:: Ty ) -> TyKind {
149
+ let mut tables = self . 0 . borrow_mut ( ) ;
150
+ tables. types [ ty. 0 ] . clone ( ) . stable ( & mut * tables)
130
151
}
131
152
132
- fn mk_ty ( & mut self , kind : TyKind ) -> stable_mir:: ty:: Ty {
133
- let n = self . types . len ( ) ;
134
- self . types . push ( MaybeStable :: Stable ( kind) ) ;
153
+ fn mk_ty ( & self , kind : TyKind ) -> stable_mir:: ty:: Ty {
154
+ let mut tables = self . 0 . borrow_mut ( ) ;
155
+ let n = tables. types . len ( ) ;
156
+ tables. types . push ( MaybeStable :: Stable ( kind) ) ;
135
157
stable_mir:: ty:: Ty ( n)
136
158
}
137
159
138
- fn generics_of ( & mut self , def_id : stable_mir:: DefId ) -> stable_mir:: ty:: Generics {
139
- let def_id = self [ def_id] ;
140
- let generics = self . tcx . generics_of ( def_id) ;
141
- generics. stable ( self )
160
+ fn generics_of ( & self , def_id : stable_mir:: DefId ) -> stable_mir:: ty:: Generics {
161
+ let mut tables = self . 0 . borrow_mut ( ) ;
162
+ let def_id = tables[ def_id] ;
163
+ let generics = tables. tcx . generics_of ( def_id) ;
164
+ generics. stable ( & mut * tables)
142
165
}
143
166
144
- fn predicates_of ( & mut self , def_id : stable_mir:: DefId ) -> stable_mir:: ty:: GenericPredicates {
145
- let def_id = self [ def_id] ;
146
- let ty:: GenericPredicates { parent, predicates } = self . tcx . predicates_of ( def_id) ;
167
+ fn predicates_of ( & self , def_id : stable_mir:: DefId ) -> stable_mir:: ty:: GenericPredicates {
168
+ let mut tables = self . 0 . borrow_mut ( ) ;
169
+ let def_id = tables[ def_id] ;
170
+ let ty:: GenericPredicates { parent, predicates } = tables. tcx . predicates_of ( def_id) ;
147
171
stable_mir:: ty:: GenericPredicates {
148
- parent : parent. map ( |did| self . trait_def ( did) ) ,
172
+ parent : parent. map ( |did| tables . trait_def ( did) ) ,
149
173
predicates : predicates
150
174
. iter ( )
151
175
. map ( |( clause, span) | {
152
- ( clause. as_predicate ( ) . kind ( ) . skip_binder ( ) . stable ( self ) , span. stable ( self ) )
176
+ (
177
+ clause. as_predicate ( ) . kind ( ) . skip_binder ( ) . stable ( & mut * tables) ,
178
+ span. stable ( & mut * tables) ,
179
+ )
153
180
} )
154
181
. collect ( ) ,
155
182
}
156
183
}
157
184
158
185
fn explicit_predicates_of (
159
- & mut self ,
186
+ & self ,
160
187
def_id : stable_mir:: DefId ,
161
188
) -> stable_mir:: ty:: GenericPredicates {
162
- let def_id = self [ def_id] ;
163
- let ty:: GenericPredicates { parent, predicates } = self . tcx . explicit_predicates_of ( def_id) ;
189
+ let mut tables = self . 0 . borrow_mut ( ) ;
190
+ let def_id = tables[ def_id] ;
191
+ let ty:: GenericPredicates { parent, predicates } =
192
+ tables. tcx . explicit_predicates_of ( def_id) ;
164
193
stable_mir:: ty:: GenericPredicates {
165
- parent : parent. map ( |did| self . trait_def ( did) ) ,
194
+ parent : parent. map ( |did| tables . trait_def ( did) ) ,
166
195
predicates : predicates
167
196
. iter ( )
168
197
. map ( |( clause, span) | {
169
- ( clause. as_predicate ( ) . kind ( ) . skip_binder ( ) . stable ( self ) , span. stable ( self ) )
198
+ (
199
+ clause. as_predicate ( ) . kind ( ) . skip_binder ( ) . stable ( & mut * tables) ,
200
+ span. stable ( & mut * tables) ,
201
+ )
170
202
} )
171
203
. collect ( ) ,
172
204
}
173
205
}
174
206
175
- fn instance_body ( & mut self , def : InstanceDef ) -> Body {
176
- let instance = self . instances [ def] ;
177
- builder:: BodyBuilder :: new ( self . tcx , instance) . build ( self )
207
+ fn instance_body ( & self , def : InstanceDef ) -> Body {
208
+ let mut tables = self . 0 . borrow_mut ( ) ;
209
+ let instance = tables. instances [ def] ;
210
+ builder:: BodyBuilder :: new ( tables. tcx , instance) . build ( & mut * tables)
178
211
}
179
212
180
- fn instance_ty ( & mut self , def : InstanceDef ) -> stable_mir:: ty:: Ty {
181
- let instance = self . instances [ def] ;
182
- let ty = instance. ty ( self . tcx , ParamEnv :: empty ( ) ) ;
183
- self . intern_ty ( ty)
213
+ fn instance_ty ( & self , def : InstanceDef ) -> stable_mir:: ty:: Ty {
214
+ let mut tables = self . 0 . borrow_mut ( ) ;
215
+ let instance = tables. instances [ def] ;
216
+ let ty = instance. ty ( tables. tcx , ParamEnv :: empty ( ) ) ;
217
+ tables. intern_ty ( ty)
184
218
}
185
219
186
- fn instance_def_id ( & mut self , def : InstanceDef ) -> stable_mir:: DefId {
187
- let def_id = self . instances [ def] . def_id ( ) ;
188
- self . create_def_id ( def_id)
220
+ fn instance_def_id ( & self , def : InstanceDef ) -> stable_mir:: DefId {
221
+ let mut tables = self . 0 . borrow_mut ( ) ;
222
+ let def_id = tables. instances [ def] . def_id ( ) ;
223
+ tables. create_def_id ( def_id)
189
224
}
190
225
191
- fn mono_instance ( & mut self , item : stable_mir:: CrateItem ) -> stable_mir:: mir:: mono:: Instance {
192
- let def_id = self [ item. 0 ] ;
193
- Instance :: mono ( self . tcx , def_id) . stable ( self )
226
+ fn mono_instance ( & self , item : stable_mir:: CrateItem ) -> stable_mir:: mir:: mono:: Instance {
227
+ let mut tables = self . 0 . borrow_mut ( ) ;
228
+ let def_id = tables[ item. 0 ] ;
229
+ Instance :: mono ( tables. tcx , def_id) . stable ( & mut * tables)
194
230
}
195
231
196
232
fn requires_monomorphization ( & self , def_id : stable_mir:: DefId ) -> bool {
197
- let def_id = self [ def_id] ;
198
- let generics = self . tcx . generics_of ( def_id) ;
199
- let result = generics. requires_monomorphization ( self . tcx ) ;
233
+ let tables = self . 0 . borrow ( ) ;
234
+ let def_id = tables[ def_id] ;
235
+ let generics = tables. tcx . generics_of ( def_id) ;
236
+ let result = generics. requires_monomorphization ( tables. tcx ) ;
200
237
result
201
238
}
202
239
203
240
fn resolve_instance (
204
- & mut self ,
241
+ & self ,
205
242
def : stable_mir:: ty:: FnDef ,
206
243
args : & stable_mir:: ty:: GenericArgs ,
207
244
) -> Option < stable_mir:: mir:: mono:: Instance > {
208
- let def_id = def. 0 . internal ( self ) ;
209
- let args_ref = args. internal ( self ) ;
210
- match Instance :: resolve ( self . tcx , ParamEnv :: reveal_all ( ) , def_id, args_ref) {
211
- Ok ( Some ( instance) ) => Some ( instance. stable ( self ) ) ,
245
+ let mut tables = self . 0 . borrow_mut ( ) ;
246
+ let def_id = def. 0 . internal ( & mut * tables) ;
247
+ let args_ref = args. internal ( & mut * tables) ;
248
+ match Instance :: resolve ( tables. tcx , ParamEnv :: reveal_all ( ) , def_id, args_ref) {
249
+ Ok ( Some ( instance) ) => Some ( instance. stable ( & mut * tables) ) ,
212
250
Ok ( None ) | Err ( _) => None ,
213
251
}
214
252
}
@@ -241,13 +279,15 @@ impl<S, R: PartialEq> PartialEq<R> for MaybeStable<S, R> {
241
279
}
242
280
}
243
281
282
+ pub ( crate ) struct TablesWrapper < ' tcx > ( pub ( crate ) RefCell < Tables < ' tcx > > ) ;
283
+
244
284
pub struct Tables < ' tcx > {
245
- pub tcx : TyCtxt < ' tcx > ,
246
- pub def_ids : IndexMap < DefId , stable_mir:: DefId > ,
247
- pub alloc_ids : IndexMap < AllocId , stable_mir:: AllocId > ,
248
- pub spans : IndexMap < rustc_span:: Span , Span > ,
249
- pub types : Vec < MaybeStable < TyKind , Ty < ' tcx > > > ,
250
- pub instances : IndexMap < ty:: Instance < ' tcx > , InstanceDef > ,
285
+ pub ( crate ) tcx : TyCtxt < ' tcx > ,
286
+ pub ( crate ) def_ids : IndexMap < DefId , stable_mir:: DefId > ,
287
+ pub ( crate ) alloc_ids : IndexMap < AllocId , stable_mir:: AllocId > ,
288
+ pub ( crate ) spans : IndexMap < rustc_span:: Span , Span > ,
289
+ pub ( crate ) types : Vec < MaybeStable < TyKind , Ty < ' tcx > > > ,
290
+ pub ( crate ) instances : IndexMap < ty:: Instance < ' tcx > , InstanceDef > ,
251
291
}
252
292
253
293
impl < ' tcx > Tables < ' tcx > {
@@ -270,7 +310,7 @@ fn smir_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> stable_mir::Crate {
270
310
}
271
311
272
312
/// Trait used to convert between an internal MIR type to a Stable MIR type.
273
- pub ( crate ) trait Stable < ' tcx > {
313
+ pub trait Stable < ' tcx > {
274
314
/// The stable representation of the type implementing Stable.
275
315
type T ;
276
316
/// Converts an object to the equivalent Stable MIR representation.
0 commit comments