]> git.lizzy.rs Git - rust.git/blobdiff - src/driver.rs
deps: bump toml from 0.4 to 0.5
[rust.git] / src / driver.rs
index fbff693f8870964b226cf1f80852bf3c69c40acf..834d11861c0d58540e9420c82f64a41f379f1399 100644 (file)
@@ -1,18 +1,15 @@
-// error-pattern:yummy
-#![feature(box_syntax)]
 #![feature(rustc_private)]
-#![feature(try_from)]
-#![allow(clippy::missing_docs_in_private_items)]
 
 // FIXME: switch to something more ergonomic here, once available.
-// (currently there is no way to opt into sysroot crates w/o `extern crate`)
+// (Currently there is no way to opt into sysroot crates without `extern crate`.)
 #[allow(unused_extern_crates)]
 extern crate rustc_driver;
 #[allow(unused_extern_crates)]
+extern crate rustc_interface;
+#[allow(unused_extern_crates)]
 extern crate rustc_plugin;
-use self::rustc_driver::{driver::CompileController, Compilation};
 
-use std::convert::TryInto;
+use rustc_interface::interface;
 use std::path::Path;
 use std::process::{exit, Command};
 
@@ -47,7 +44,7 @@ fn arg_value<'a>(
 fn test_arg_value() {
     let args: Vec<_> = ["--bar=bar", "--foobar", "123", "--foo"]
         .iter()
-        .map(|s| s.to_string())
+        .map(std::string::ToString::to_string)
         .collect();
 
     assert_eq!(arg_value(None, "--foobar", |_| true), None);
@@ -61,10 +58,62 @@ fn test_arg_value() {
 }
 
 #[allow(clippy::too_many_lines)]
+
+struct ClippyCallbacks;
+
+impl rustc_driver::Callbacks for ClippyCallbacks {
+    fn after_parsing(&mut self, compiler: &interface::Compiler) -> bool {
+        let sess = compiler.session();
+        let mut registry = rustc_plugin::registry::Registry::new(
+            sess,
+            compiler
+                .parse()
+                .expect(
+                    "at this compilation stage \
+                     the crate must be parsed",
+                )
+                .peek()
+                .span,
+        );
+        registry.args_hidden = Some(Vec::new());
+
+        let conf = clippy_lints::read_conf(&registry);
+        clippy_lints::register_plugins(&mut registry, &conf);
+
+        let rustc_plugin::registry::Registry {
+            early_lint_passes,
+            late_lint_passes,
+            lint_groups,
+            llvm_passes,
+            attributes,
+            ..
+        } = registry;
+        let mut ls = sess.lint_store.borrow_mut();
+        for pass in early_lint_passes {
+            ls.register_early_pass(Some(sess), true, false, pass);
+        }
+        for pass in late_lint_passes {
+            ls.register_late_pass(Some(sess), true, false, false, pass);
+        }
+
+        for (name, (to, deprecated_name)) in lint_groups {
+            ls.register_group(Some(sess), true, name, deprecated_name, to);
+        }
+        clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
+        clippy_lints::register_renamed(&mut ls);
+
+        sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
+        sess.plugin_attributes.borrow_mut().extend(attributes);
+
+        // Continue execution
+        true
+    }
+}
+
 pub fn main() {
     rustc_driver::init_rustc_env_logger();
     exit(
-        rustc_driver::run(move || {
+        rustc_driver::report_ices_to_stderr_if_any(move || {
             use std::env;
 
             if std::env::args().any(|a| a == "--version" || a == "-V") {
@@ -84,7 +133,7 @@ pub fn main() {
             let sys_root_arg = arg_value(&orig_args, "--sysroot", |_| true);
             let have_sys_root_arg = sys_root_arg.is_some();
             let sys_root = sys_root_arg
-                .map(|s| s.to_string())
+                .map(std::string::ToString::to_string)
                 .or_else(|| std::env::var("SYSROOT").ok())
                 .or_else(|| {
                     let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
@@ -145,58 +194,14 @@ pub fn main() {
                 }
             }
 
-            let mut controller = CompileController::basic();
-            if clippy_enabled {
-                controller.after_parse.callback = Box::new(move |state| {
-                    let mut registry = rustc_plugin::registry::Registry::new(
-                        state.session,
-                        state
-                            .krate
-                            .as_ref()
-                            .expect(
-                                "at this compilation stage \
-                                 the crate must be parsed",
-                            )
-                            .span,
-                    );
-                    registry.args_hidden = Some(Vec::new());
-
-                    let conf = clippy_lints::read_conf(&registry);
-                    clippy_lints::register_plugins(&mut registry, &conf);
-
-                    let rustc_plugin::registry::Registry {
-                        early_lint_passes,
-                        late_lint_passes,
-                        lint_groups,
-                        llvm_passes,
-                        attributes,
-                        ..
-                    } = registry;
-                    let sess = &state.session;
-                    let mut ls = sess.lint_store.borrow_mut();
-                    for pass in early_lint_passes {
-                        ls.register_early_pass(Some(sess), true, false, pass);
-                    }
-                    for pass in late_lint_passes {
-                        ls.register_late_pass(Some(sess), true, pass);
-                    }
-
-                    for (name, (to, deprecated_name)) in lint_groups {
-                        ls.register_group(Some(sess), true, name, deprecated_name, to);
-                    }
-                    clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
-                    clippy_lints::register_renamed(&mut ls);
-
-                    sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
-                    sess.plugin_attributes.borrow_mut().extend(attributes);
-                });
-            }
-            controller.compilation_done.stop = Compilation::Stop;
-
+            let mut clippy = ClippyCallbacks;
+            let mut default = rustc_driver::DefaultCallbacks;
+            let callbacks: &mut (dyn rustc_driver::Callbacks + Send) =
+                if clippy_enabled { &mut clippy } else { &mut default };
             let args = args;
-            rustc_driver::run_compiler(&args, Box::new(controller), None, None)
+            rustc_driver::run_compiler(&args, callbacks, None, None)
         })
-        .try_into()
-        .expect("exit code too large"),
+        .and_then(|result| result)
+        .is_err() as i32,
     )
 }