4
4
//! until stable MIR is complete.
5
5
6
6
use std:: fmt:: Debug ;
7
+ use std:: ops:: Index ;
7
8
use std:: string:: ToString ;
8
9
10
+ use crate :: rustc_internal;
9
11
use crate :: {
10
12
rustc_smir:: Tables ,
11
13
stable_mir:: { self , with} ,
12
14
} ;
15
+ use rustc_driver:: { Callbacks , Compilation , RunCompiler } ;
16
+ use rustc_interface:: { interface, Queries } ;
13
17
use rustc_middle:: ty:: TyCtxt ;
18
+ use rustc_session:: EarlyErrorHandler ;
14
19
pub use rustc_span:: def_id:: { CrateNum , DefId } ;
15
20
16
21
fn with_tables < R > ( mut f : impl FnMut ( & mut Tables < ' _ > ) -> R ) -> R {
@@ -20,7 +25,7 @@ fn with_tables<R>(mut f: impl FnMut(&mut Tables<'_>) -> R) -> R {
20
25
}
21
26
22
27
pub fn item_def_id ( item : & stable_mir:: CrateItem ) -> DefId {
23
- with_tables ( |t| t. item_def_id ( item) )
28
+ with_tables ( |t| t[ item. 0 ] )
24
29
}
25
30
26
31
pub fn crate_item ( did : DefId ) -> stable_mir:: CrateItem {
@@ -67,23 +72,16 @@ pub fn impl_def(did: DefId) -> stable_mir::ty::ImplDef {
67
72
with_tables ( |t| t. impl_def ( did) )
68
73
}
69
74
70
- impl < ' tcx > Tables < ' tcx > {
71
- pub fn item_def_id ( & self , item : & stable_mir:: CrateItem ) -> DefId {
72
- self . def_ids [ item. 0 ]
73
- }
75
+ impl < ' tcx > Index < stable_mir:: DefId > for Tables < ' tcx > {
76
+ type Output = DefId ;
74
77
75
- pub fn trait_def_id ( & self , trait_def : & stable_mir:: ty:: TraitDef ) -> DefId {
76
- self . def_ids [ trait_def. 0 ]
77
- }
78
-
79
- pub fn impl_trait_def_id ( & self , impl_def : & stable_mir:: ty:: ImplDef ) -> DefId {
80
- self . def_ids [ impl_def. 0 ]
81
- }
82
-
83
- pub fn generic_def_id ( & self , generic_def : & stable_mir:: ty:: GenericDef ) -> DefId {
84
- self . def_ids [ generic_def. 0 ]
78
+ #[ inline( always) ]
79
+ fn index ( & self , index : stable_mir:: DefId ) -> & Self :: Output {
80
+ & self . def_ids [ index. 0 ]
85
81
}
82
+ }
86
83
84
+ impl < ' tcx > Tables < ' tcx > {
87
85
pub fn crate_item ( & mut self , did : DefId ) -> stable_mir:: CrateItem {
88
86
stable_mir:: CrateItem ( self . create_def_id ( did) )
89
87
}
@@ -140,12 +138,12 @@ impl<'tcx> Tables<'tcx> {
140
138
// FIXME: this becomes inefficient when we have too many ids
141
139
for ( i, & d) in self . def_ids . iter ( ) . enumerate ( ) {
142
140
if d == did {
143
- return i ;
141
+ return stable_mir :: DefId ( i ) ;
144
142
}
145
143
}
146
144
let id = self . def_ids . len ( ) ;
147
145
self . def_ids . push ( did) ;
148
- id
146
+ stable_mir :: DefId ( id )
149
147
}
150
148
}
151
149
@@ -163,3 +161,40 @@ pub type Opaque = impl Debug + ToString + Clone;
163
161
pub ( crate ) fn opaque < T : Debug > ( value : & T ) -> Opaque {
164
162
format ! ( "{value:?}" )
165
163
}
164
+
165
+ pub struct StableMir {
166
+ args : Vec < String > ,
167
+ callback : fn ( TyCtxt < ' _ > ) ,
168
+ }
169
+
170
+ impl StableMir {
171
+ /// Creates a new `StableMir` instance, with given test_function and arguments.
172
+ pub fn new ( args : Vec < String > , callback : fn ( TyCtxt < ' _ > ) ) -> Self {
173
+ StableMir { args, callback }
174
+ }
175
+
176
+ /// Runs the compiler against given target and tests it with `test_function`
177
+ pub fn run ( & mut self ) {
178
+ rustc_driver:: catch_fatal_errors ( || {
179
+ RunCompiler :: new ( & self . args . clone ( ) , self ) . run ( ) . unwrap ( ) ;
180
+ } )
181
+ . unwrap ( ) ;
182
+ }
183
+ }
184
+
185
+ impl Callbacks for StableMir {
186
+ /// Called after analysis. Return value instructs the compiler whether to
187
+ /// continue the compilation afterwards (defaults to `Compilation::Continue`)
188
+ fn after_analysis < ' tcx > (
189
+ & mut self ,
190
+ _handler : & EarlyErrorHandler ,
191
+ _compiler : & interface:: Compiler ,
192
+ queries : & ' tcx Queries < ' tcx > ,
193
+ ) -> Compilation {
194
+ queries. global_ctxt ( ) . unwrap ( ) . enter ( |tcx| {
195
+ rustc_internal:: run ( tcx, || ( self . callback ) ( tcx) ) ;
196
+ } ) ;
197
+ // No need to keep going.
198
+ Compilation :: Stop
199
+ }
200
+ }
0 commit comments