Skip to content

Commit 7d142ec

Browse files
Zoxcoli-obk
authored andcommitted
Use the new rustc interface
1 parent e66d6ec commit 7d142ec

File tree

3 files changed

+99
-213
lines changed

3 files changed

+99
-213
lines changed

benches/helpers/miri_helper.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,39 @@ extern crate getopts;
22
extern crate miri;
33
extern crate rustc;
44
extern crate rustc_driver;
5+
extern crate rustc_interface;
56
extern crate test;
67

7-
use rustc_driver::{driver, Compilation};
8+
use self::miri::eval_main;
89
use rustc::hir::def_id::LOCAL_CRATE;
9-
use std::cell::RefCell;
10-
use std::rc::Rc;
10+
use rustc_interface::interface;
11+
use crate::test::Bencher;
12+
13+
struct MiriCompilerCalls<'a> {
14+
bencher: &'a mut Bencher,
15+
}
1116

12-
use miri::{MiriConfig, eval_main};
17+
impl rustc_driver::Callbacks for MiriCompilerCalls<'_> {
18+
fn after_analysis(&mut self, compiler: &interface::Compiler<'_>) -> bool {
19+
compiler.session().abort_if_errors();
1320

14-
use crate::test::Bencher;
21+
compiler.global_ctxt().unwrap().peek_mut().enter(|tcx| {
22+
let (entry_def_id, _) = tcx.entry_fn(LOCAL_CRATE).expect(
23+
"no main or start function found",
24+
);
1525

16-
pub struct MiriCompilerCalls<'a>(Rc<RefCell<&'a mut Bencher>>);
26+
self.bencher.iter(|| {
27+
let config = MiriConfig { validate: true, args: vec![] };
28+
eval_main(tcx, entry_def_id, config);
29+
});
30+
});
31+
32+
compiler.session().abort_if_errors();
33+
34+
// Don't continue execution
35+
false
36+
}
37+
}
1738

1839
fn find_sysroot() -> String {
1940
// Taken from https://github.com/Manishearth/rust-clippy/pull/911.
@@ -38,26 +59,5 @@ pub fn run(filename: &str, bencher: &mut Bencher) {
3859
"--sysroot".to_string(),
3960
find_sysroot(),
4061
];
41-
let bencher = RefCell::new(bencher);
42-
43-
let mut control = driver::CompileController::basic();
44-
45-
control.after_analysis.stop = Compilation::Stop;
46-
control.after_analysis.callback = Box::new(move |state| {
47-
state.session.abort_if_errors();
48-
49-
let tcx = state.tcx.unwrap();
50-
let (entry_def_id, _) = tcx.entry_fn(LOCAL_CRATE).expect(
51-
"no main or start function found",
52-
);
53-
54-
bencher.borrow_mut().iter(|| {
55-
let config = MiriConfig { validate: true, args: vec![] };
56-
eval_main(tcx, entry_def_id, config);
57-
});
58-
59-
state.session.abort_if_errors();
60-
});
61-
62-
rustc_driver::run_compiler(args, Box::new(control), None, None);
62+
rustc_driver::run_compiler(args, &mut MiriCompilerCalls { bencher }, None, None);
6363
}

src/bin/miri-rustc-tests.rs

Lines changed: 42 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern crate rustc_metadata;
66
extern crate rustc_driver;
77
extern crate rustc_errors;
88
extern crate rustc_codegen_utils;
9+
extern crate rustc_interface;
910
extern crate syntax;
1011

1112
use std::path::{PathBuf, Path};
@@ -15,106 +16,66 @@ use std::io;
1516

1617

1718
use rustc::session::Session;
19+
use rustc_interface::interface;
1820
use rustc_metadata::cstore::CStore;
19-
use rustc_driver::{Compilation, CompilerCalls, RustcDefaultCalls};
20-
use rustc_driver::driver::{CompileState, CompileController};
2121
use rustc::session::config::{self, Input, ErrorOutputType};
2222
use rustc::hir::{self, itemlikevisit};
23-
use rustc_codegen_utils::codegen_backend::CodegenBackend;
2423
use rustc::ty::TyCtxt;
2524
use syntax::ast;
2625
use rustc::hir::def_id::LOCAL_CRATE;
2726

2827
use miri::MiriConfig;
2928

3029
struct MiriCompilerCalls {
31-
default: Box<RustcDefaultCalls>,
3230
/// whether we are building for the host
3331
host_target: bool,
3432
}
3533

