Skip to content

Commit 72b4ee0

Browse files
authored
Merge pull request #659 from rust-lang/update
Use the new rustc interface
2 parents e66d6ec + 8568c20 commit 72b4ee0

File tree

6 files changed

+104
-245
lines changed

6 files changed

+104
-245
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 = miri::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).unwrap()
6363
}

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nightly-2019-03-02
1+
nightly-2019-03-11

src/bin/cargo-miri.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ Common options:
2525
Other [options] are the same as `cargo rustc`. Everything after the first "--" is
2626
passed verbatim to Miri, which will pass everything after the second "--" verbatim
2727
to the interpreted program.
28-
29-
The config flag `miri` is automatically defined for convenience. You can use
30-
it to configure the resource limits
31-
32-
#![cfg_attr(miri, memory_size = 42)]
33-
34-
available resource limits are `memory_size`, `step_limit`, `stack_limit`
3528
"#;
3629

3730
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

src/bin/miri-rustc-tests.rs

Lines changed: 43 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -6,115 +6,72 @@ 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

11-
use std::path::{PathBuf, Path};
12+
use std::path::Path;
1213
use std::io::Write;
1314
use std::sync::{Mutex, Arc};
1415
use std::io;
1516

1617

17-
use rustc::session::Session;
18-
use rustc_metadata::cstore::CStore;
19-
use rustc_driver::{Compilation, CompilerCalls, RustcDefaultCalls};
20-
use rustc_driver::driver::{CompileState, CompileController};
21-
use rustc::session::config::{self, Input, ErrorOutputType};
18+
use rustc_interface::interface;
2219
use rustc::hir::{self, itemlikevisit};
23-
use rustc_codegen_utils::codegen_backend::CodegenBackend;
2420
use rustc::ty::TyCtxt;
25-
use syntax::ast;
2621
use rustc::hir::def_id::LOCAL_CRATE;
2722

2823
use miri::MiriConfig;
2924

3025
struct MiriCompilerCalls {
31-
default: Box<RustcDefaultCalls>,
3226
/// whether we are building for the host
3327
host_target: bool,
3428
}
3529

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-
}
30+
impl rustc_driver::Callbacks for MiriCompilerCalls {
31+
fn after_parsing(&mut self, compiler: &interface::Compiler) -> bool {
32+
let attr = (
33+
String::from("miri"),
34+
syntax::feature_gate::AttributeType::Whitelisted,
35+
);
36+
compiler.session().plugin_attributes.borrow_mut().push(attr);
8737

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

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();
42+
fn after_analysis(&mut self, compiler: &interface::Compiler) -> bool {
43+
compiler.session().abort_if_errors();
44+
compiler.global_ctxt().unwrap().peek_mut().enter(|tcx| {
45+
if std::env::args().any(|arg| arg == "--test") {
46+
struct Visitor<'a, 'tcx: 'a>(TyCtxt<'a, 'tcx, 'tcx>);
47+
impl<'a, 'tcx: 'a, 'hir> itemlikevisit::ItemLikeVisitor<'hir> for Visitor<'a, 'tcx> {
48+
fn visit_item(&mut self, i: &'hir hir::Item) {
49+
if let hir::ItemKind::Fn(.., body_id) = i.node {
50+
if i.attrs.iter().any(|attr| attr.name() == "test") {
51+
let config = MiriConfig { validate: true, args: vec![] };
52+
let did = self.0.hir().body_owner_def_id(body_id);
53+
println!("running test: {}", self.0.def_path_debug_str(did));
54+
miri::eval_main(self.0, did, config);
55+
self.0.sess.abort_if_errors();
56+
}
57+
}
10458
}
59+
fn visit_trait_item(&mut self, _trait_item: &'hir hir::TraitItem) {}
60+
fn visit_impl_item(&mut self, _impl_item: &'hir hir::ImplItem) {}
10561
}
62+
tcx.hir().krate().visit_all_item_likes(&mut Visitor(tcx));
63+
} else if let Some((entry_def_id, _)) = tcx.entry_fn(LOCAL_CRATE) {
64+
let config = MiriConfig { validate: true, args: vec![] };
65+
miri::eval_main(tcx, entry_def_id, config);
66+
67+
compiler.session().abort_if_errors();
68+
} else {
69+
println!("no main function found, assuming auxiliary build");
10670
}
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);
71+
});
11472

115-
state.session.abort_if_errors();
116-
} else {
117-
println!("no main function found, assuming auxiliary build");
73+
// Continue execution on host target
74+
self.host_target
11875
}
11976
}
12077

@@ -185,10 +142,7 @@ fn main() {
185142
let buf = BufWriter::default();
186143
let output = buf.clone();
187144
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)));
145+
let _ = rustc_driver::run_compiler(&args, &mut MiriCompilerCalls { host_target }, None, Some(Box::new(buf)));
192146
});
193147

194148
match result {

0 commit comments

Comments
 (0)