Skip to content

Commit

Permalink
Rollup merge of #72090 - RalfJung:rustc_driver-exit-code, r=oli-obk
Browse files Browse the repository at this point in the history
rustc_driver: factor out computing the exit code

In a recent Miri PR I [added a convenience wrapper](https://github.com/rust-lang/miri/pull/1405/files#diff-c3d602c5c8035a16699ce9c015bfeceaR125) around `catch_fatal_errors` and `run_compiler` that @oli-obk suggested I could upstream. However, after seeing what could be shared between `rustc_driver::main`, clippy and Miri, really the only thing I found is computing the exit code -- so that's what this PR does.

What prevents using the Miri convenience function in `rustc_driver::main` and clippy is that they do extra work inside `catch_fatal_errors`, and while I could abstract that away, clippy actually *computes the callbacks* inside there, and I fond no good way to abstract that and thus gave up. Maybe the clippy thing could be moved out, I am not sure if it ever can actually raise a `FatalErrorMarker` -- someone more knowledgeable in clippy would have to do that.
  • Loading branch information
RalfJung authored May 15, 2020
2 parents ae17c19 + 51e466d commit 7d01ffc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
23 changes: 14 additions & 9 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,16 @@ pub fn catch_fatal_errors<F: FnOnce() -> R, R>(f: F) -> Result<R, ErrorReported>
})
}

/// Variant of `catch_fatal_errors` for the `interface::Result` return type
/// that also computes the exit code.
pub fn catch_with_exit_code(f: impl FnOnce() -> interface::Result<()>) -> i32 {
let result = catch_fatal_errors(f).and_then(|result| result);
match result {
Ok(()) => EXIT_SUCCESS,
Err(_) => EXIT_FAILURE,
}
}

lazy_static! {
static ref DEFAULT_HOOK: Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static> = {
let hook = panic::take_hook();
Expand Down Expand Up @@ -1228,12 +1238,12 @@ pub fn init_rustc_env_logger() {
env_logger::init_from_env("RUSTC_LOG");
}

pub fn main() {
pub fn main() -> ! {
let start = Instant::now();
init_rustc_env_logger();
let mut callbacks = TimePassesCallbacks::default();
install_ice_hook();
let result = catch_fatal_errors(|| {
let exit_code = catch_with_exit_code(|| {
let args = env::args_os()
.enumerate()
.map(|(i, arg)| {
Expand All @@ -1246,13 +1256,8 @@ pub fn main() {
})
.collect::<Vec<_>>();
run_compiler(&args, &mut callbacks, None, None)
})
.and_then(|result| result);
let exit_code = match result {
Ok(_) => EXIT_SUCCESS,
Err(_) => EXIT_FAILURE,
};
});
// The extra `\t` is necessary to align this label with the others.
print_time_passes_entry(callbacks.time_passes, "\ttotal", start.elapsed());
process::exit(exit_code);
process::exit(exit_code)
}
4 changes: 1 addition & 3 deletions src/tools/clippy/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ pub fn main() {
rustc_driver::init_rustc_env_logger();
lazy_static::initialize(&ICE_HOOK);
exit(
rustc_driver::catch_fatal_errors(move || {
rustc_driver::catch_with_exit_code(move || {
let mut orig_args: Vec<String> = env::args().collect();

if orig_args.iter().any(|a| a == "--version" || a == "-V") {
Expand Down Expand Up @@ -411,7 +411,5 @@ pub fn main() {
if clippy_enabled { &mut clippy } else { &mut default };
rustc_driver::run_compiler(&args, callbacks, None, None)
})
.and_then(|result| result)
.is_err() as i32,
)
}

0 comments on commit 7d01ffc

Please sign in to comment.