4
4
//! until stable MIR is complete.
5
5
6
6
use std:: fmt:: Debug ;
7
- use std:: ops:: Index ;
7
+ use std:: ops:: { ControlFlow , Index } ;
8
8
9
9
use crate :: rustc_internal;
10
10
use crate :: stable_mir:: CompilerError ;
@@ -190,52 +190,44 @@ pub(crate) fn opaque<T: Debug>(value: &T) -> Opaque {
190
190
Opaque ( format ! ( "{value:?}" ) )
191
191
}
192
192
193
- pub struct StableMir < T : Send >
193
+ pub struct StableMir < B = ( ) , C = ( ) >
194
194
where
195
- T : Send ,
195
+ B : Send ,
196
+ C : Send ,
196
197
{
197
198
args : Vec < String > ,
198
- callback : fn ( TyCtxt < ' _ > ) -> T ,
199
- after_analysis : Compilation ,
200
- result : Option < T > ,
199
+ callback : fn ( TyCtxt < ' _ > ) -> ControlFlow < B , C > ,
200
+ result : Option < ControlFlow < B , C > > ,
201
201
}
202
202
203
- impl < T > StableMir < T >
203
+ impl < B , C > StableMir < B , C >
204
204
where
205
- T : Send ,
205
+ B : Send ,
206
+ C : Send ,
206
207
{
207
208
/// Creates a new `StableMir` instance, with given test_function and arguments.
208
- pub fn new ( args : Vec < String > , callback : fn ( TyCtxt < ' _ > ) -> T ) -> Self {
209
- StableMir { args, callback, result : None , after_analysis : Compilation :: Stop }
210
- }
211
-
212
- /// Configure object to stop compilation after callback is called.
213
- pub fn stop_compilation ( & mut self ) -> & mut Self {
214
- self . after_analysis = Compilation :: Stop ;
215
- self
216
- }
217
-
218
- /// Configure object to continue compilation after callback is called.
219
- pub fn continue_compilation ( & mut self ) -> & mut Self {
220
- self . after_analysis = Compilation :: Continue ;
221
- self
209
+ pub fn new ( args : Vec < String > , callback : fn ( TyCtxt < ' _ > ) -> ControlFlow < B , C > ) -> Self {
210
+ StableMir { args, callback, result : None }
222
211
}
223
212
224
213
/// Runs the compiler against given target and tests it with `test_function`
225
- pub fn run ( & mut self ) -> Result < T , CompilerError > {
214
+ pub fn run ( & mut self ) -> Result < C , CompilerError < B > > {
226
215
let compiler_result =
227
216
rustc_driver:: catch_fatal_errors ( || RunCompiler :: new ( & self . args . clone ( ) , self ) . run ( ) ) ;
228
- match compiler_result {
229
- Ok ( Ok ( ( ) ) ) => Ok ( self . result . take ( ) . unwrap ( ) ) ,
230
- Ok ( Err ( _) ) => Err ( CompilerError :: CompilationFailed ) ,
231
- Err ( _) => Err ( CompilerError :: ICE ) ,
217
+ match ( compiler_result, self . result . take ( ) ) {
218
+ ( Ok ( Ok ( ( ) ) ) , Some ( ControlFlow :: Continue ( value) ) ) => Ok ( value) ,
219
+ ( Ok ( Ok ( ( ) ) ) , Some ( ControlFlow :: Break ( value) ) ) => Err ( CompilerError :: Interrupted ( value) ) ,
220
+ ( Ok ( Ok ( _) ) , None ) => Err ( CompilerError :: Skipped ) ,
221
+ ( Ok ( Err ( _) ) , _) => Err ( CompilerError :: CompilationFailed ) ,
222
+ ( Err ( _) , _) => Err ( CompilerError :: ICE ) ,
232
223
}
233
224
}
234
225
}
235
226
236
- impl < T > Callbacks for StableMir < T >
227
+ impl < B , C > Callbacks for StableMir < B , C >
237
228
where
238
- T : Send ,
229
+ B : Send ,
230
+ C : Send ,
239
231
{
240
232
/// Called after analysis. Return value instructs the compiler whether to
241
233
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
@@ -249,8 +241,11 @@ where
249
241
rustc_internal:: run ( tcx, || {
250
242
self . result = Some ( ( self . callback ) ( tcx) ) ;
251
243
} ) ;
252
- } ) ;
253
- // Let users define if they want to stop compilation.
254
- self . after_analysis
244
+ if self . result . as_ref ( ) . is_some_and ( |val| val. is_continue ( ) ) {
245
+ Compilation :: Continue
246
+ } else {
247
+ Compilation :: Stop
248
+ }
249
+ } )
255
250
}
256
251
}
0 commit comments