| 
3 | 3 | //! For that, we define APIs that will temporarily be public to 3P that exposes rustc internal APIs  | 
4 | 4 | //! until stable MIR is complete.  | 
5 | 5 | 
  | 
6 |  | -use crate::rustc_internal;  | 
7 | 6 | use crate::rustc_smir::Tables;  | 
8 | 7 | use rustc_data_structures::fx;  | 
9 | 8 | use rustc_data_structures::fx::FxIndexMap;  | 
10 |  | -use rustc_driver::{Callbacks, Compilation, RunCompiler};  | 
11 |  | -use rustc_interface::{interface, Queries};  | 
12 | 9 | use rustc_middle::mir::interpret::AllocId;  | 
13 | 10 | use rustc_middle::ty;  | 
14 | 11 | use rustc_middle::ty::TyCtxt;  | 
15 | 12 | use rustc_span::def_id::{CrateNum, DefId};  | 
16 | 13 | use rustc_span::Span;  | 
17 | 14 | use stable_mir::ty::IndexedVal;  | 
18 |  | -use stable_mir::CompilerError;  | 
19 | 15 | use std::fmt::Debug;  | 
20 | 16 | use std::hash::Hash;  | 
21 |  | -use std::ops::{ControlFlow, Index};  | 
 | 17 | +use std::ops::Index;  | 
22 | 18 | 
 
  | 
23 | 19 | impl<'tcx> Index<stable_mir::DefId> for Tables<'tcx> {  | 
24 | 20 |     type Output = DefId;  | 
@@ -141,63 +137,81 @@ pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {  | 
141 | 137 |     );  | 
142 | 138 | }  | 
143 | 139 | 
 
  | 
