Skip to content

Commit a8dc494

Browse files
committed
chore: replace custom Callbacks with rustc_smir::run_with_tcx!
1 parent b9fd274 commit a8dc494

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

src/main.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ extern crate rustc_driver;
44
extern crate rustc_hir;
55
extern crate rustc_interface;
66
extern crate rustc_middle;
7+
#[macro_use]
78
extern crate rustc_smir;
89
extern crate rustc_span;
910
extern crate rustc_stable_hash;
1011
extern crate stable_mir;
1112

12-
use eyre::{Context, Ok};
13-
use functions::analyze;
14-
use rustc_driver::{Compilation, run_compiler};
13+
// FIXME: this is a bug for rustc_smir, because rustc_interface is used by
14+
// run_with_tcx! without being imported inside.
15+
use rustc_smir::rustc_internal;
1516

1617
mod cli;
1718
mod functions;
@@ -23,7 +24,7 @@ extern crate tracing;
2324
fn main() {
2425
logger::init();
2526
let cli = cli::parse();
26-
let mut v = Vec::from(
27+
let mut args = Vec::from(
2728
[
2829
// the first argument to rustc is unimportant
2930
"rustc",
@@ -45,26 +46,14 @@ fn main() {
4546
]
4647
.map(String::from),
4748
);
48-
v.extend(cli.rustc_args);
49-
run_compiler(&v, &mut Callback { json: cli.json });
50-
}
51-
52-
struct Callback {
53-
json: Option<String>,
54-
}
49+
args.extend(cli.rustc_args);
5550

56-
impl rustc_driver::Callbacks for Callback {
57-
fn after_analysis<'tcx>(
58-
&mut self,
59-
_compiler: &rustc_interface::interface::Compiler,
60-
tcx: rustc_middle::ty::TyCtxt<'tcx>,
61-
) -> Compilation {
51+
let res = run_with_tcx!(args, |tcx| {
52+
use eyre::{Context, Ok};
6253
let src_map = rustc_span::source_map::get_source_map().expect("No source map.");
54+
let output = functions::analyze(tcx, &src_map);
6355

64-
let output = rustc_smir::rustc_internal::run(tcx, || analyze(tcx, &src_map))
65-
.expect("Failed to run rustc_smir.");
66-
67-
let res = || match &self.json {
56+
let res = || match &cli.json {
6857
Some(path) => {
6958
if path == "false" {
7059
return Ok(());
@@ -79,6 +68,10 @@ impl rustc_driver::Callbacks for Callback {
7968
};
8069

8170
res().unwrap();
82-
Compilation::Stop
83-
}
71+
72+
// Stop emitting artifact for the source code being compiled.
73+
ControlFlow::<(), ()>::Break(())
74+
});
75+
// rustc_smir uses `Err(CompilerError::Interrupted)` to represent ControlFlow::Break.
76+
assert!(res == Err(stable_mir::CompilerError::Interrupted(())), "Unexpected {res:?}");
8477
}

0 commit comments

Comments
 (0)