36-
impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
37-
fn early_callback(
38-
&mut self,
39-
matches: &getopts::Matches,
40-
sopts: &config::Options,
41-
cfg: &ast::CrateConfig,
42-
descriptions: &rustc_errors::registry::Registry,
43-
output: ErrorOutputType
44-
) -> Compilation {
45-
self.default.early_callback(matches, sopts, cfg, descriptions, output)
46-
}
47-
fn no_input(
48-
&mut self,
49-
matches: &getopts::Matches,
50-
sopts: &config::Options,
51-
cfg: &ast::CrateConfig,
52-
odir: &Option<PathBuf>,
53-
ofile: &Option<PathBuf>,
54-
descriptions: &rustc_errors::registry::Registry
55-
) -> Option<(Input, Option<PathBuf>)> {
56-
self.default.no_input(matches, sopts, cfg, odir, ofile, descriptions)
57-
}
58-
fn late_callback(
59-
&mut self,
60-
trans: &CodegenBackend,
61-
matches: &getopts::Matches,
62-
sess: &Session,
63-
cstore: &CStore,
64-
input: &Input,
65-
odir: &Option<PathBuf>,
66-
ofile: &Option<PathBuf>,
67-
) -> Compilation {
68-
self.default.late_callback(trans, matches, sess, cstore, input, odir, ofile)
69-
}
70-
fn build_controller(self: Box<Self>, sess: &Session, matches: &getopts::Matches) -> CompileController<'a> {
71-
let this = *self;
72-
let mut control = this.default.build_controller(sess, matches);
73-
control.after_hir_lowering.callback = Box::new(after_hir_lowering);
74-
control.after_analysis.callback = Box::new(after_analysis);
75-
if !this.host_target {
76-
// only fully compile targets on the host
77-
control.after_analysis.stop = Compilation::Stop;
78-
}
79-
control
80-
}
81-
}
82-
83-
fn after_hir_lowering(state: &mut CompileState) {
84-
let attr = (String::from("miri"), syntax::feature_gate::AttributeType::Whitelisted);
85-
state.session.plugin_attributes.borrow_mut().push(attr);
86-
}
34+
impl rustc_driver::Callbacks for MiriCompilerCalls {
35+
fn after_parsing(&mut self, compiler: &interface::Compiler<'_>) -> bool {
36+
let attr = (
37+
String::from("miri"),
38+
syntax::feature_gate::AttributeType::Whitelisted,
39+
);
40+
compiler.session().plugin_attributes.borrow_mut().push(attr);
8741

88-
fn after_analysis<'a, 'tcx>(state: &mut CompileState<'a, 'tcx>) {
89-
state.session.abort_if_errors();
90-
91-
let tcx = state.tcx.unwrap();
42+
// Continue execution
43+
true
44+
}
9245

93-
if std::env::args().any(|arg| arg == "--test") {
94-
struct Visitor<'a, 'tcx: 'a>(TyCtxt<'a, 'tcx, 'tcx>, &'a CompileState<'a, 'tcx>);
95-
impl<'a, 'tcx: 'a, 'hir> itemlikevisit::ItemLikeVisitor<'hir> for Visitor<'a, 'tcx> {
96-
fn visit_item(&mut self, i: &'hir hir::Item) {
97-
if let hir::ItemKind::Fn(.., body_id) = i.node {
98-
if i.attrs.iter().any(|attr| attr.name() == "test") {
99-
let config = MiriConfig { validate: true, args: vec![] };
100-
let did = self.0.hir().body_owner_def_id(body_id);
101-
println!("running test: {}", self.0.def_path_debug_str(did));
102-
miri::eval_main(self.0, did, config);
103-
self.1.session.abort_if_errors();
46+
fn after_analysis(&mut self, compiler: &interface::Compiler<'_>) -> bool {
47+
compiler.session().abort_if_errors();
48+
compiler.global_ctxt().unwrap().peek_mut().enter(|tcx| {
49+
if std::env::args().any(|arg| arg == "--test") {
50+
struct Visitor<'a, 'tcx: 'a>(TyCtxt<'a, 'tcx, 'tcx>);
51+
impl<'a, 'tcx: 'a, 'hir> itemlikevisit::ItemLikeVisitor<'hir> for Visitor<'a, 'tcx> {
52+
fn visit_item(&mut self, i: &'hir hir::Item) {
53+
if let hir::ItemKind::Fn(.., body_id) = i.node {
54+
if i.attrs.iter().any(|attr| attr.name() == "test") {
55+
let config = MiriConfig { validate: true, args: vec![] };
56+
let did = self.0.hir().body_owner_def_id(body_id);
57+
println!("running test: {}", self.0.def_path_debug_str(did));
58+
miri::eval_main(self.0, did, config);
59+
self.0.sess.abort_if_errors();
60+
}
61+
}
10462
}
63+
fn visit_trait_item(&mut self, _trait_item: &'hir hir::TraitItem) {}
64+
fn visit_impl_item(&mut self, _impl_item: &'hir hir::ImplItem) {}
10565
}
66+
tcx.hir().krate().visit_all_item_likes(&mut Visitor(tcx));
67+
} else if let Some((entry_def_id, _)) = tcx.entry_fn(LOCAL_CRATE) {
68+
let config = MiriConfig { validate: true, args: vec![] };
69+
miri::eval_main(tcx, entry_def_id, config);
70+
71+
compiler.session().abort_if_errors();
72+
} else {
73+
println!("no main function found, assuming auxiliary build");
10674
}
107-
fn visit_trait_item(&mut self, _trait_item: &'hir hir::TraitItem) {}
108-
fn visit_impl_item(&mut self, _impl_item: &'hir hir::ImplItem) {}
109-
}
110-
state.hir_crate.unwrap().visit_all_item_likes(&mut Visitor(tcx, state));
111-
} else if let Some((entry_def_id, _)) = tcx.entry_fn(LOCAL_CRATE) {
112-
let config = MiriConfig { validate: true, args: vec![] };
113-
miri::eval_main(tcx, entry_def_id, config);
75+
});
11476

115-
state.session.abort_if_errors();
116-
} else {
117-
println!("no main function found, assuming auxiliary build");
77+
// Continue execution on host target
78+
self.host_target
11879
}
11980
}
12081

@@ -185,10 +146,7 @@ fn main() {
185146
let buf = BufWriter::default();
186147
let output = buf.clone();
187148
let result = std::panic::catch_unwind(|| {
188-
rustc_driver::run_compiler(&args, Box::new(MiriCompilerCalls {
189-
default: Box::new(RustcDefaultCalls),
190-
host_target,
191-
}), None, Some(Box::new(buf)));
149+
rustc_driver::run_compiler(&args, &mut MiriCompilerCalls { host_target }, None, Some(Box::new(buf)));
192150
});
193151

194152
match result {

src/bin/miri.rs

Lines changed: 29 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -11,115 +11,46 @@ extern crate rustc_metadata;
1111
extern crate rustc_driver;
1212
extern crate rustc_errors;
1313
extern crate rustc_codegen_utils;
14+
extern crate rustc_interface;
1415
extern crate syntax;
1516

16-
use std::path::PathBuf;
1717
use std::str::FromStr;
1818
use std::env;
1919

20-
use miri::MiriConfig;
21-
use rustc::session::Session;
22-
use rustc_metadata::cstore::CStore;
23-
use rustc_driver::{Compilation, CompilerCalls, RustcDefaultCalls};
24-
use rustc_driver::driver::{CompileState, CompileController};
25-
use rustc::session::config::{self, Input, ErrorOutputType};
26-
use rustc_codegen_utils::codegen_backend::CodegenBackend;
20+
use rustc_interface::interface;
2721
use rustc::hir::def_id::LOCAL_CRATE;
28-
use syntax::ast;
2922

3023
struct MiriCompilerCalls {
31-
default: Box<RustcDefaultCalls>,
32-
miri_config: MiriConfig,
24+
miri_config: miri::MiriConfig,
3325
}
3426

35-
impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
36-
fn early_callback(
37-
&mut self,
38-
matches: &getopts::Matches,
39-
sopts: &config::Options,
40-
cfg: &ast::CrateConfig,
41-
descriptions: &rustc_errors::registry::Registry,
42-
output: ErrorOutputType,
43-
) -> Compilation {
44-
self.default.early_callback(
45-
matches,
46-
sopts,
47-
cfg,
48-
descriptions,
49-
output,
50-
)
51-
}
52-
fn no_input(
53-
&mut self,
54-
matches: &getopts::Matches,
55-
sopts: &config::Options,
56-
cfg: &ast::CrateConfig,
57-
odir: &Option<PathBuf>,
58-
ofile: &Option<PathBuf>,
59-
descriptions: &rustc_errors::registry::Registry,
60-
) -> Option<(Input, Option<PathBuf>)> {
61-
self.default.no_input(
62-
matches,
63-
sopts,
64-
cfg,
65-
odir,
66-
ofile,
67-
descriptions,
68-
)
69-
}
70-
fn late_callback(
71-
&mut self,
72-
codegen_backend: &CodegenBackend,
73-
matches: &getopts::Matches,
74-
sess: &Session,
75-
cstore: &CStore,
76-
input: &Input,
77-
odir: &Option<PathBuf>,
78-
ofile: &Option<PathBuf>,
79-
) -> Compilation {
80-
// Called *before* `build_controller`. Add filename to `miri` arguments.
81-
self.miri_config.args.insert(0, input.filestem().to_string());
82-
self.default.late_callback(codegen_backend, matches, sess, cstore, input, odir, ofile)
83-
}
84-
fn build_controller(
85-
self: Box<Self>,
86-
sess: &Session,
87-
matches: &getopts::Matches,
88-
) -> CompileController<'a> {
89-
let this = *self;
90-
let mut control = this.default.build_controller(sess, matches);
91-
control.after_hir_lowering.callback = Box::new(after_hir_lowering);
92-
let miri_config = this.miri_config;
93-
control.after_analysis.callback =
94-
Box::new(move |state| after_analysis(state, miri_config.clone()));
95-
control.after_analysis.stop = Compilation::Stop;
96-
control
97-
}
98-
}
27+
impl rustc_driver::Callbacks for MiriCompilerCalls {
28+
fn after_parsing(&mut self, compiler: &interface::Compiler) -> bool {
29+
let attr = (
30+
String::from("miri"),
31+
syntax::feature_gate::AttributeType::Whitelisted,
32+
);
33+
compiler.session().plugin_attributes.borrow_mut().push(attr);
9934

100-
fn after_hir_lowering(state: &mut CompileState) {
101-
let attr = (
102-
String::from("miri"),
103-
syntax::feature_gate::AttributeType::Whitelisted,
104-
);
105-
state.session.plugin_attributes.borrow_mut().push(attr);
106-
}
107-
108-
fn after_analysis<'a, 'tcx>(
109-
state: &mut CompileState<'a, 'tcx>,
110-
miri_config: MiriConfig,
111-
) {
112-
init_late_loggers();
113-
state.session.abort_if_errors();
35+
// Continue execution
36+
true
37+
}
11438

115-
let tcx = state.tcx.unwrap();
39+
fn after_analysis(&mut self, compiler: &interface::Compiler) -> bool {
40+
init_late_loggers();
41+
compiler.session().abort_if_errors();
11642

43+
compiler.global_ctxt().unwrap().peek_mut().enter(|tcx| {
44+
let (entry_def_id, _) = tcx.entry_fn(LOCAL_CRATE).expect("no main function found!");
11745

118-
let (entry_def_id, _) = tcx.entry_fn(LOCAL_CRATE).expect("no main function found!");
46+
miri::eval_main(tcx, entry_def_id, self.miri_config.clone());
47+
});
11948

120-
miri::eval_main(tcx, entry_def_id, miri_config);
49+
compiler.session().abort_if_errors();
12150

122-
state.session.abort_if_errors();
51+
// Don't continue execution
52+
false
53+
}
12354
}
12455

12556
fn init_early_loggers() {
@@ -228,12 +159,9 @@ fn main() {
228159

229160
debug!("rustc arguments: {:?}", rustc_args);
230161
debug!("miri arguments: {:?}", miri_args);
231-
let miri_config = MiriConfig { validate, args: miri_args };
232-
let result = rustc_driver::run(move || {
233-
rustc_driver::run_compiler(&rustc_args, Box::new(MiriCompilerCalls {
234-
default: Box::new(RustcDefaultCalls),
235-
miri_config,
236-
}), None, None)
237-
});
238-
std::process::exit(result as i32);
162+
let miri_config = miri::MiriConfig { validate, args: miri_args };
163+
let result = rustc_driver::report_ices_to_stderr_if_any(move || {
164+
rustc_driver::run_compiler(&rustc_args, &mut MiriCompilerCalls { miri_config }, None, None)
165+
}).and_then(|result| result);
166+
std::process::exit(result.is_err() as i32);
239167
}

0 commit comments

Comments
 (0)