144 |  | -pub struct StableMir<B = (), C = ()>  | 
145 |  | -where  | 
146 |  | -    B: Send,  | 
147 |  | -    C: Send,  | 
148 |  | -{  | 
149 |  | -    args: Vec<String>,  | 
150 |  | -    callback: fn(TyCtxt<'_>) -> ControlFlow<B, C>,  | 
151 |  | -    result: Option<ControlFlow<B, C>>,  | 
152 |  | -}  | 
 | 140 | +#[macro_export]  | 
 | 141 | +macro_rules! run {  | 
 | 142 | +    ($args:expr, $callback:expr) => {  | 
 | 143 | +        run!($args, tcx, $callback)  | 
 | 144 | +    };  | 
 | 145 | +    ($args:expr, $tcx:ident, $callback:expr) => {{  | 
 | 146 | +        use rustc_driver::{Callbacks, Compilation, RunCompiler};  | 
 | 147 | +        use rustc_interface::{interface, Queries};  | 
 | 148 | +        use stable_mir::CompilerError;  | 
 | 149 | +        use std::ops::ControlFlow;  | 
 | 150 | + | 
 | 151 | +        pub struct StableMir<B = (), C = ()>  | 
 | 152 | +        where  | 
 | 153 | +            B: Send,  | 
 | 154 | +            C: Send,  | 
 | 155 | +        {  | 
 | 156 | +            args: Vec<String>,  | 
 | 157 | +            callback: fn(TyCtxt<'_>) -> ControlFlow<B, C>,  | 
 | 158 | +            result: Option<ControlFlow<B, C>>,  | 
 | 159 | +        }  | 
153 | 160 | 
 
  | 
154 |  | -impl<B, C> StableMir<B, C>  | 
155 |  | -where  | 
156 |  | -    B: Send,  | 
157 |  | -    C: Send,  | 
158 |  | -{  | 
159 |  | -    /// Creates a new `StableMir` instance, with given test_function and arguments.  | 
160 |  | -    pub fn new(args: Vec<String>, callback: fn(TyCtxt<'_>) -> ControlFlow<B, C>) -> Self {  | 
161 |  | -        StableMir { args, callback, result: None }  | 
162 |  | -    }  | 
163 |  | - | 
164 |  | -    /// Runs the compiler against given target and tests it with `test_function`  | 
165 |  | -    pub fn run(&mut self) -> Result<C, CompilerError<B>> {  | 
166 |  | -        let compiler_result =  | 
167 |  | -            rustc_driver::catch_fatal_errors(|| RunCompiler::new(&self.args.clone(), self).run());  | 
168 |  | -        match (compiler_result, self.result.take()) {  | 
169 |  | -            (Ok(Ok(())), Some(ControlFlow::Continue(value))) => Ok(value),  | 
170 |  | -            (Ok(Ok(())), Some(ControlFlow::Break(value))) => Err(CompilerError::Interrupted(value)),  | 
171 |  | -            (Ok(Ok(_)), None) => Err(CompilerError::Skipped),  | 
172 |  | -            (Ok(Err(_)), _) => Err(CompilerError::CompilationFailed),  | 
173 |  | -            (Err(_), _) => Err(CompilerError::ICE),  | 
 | 161 | +        impl<B, C> StableMir<B, C>  | 
 | 162 | +        where  | 
 | 163 | +            B: Send,  | 
 | 164 | +            C: Send,  | 
 | 165 | +        {  | 
 | 166 | +            /// Creates a new `StableMir` instance, with given test_function and arguments.  | 
 | 167 | +            pub fn new(args: Vec<String>, callback: fn(TyCtxt<'_>) -> ControlFlow<B, C>) -> Self {  | 
 | 168 | +                StableMir { args, callback, result: None }  | 
 | 169 | +            }  | 
 | 170 | + | 
 | 171 | +            /// Runs the compiler against given target and tests it with `test_function`  | 
 | 172 | +            pub fn run(&mut self) -> Result<C, CompilerError<B>> {  | 
 | 173 | +                let compiler_result = rustc_driver::catch_fatal_errors(|| {  | 
 | 174 | +                    RunCompiler::new(&self.args.clone(), self).run()  | 
 | 175 | +                });  | 
 | 176 | +                match (compiler_result, self.result.take()) {  | 
 | 177 | +                    (Ok(Ok(())), Some(ControlFlow::Continue(value))) => Ok(value),  | 
 | 178 | +                    (Ok(Ok(())), Some(ControlFlow::Break(value))) => {  | 
 | 179 | +                        Err(CompilerError::Interrupted(value))  | 
 | 180 | +                    }  | 
 | 181 | +                    (Ok(Ok(_)), None) => Err(CompilerError::Skipped),  | 
 | 182 | +                    (Ok(Err(_)), _) => Err(CompilerError::CompilationFailed),  | 
 | 183 | +                    (Err(_), _) => Err(CompilerError::ICE),  | 
 | 184 | +                }  | 
 | 185 | +            }  | 
174 | 186 |         }  | 
175 |  | -    }  | 
176 |  | -}  | 
177 | 187 | 
 
  | 
178 |  | -impl<B, C> Callbacks for StableMir<B, C>  | 
179 |  | -where  | 
180 |  | -    B: Send,  | 
181 |  | -    C: Send,  | 
182 |  | -{  | 
183 |  | -    /// Called after analysis. Return value instructs the compiler whether to  | 
184 |  | -    /// continue the compilation afterwards (defaults to `Compilation::Continue`)  | 
185 |  | -    fn after_analysis<'tcx>(  | 
186 |  | -        &mut self,  | 
187 |  | -        _compiler: &interface::Compiler,  | 
188 |  | -        queries: &'tcx Queries<'tcx>,  | 
189 |  | -    ) -> Compilation {  | 
190 |  | -        queries.global_ctxt().unwrap().enter(|tcx| {  | 
191 |  | -            rustc_internal::run(tcx, || {  | 
192 |  | -                self.result = Some((self.callback)(tcx));  | 
193 |  | -            });  | 
194 |  | -            if self.result.as_ref().is_some_and(|val| val.is_continue()) {  | 
195 |  | -                Compilation::Continue  | 
196 |  | -            } else {  | 
197 |  | -                Compilation::Stop  | 
 | 188 | +        impl<B, C> Callbacks for StableMir<B, C>  | 
 | 189 | +        where  | 
 | 190 | +            B: Send,  | 
 | 191 | +            C: Send,  | 
 | 192 | +        {  | 
 | 193 | +            /// Called after analysis. Return value instructs the compiler whether to  | 
 | 194 | +            /// continue the compilation afterwards (defaults to `Compilation::Continue`)  | 
 | 195 | +            fn after_analysis<'tcx>(  | 
 | 196 | +                &mut self,  | 
 | 197 | +                _compiler: &interface::Compiler,  | 
 | 198 | +                queries: &'tcx Queries<'tcx>,  | 
 | 199 | +            ) -> Compilation {  | 
 | 200 | +                queries.global_ctxt().unwrap().enter(|tcx| {  | 
 | 201 | +                    rustc_internal::run(tcx, || {  | 
 | 202 | +                        self.result = Some((self.callback)(tcx));  | 
 | 203 | +                    });  | 
 | 204 | +                    if self.result.as_ref().is_some_and(|val| val.is_continue()) {  | 
 | 205 | +                        Compilation::Continue  | 
 | 206 | +                    } else {  | 
 | 207 | +                        Compilation::Stop  | 
 | 208 | +                    }  | 
 | 209 | +                })  | 
198 | 210 |             }  | 
199 |  | -        })  | 
200 |  | -    }  | 
 | 211 | +        }  | 
 | 212 | + | 
 | 213 | +        StableMir::new($args, |$tcx| $callback).run()  | 
 | 214 | +    }};  | 
201 | 215 | }  | 
202 | 216 | 
 
  | 
203 | 217 | /// Simmilar to rustc's `FxIndexMap`, `IndexMap` with extra  | 
 | 
0 commit comments