Skip to content

Commit 016d92d

Browse files
committed
Auto merge of #3733 - Zoxc:rustc-interface, r=oli-obk
Use the new rustc interface Shows the changes required to compile with rust-lang/rust#56732
2 parents 9308df7 + 1388f24 commit 016d92d

File tree

1 file changed

+63
-54
lines changed

1 file changed

+63
-54
lines changed

src/driver.rs

+63-54
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
#[allow(unused_extern_crates)]
99
extern crate rustc_driver;
1010
#[allow(unused_extern_crates)]
11+
extern crate rustc_interface;
12+
#[allow(unused_extern_crates)]
1113
extern crate rustc_plugin;
12-
use self::rustc_driver::{driver::CompileController, Compilation};
1314

14-
use std::convert::TryInto;
15+
use rustc_interface::interface;
1516
use std::path::Path;
1617
use std::process::{exit, Command};
1718

@@ -60,10 +61,62 @@ fn test_arg_value() {
6061
}
6162

6263
#[allow(clippy::too_many_lines)]
64+
65+
struct ClippyCallbacks;
66+
67+
impl rustc_driver::Callbacks for ClippyCallbacks {
68+
fn after_parsing(&mut self, compiler: &interface::Compiler) -> bool {
69+
let sess = compiler.session();
70+
let mut registry = rustc_plugin::registry::Registry::new(
71+
sess,
72+
compiler
73+
.parse()
74+
.expect(
75+
"at this compilation stage \
76+
the crate must be parsed",
77+
)
78+
.peek()
79+
.span,
80+
);
81+
registry.args_hidden = Some(Vec::new());
82+
83+
let conf = clippy_lints::read_conf(&registry);
84+
clippy_lints::register_plugins(&mut registry, &conf);
85+
86+
let rustc_plugin::registry::Registry {
87+
early_lint_passes,
88+
late_lint_passes,
89+
lint_groups,
90+
llvm_passes,
91+
attributes,
92+
..
93+
} = registry;
94+
let mut ls = sess.lint_store.borrow_mut();
95+
for pass in early_lint_passes {
96+
ls.register_early_pass(Some(sess), true, false, pass);
97+
}
98+
for pass in late_lint_passes {
99+
ls.register_late_pass(Some(sess), true, pass);
100+
}
101+
102+
for (name, (to, deprecated_name)) in lint_groups {
103+
ls.register_group(Some(sess), true, name, deprecated_name, to);
104+
}
105+
clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
106+
clippy_lints::register_renamed(&mut ls);
107+
108+
sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
109+
sess.plugin_attributes.borrow_mut().extend(attributes);
110+
111+
// Continue execution
112+
true
113+
}
114+
}
115+
63116
pub fn main() {
64117
rustc_driver::init_rustc_env_logger();
65118
exit(
66-
rustc_driver::run(move || {
119+
rustc_driver::report_ices_to_stderr_if_any(move || {
67120
use std::env;
68121

69122
if std::env::args().any(|a| a == "--version" || a == "-V") {
@@ -144,58 +197,14 @@ pub fn main() {
144197
}
145198
}
146199

147-
let mut controller = CompileController::basic();
148-
if clippy_enabled {
149-
controller.after_parse.callback = Box::new(move |state| {
150-
let mut registry = rustc_plugin::registry::Registry::new(
151-
state.session,
152-
state
153-
.krate
154-
.as_ref()
155-
.expect(
156-
"at this compilation stage \
157-
the crate must be parsed",
158-
)
159-
.span,
160-
);
161-
registry.args_hidden = Some(Vec::new());
162-
163-
let conf = clippy_lints::read_conf(&registry);
164-
clippy_lints::register_plugins(&mut registry, &conf);
165-
166-
let rustc_plugin::registry::Registry {
167-
early_lint_passes,
168-
late_lint_passes,
169-
lint_groups,
170-
llvm_passes,
171-
attributes,
172-
..
173-
} = registry;
174-
let sess = &state.session;
175-
let mut ls = sess.lint_store.borrow_mut();
176-
for pass in early_lint_passes {
177-
ls.register_early_pass(Some(sess), true, false, pass);
178-
}
179-
for pass in late_lint_passes {
180-
ls.register_late_pass(Some(sess), true, pass);
181-
}
182-
183-
for (name, (to, deprecated_name)) in lint_groups {
184-
ls.register_group(Some(sess), true, name, deprecated_name, to);
185-
}
186-
clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
187-
clippy_lints::register_renamed(&mut ls);
188-
189-
sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
190-
sess.plugin_attributes.borrow_mut().extend(attributes);
191-
});
192-
}
193-
controller.compilation_done.stop = Compilation::Stop;
194-
200+
let mut clippy = ClippyCallbacks;
201+
let mut default = rustc_driver::DefaultCallbacks;
202+
let callbacks: &mut (dyn rustc_driver::Callbacks + Send) =
203+
if clippy_enabled { &mut clippy } else { &mut default };
195204
let args = args;
196-
rustc_driver::run_compiler(&args, Box::new(controller), None, None)
205+
rustc_driver::run_compiler(&args, callbacks, None, None)
197206
})
198-
.try_into()
199-
.expect("exit code too large"),
207+
.and_then(|result| result)
208+
.is_err() as i32,
200209
)
201210
}

0 commit comments

Comments
 (0)