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