]> git.lizzy.rs Git - rust.git/blobdiff - benches/helpers/miri_helper.rs
Auto merge of #847 - RalfJung:rustup, r=RalfJung
[rust.git] / benches / helpers / miri_helper.rs
index 2bf78b0a71cc16a98b67dd4e39da4f39f5d2fde0..203a8b1133a5c5524ba1234f761c3a547d501cb8 100644 (file)
@@ -2,16 +2,39 @@
 extern crate miri;
 extern crate rustc;
 extern crate rustc_driver;
+extern crate rustc_interface;
 extern crate test;
 
 use self::miri::eval_main;
-use self::rustc::session::Session;
-use self::rustc_driver::{driver, CompilerCalls, Compilation};
-use std::cell::RefCell;
-use std::rc::Rc;
-use test::Bencher;
+use rustc::hir::def_id::LOCAL_CRATE;
+use rustc_interface::interface;
+use rustc_driver::Compilation;
+use crate::test::Bencher;
 
-pub struct MiriCompilerCalls<'a>(Rc<RefCell<&'a mut Bencher>>);
+struct MiriCompilerCalls<'a> {
+    bencher: &'a mut Bencher,
+}
+
+impl rustc_driver::Callbacks for MiriCompilerCalls<'_> {
+    fn after_analysis(&mut self, compiler: &interface::Compiler) -> Compilation {
+        compiler.session().abort_if_errors();
+
+        compiler.global_ctxt().unwrap().peek_mut().enter(|tcx| {
+            let (entry_def_id, _) = tcx.entry_fn(LOCAL_CRATE).expect(
+                "no main or start function found",
+            );
+
+            self.bencher.iter(|| {
+                let config = miri::MiriConfig { validate: true, args: vec![], seed: None };
+                eval_main(tcx, entry_def_id, config);
+            });
+        });
+
+        compiler.session().abort_if_errors();
+
+        Compilation::Stop
+    }
+}
 
 fn find_sysroot() -> String {
     // Taken from https://github.com/Manishearth/rust-clippy/pull/911.
@@ -36,37 +59,5 @@ pub fn run(filename: &str, bencher: &mut Bencher) {
         "--sysroot".to_string(),
         find_sysroot(),
     ];
-    let compiler_calls = &mut MiriCompilerCalls(Rc::new(RefCell::new(bencher)));
-    rustc_driver::run_compiler(args, compiler_calls, None, None);
-}
-
-impl<'a> CompilerCalls<'a> for MiriCompilerCalls<'a> {
-    fn build_controller(
-        &mut self,
-        _: &Session,
-        _: &getopts::Matches,
-    ) -> driver::CompileController<'a> {
-        let mut control: driver::CompileController<'a> = driver::CompileController::basic();
-
-        let bencher = self.0.clone();
-
-        control.after_analysis.stop = Compilation::Stop;
-        control.after_analysis.callback = Box::new(move |state| {
-            state.session.abort_if_errors();
-
-            let tcx = state.tcx.unwrap();
-            let (entry_node_id, _, _) = state.session.entry_fn.borrow().expect(
-                "no main or start function found",
-            );
-            let entry_def_id = tcx.hir.local_def_id(entry_node_id);
-
-            bencher.borrow_mut().iter(|| {
-                eval_main(tcx, entry_def_id, None);
-            });
-
-            state.session.abort_if_errors();
-        });
-
-        control
-    }
+    rustc_driver::run_compiler(args, &mut MiriCompilerCalls { bencher }, None, None).unwrap()
 }