|
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 | mod internal;
|
24 | 20 |
|
@@ -143,63 +139,81 @@ pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
|
143 | 139 | );
|
144 | 140 | }
|
145 | 141 |
|
146 |
| -pub struct StableMir<B = (), C = ()> |
147 |
| -where |
148 |
| - B: Send, |
149 |
| - C: Send, |
150 |
| -{ |
151 |
| - args: Vec<String>, |
152 |
| - callback: fn(TyCtxt<'_>) -> ControlFlow<B, C>, |
153 |
| - result: Option<ControlFlow<B, C>>, |
154 |
| -} |
| 142 | +#[macro_export] |
| 143 | +macro_rules! run { |
| 144 | + ($args:expr, $callback:expr) => { |
| 145 | + run!($args, tcx, $callback) |
| 146 | + }; |
| 147 | + ($args:expr, $tcx:ident, $callback:expr) => {{ |
| 148 | + use rustc_driver::{Callbacks, Compilation, RunCompiler}; |
| 149 | + use rustc_interface::{interface, Queries}; |
| 150 | + use stable_mir::CompilerError; |
| 151 | + use std::ops::ControlFlow; |
| 152 | + |
| 153 | + pub struct StableMir<B = (), C = ()> |
| 154 | + where |
| 155 | + B: Send, |
| 156 | + C: Send, |
| 157 | + { |
| 158 | + args: Vec<String>, |
| 159 | + callback: fn(TyCtxt<'_>) -> ControlFlow<B, C>, |
| 160 | + result: Option<ControlFlow<B, C>>, |
| 161 | + } |
155 | 162 |
|
156 |
| -impl<B, C> StableMir<B, C> |
157 |
| -where |
158 |
| - B: Send, |
159 |
| - C: Send, |
160 |
| -{ |
161 |
| - /// Creates a new `StableMir` instance, with given test_function and arguments. |
162 |
| - pub fn new(args: Vec<String>, callback: fn(TyCtxt<'_>) -> ControlFlow<B, C>) -> Self { |
163 |
| - StableMir { args, callback, result: None } |
164 |
| - } |
165 |
| - |
166 |
| - /// Runs the compiler against given target and tests it with `test_function` |
167 |
| - pub fn run(&mut self) -> Result<C, CompilerError<B>> { |
168 |
| - let compiler_result = |
169 |
| - rustc_driver::catch_fatal_errors(|| RunCompiler::new(&self.args.clone(), self).run()); |
170 |
| - match (compiler_result, self.result.take()) { |
171 |
| - (Ok(Ok(())), Some(ControlFlow::Continue(value))) => Ok(value), |
172 |
| - (Ok(Ok(())), Some(ControlFlow::Break(value))) => Err(CompilerError::Interrupted(value)), |
173 |
| - (Ok(Ok(_)), None) => Err(CompilerError::Skipped), |
174 |
| - (Ok(Err(_)), _) => Err(CompilerError::CompilationFailed), |
175 |
| - (Err(_), _) => Err(CompilerError::ICE), |
| 163 | + impl<B, C> StableMir<B, C> |
| 164 | + where |
| 165 | + B: Send, |
| 166 | + C: Send, |
| 167 | + { |
| 168 | + /// Creates a new `StableMir` instance, with given test_function and arguments. |
| 169 | + pub fn new(args: Vec<String>, callback: fn(TyCtxt<'_>) -> ControlFlow<B, C>) -> Self { |
| 170 | + StableMir { args, callback, result: None } |
| 171 | + } |
| 172 | + |
| 173 | + /// Runs the compiler against given target and tests it with `test_function` |
| 174 | + pub fn run(&mut self) -> Result<C, CompilerError<B>> { |
| 175 | + let compiler_result = rustc_driver::catch_fatal_errors(|| { |
| 176 | + RunCompiler::new(&self.args.clone(), self).run() |
| 177 | + }); |
| 178 | + match (compiler_result, self.result.take()) { |
| 179 | + (Ok(Ok(())), Some(ControlFlow::Continue(value))) => Ok(value), |
| 180 | + (Ok(Ok(())), Some(ControlFlow::Break(value))) => { |
| 181 | + Err(CompilerError::Interrupted(value)) |
| 182 | + } |
| 183 | + (Ok(Ok(_)), None) => Err(CompilerError::Skipped), |
| 184 | + (Ok(Err(_)), _) => Err(CompilerError::CompilationFailed), |
| 185 | + (Err(_), _) => Err(CompilerError::ICE), |
| 186 | + } |
| 187 | + } |
176 | 188 | }
|
177 |
| - } |
178 |
| -} |
179 | 189 |
|
180 |
| -impl<B, C> Callbacks for StableMir<B, C> |
181 |
| -where |
182 |
| - B: Send, |
183 |
| - C: Send, |
184 |
| -{ |
185 |
| - /// Called after analysis. Return value instructs the compiler whether to |
186 |
| - /// continue the compilation afterwards (defaults to `Compilation::Continue`) |
187 |
| - fn after_analysis<'tcx>( |
188 |
| - &mut self, |
189 |
| - _compiler: &interface::Compiler, |
190 |
| - queries: &'tcx Queries<'tcx>, |
191 |
| - ) -> Compilation { |
192 |
| - queries.global_ctxt().unwrap().enter(|tcx| { |
193 |
| - rustc_internal::run(tcx, || { |
194 |
| - self.result = Some((self.callback)(tcx)); |
195 |
| - }); |
196 |
| - if self.result.as_ref().is_some_and(|val| val.is_continue()) { |
197 |
| - Compilation::Continue |
198 |
| - } else { |
199 |
| - Compilation::Stop |
| 190 | + impl<B, C> Callbacks for StableMir<B, C> |
| 191 | + where |
| 192 | + B: Send, |
| 193 | + C: Send, |
| 194 | + { |
| 195 | + /// Called after analysis. Return value instructs the compiler whether to |
| 196 | + /// continue the compilation afterwards (defaults to `Compilation::Continue`) |
| 197 | + fn after_analysis<'tcx>( |
| 198 | + &mut self, |
| 199 | + _compiler: &interface::Compiler, |
| 200 | + queries: &'tcx Queries<'tcx>, |
| 201 | + ) -> Compilation { |
| 202 | + queries.global_ctxt().unwrap().enter(|tcx| { |
| 203 | + rustc_internal::run(tcx, || { |
| 204 | + self.result = Some((self.callback)(tcx)); |
| 205 | + }); |
| 206 | + if self.result.as_ref().is_some_and(|val| val.is_continue()) { |
| 207 | + Compilation::Continue |
| 208 | + } else { |
| 209 | + Compilation::Stop |
| 210 | + } |
| 211 | + }) |
200 | 212 | }
|
201 |
| - }) |
202 |
| - } |
| 213 | + } |
| 214 | + |
| 215 | + StableMir::new($args, |$tcx| $callback).run() |
| 216 | + }}; |
203 | 217 | }
|
204 | 218 |
|
205 | 219 | /// Simmilar to rustc's `FxIndexMap`, `IndexMap` with extra
|
|
0 commit comments