44//! until stable MIR is complete.
55
66use std:: fmt:: Debug ;
7- use std:: ops:: Index ;
7+ use std:: ops:: { ControlFlow , Index } ;
88
99use crate :: rustc_internal;
1010use crate :: stable_mir:: CompilerError ;
@@ -190,52 +190,44 @@ pub(crate) fn opaque<T: Debug>(value: &T) -> Opaque {
190190 Opaque ( format ! ( "{value:?}" ) )
191191}
192192
193- pub struct StableMir < T : Send >
193+ pub struct StableMir < B = ( ) , C = ( ) >
194194where
195- T : Send ,
195+ B : Send ,
196+ C : Send ,
196197{
197198 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 > > ,
201201}
202202
203- impl < T > StableMir < T >
203+ impl < B , C > StableMir < B , C >
204204where
205- T : Send ,
205+ B : Send ,
206+ C : Send ,
206207{
207208 /// 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 }
222211 }
223212
224213 /// 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 > > {
226215 let compiler_result =
227216 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 ) ,
232223 }
233224 }
234225}
235226
236- impl < T > Callbacks for StableMir < T >
227+ impl < B , C > Callbacks for StableMir < B , C >
237228where
238- T : Send ,
229+ B : Send ,
230+ C : Send ,
239231{
240232 /// Called after analysis. Return value instructs the compiler whether to
241233 /// continue the compilation afterwards (defaults to `Compilation::Continue`)
@@ -249,8 +241,11 @@ where
249241 rustc_internal:: run ( tcx, || {
250242 self . result = Some ( ( self . callback ) ( tcx) ) ;
251243 } ) ;
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+ } )
255250 }
256251}
0 commit comments