Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ba03619

Browse files
committedOct 17, 2023
Avoid having rustc_smir depend on rustc_interface or rustc_driver
1 parent 631a116 commit ba03619

File tree

5 files changed

+85
-71
lines changed

5 files changed

+85
-71
lines changed
 

‎Cargo.lock

-2
Original file line numberDiff line numberDiff line change
@@ -4481,9 +4481,7 @@ name = "rustc_smir"
44814481
version = "0.0.0"
44824482
dependencies = [
44834483
"rustc_data_structures",
4484-
"rustc_driver",
44854484
"rustc_hir",
4486-
"rustc_interface",
44874485
"rustc_middle",
44884486
"rustc_span",
44894487
"rustc_target",

‎compiler/rustc_smir/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
rustc_data_structures = { path = "../rustc_data_structures" }
8-
rustc_driver = { path = "../rustc_driver" }
98
rustc_hir = { path = "../rustc_hir" }
10-
rustc_interface = { path = "../rustc_interface" }
119
rustc_middle = { path = "../rustc_middle" }
1210
rustc_span = { path = "../rustc_span" }
1311
rustc_target = { path = "../rustc_target" }

‎compiler/rustc_smir/src/rustc_internal/mod.rs

+72-57
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@
66
use crate::rustc_internal;
77
use crate::rustc_smir::Tables;
88
use rustc_data_structures::fx;
9-
use rustc_driver::{Callbacks, Compilation, RunCompiler};
10-
use rustc_interface::{interface, Queries};
119
use rustc_middle::mir::interpret::AllocId;
1210
use rustc_middle::ty::TyCtxt;
1311
use rustc_span::def_id::{CrateNum, DefId};
1412
use rustc_span::Span;
1513
use stable_mir::ty::IndexedVal;
16-
use stable_mir::CompilerError;
1714
use std::fmt::Debug;
1815
use std::hash::Hash;
19-
use std::ops::{ControlFlow, Index};
16+
use std::ops::Index;
2017

2118
impl<'tcx> Index<stable_mir::DefId> for Tables<'tcx> {
2219
type Output = DefId;
@@ -127,63 +124,81 @@ pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
127124
);
128125
}
129126

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+
}
139147

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+
}
160173
}
161-
}
162-
}
163174

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+
})
184197
}
185-
})
186-
}
198+
}
199+
200+
StableMir::new($args, |$tcx| $callback).run()
201+
}};
187202
}
188203

189204
/// Simmilar to rustc's `FxIndexMap`, `IndexMap` with extra

‎tests/ui-fulldeps/stable-mir/compilation-result.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
#![feature(assert_matches)]
1111

1212
extern crate rustc_middle;
13+
#[macro_use]
1314
extern crate rustc_smir;
15+
extern crate rustc_driver;
16+
extern crate rustc_interface;
1417
extern crate stable_mir;
1518

1619
use rustc_middle::ty::TyCtxt;
1720
use rustc_smir::rustc_internal;
1821
use std::io::Write;
19-
use std::ops::ControlFlow;
2022

2123
/// This test will generate and analyze a dummy crate using the stable mir.
2224
/// For that, it will first write the dummy crate into a file.
@@ -33,28 +35,26 @@ fn main() {
3335
}
3436

3537
fn test_continue(args: Vec<String>) {
36-
let continue_fn = |_: TyCtxt| ControlFlow::Continue::<(), bool>(true);
37-
let result = rustc_internal::StableMir::new(args, continue_fn).run();
38+
let result = run!(args, ControlFlow::Continue::<(), bool>(true));
3839
assert_eq!(result, Ok(true));
3940
}
4041

4142
fn test_break(args: Vec<String>) {
42-
let continue_fn = |_: TyCtxt| ControlFlow::Break::<bool, i32>(false);
43-
let result = rustc_internal::StableMir::new(args, continue_fn).run();
43+
let result = run!(args, ControlFlow::Break::<bool, i32>(false));
4444
assert_eq!(result, Err(stable_mir::CompilerError::Interrupted(false)));
4545
}
4646

47+
#[allow(unreachable_code)]
4748
fn test_skipped(mut args: Vec<String>) {
4849
args.push("--version".to_string());
49-
let unreach_fn = |_: TyCtxt| -> ControlFlow<()> { unreachable!() };
50-
let result = rustc_internal::StableMir::new(args, unreach_fn).run();
50+
let result = run!(args, unreachable!() as ControlFlow<()>);
5151
assert_eq!(result, Err(stable_mir::CompilerError::Skipped));
5252
}
5353

54+
#[allow(unreachable_code)]
5455
fn test_failed(mut args: Vec<String>) {
5556
args.push("--cfg=broken".to_string());
56-
let unreach_fn = |_: TyCtxt| -> ControlFlow<()> { unreachable!() };
57-
let result = rustc_internal::StableMir::new(args, unreach_fn).run();
57+
let result = run!(args, unreachable!() as ControlFlow<()>);
5858
assert_eq!(result, Err(stable_mir::CompilerError::CompilationFailed));
5959
}
6060

‎tests/ui-fulldeps/stable-mir/crate-info.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212

1313
extern crate rustc_hir;
1414
extern crate rustc_middle;
15+
#[macro_use]
1516
extern crate rustc_smir;
17+
extern crate rustc_driver;
18+
extern crate rustc_interface;
1619
extern crate stable_mir;
1720

1821
use rustc_hir::def::DefKind;
@@ -185,7 +188,7 @@ fn main() {
185188
CRATE_NAME.to_string(),
186189
path.to_string(),
187190
];
188-
rustc_internal::StableMir::new(args, test_stable_mir).run().unwrap();
191+
run!(args, tcx, test_stable_mir(tcx)).unwrap();
189192
}
190193

191194
fn generate_input(path: &str) -> std::io::Result<()> {

0 commit comments

Comments
 (0)
Please sign in to comment.