Skip to content

Commit 040e242

Browse files
authored
Rollup merge of #72090 - RalfJung:rustc_driver-exit-code, r=oli-obk
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.
2 parents badcf26 + 51e466d commit 040e242

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/librustc_driver/lib.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,16 @@ pub fn catch_fatal_errors<F: FnOnce() -> R, R>(f: F) -> Result<R, ErrorReported>
11381138
})
11391139
}
11401140

1141+
/// Variant of `catch_fatal_errors` for the `interface::Result` return type
1142+
/// that also computes the exit code.
1143+
pub fn catch_with_exit_code(f: impl FnOnce() -> interface::Result<()>) -> i32 {
1144+
let result = catch_fatal_errors(f).and_then(|result| result);
1145+
match result {
1146+
Ok(()) => EXIT_SUCCESS,
1147+
Err(_) => EXIT_FAILURE,
1148+
}
1149+
}
1150+
11411151
lazy_static! {
11421152
static ref DEFAULT_HOOK: Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static> = {
11431153
let hook = panic::take_hook();
@@ -1228,12 +1238,12 @@ pub fn init_rustc_env_logger() {
12281238
env_logger::init_from_env("RUSTC_LOG");
12291239
}
12301240

1231-
pub fn main() {
1241+
pub fn main() -> ! {
12321242
let start = Instant::now();
12331243
init_rustc_env_logger();
12341244
let mut callbacks = TimePassesCallbacks::default();
12351245
install_ice_hook();
1236-
let result = catch_fatal_errors(|| {
1246+
let exit_code = catch_with_exit_code(|| {
12371247
let args = env::args_os()
12381248
.enumerate()
12391249
.map(|(i, arg)| {
@@ -1246,13 +1256,8 @@ pub fn main() {
12461256
})
12471257
.collect::<Vec<_>>();
12481258
run_compiler(&args, &mut callbacks, None, None)
1249-
})
1250-
.and_then(|result| result);
1251-
let exit_code = match result {
1252-
Ok(_) => EXIT_SUCCESS,
1253-
Err(_) => EXIT_FAILURE,
1254-
};
1259+
});
12551260
// The extra `\t` is necessary to align this label with the others.
12561261
print_time_passes_entry(callbacks.time_passes, "\ttotal", start.elapsed());
1257-
process::exit(exit_code);
1262+
process::exit(exit_code)
12581263
}

src/tools/clippy/src/driver.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ pub fn main() {
296296
rustc_driver::init_rustc_env_logger();
297297
lazy_static::initialize(&ICE_HOOK);
298298
exit(
299-
rustc_driver::catch_fatal_errors(move || {
299+
rustc_driver::catch_with_exit_code(move || {
300300
let mut orig_args: Vec<String> = env::args().collect();
301301

302302
if orig_args.iter().any(|a| a == "--version" || a == "-V") {
@@ -411,7 +411,5 @@ pub fn main() {
411411
if clippy_enabled { &mut clippy } else { &mut default };
412412
rustc_driver::run_compiler(&args, callbacks, None, None)
413413
})
414-
.and_then(|result| result)
415-
.is_err() as i32,
416414
)
417415
}

0 commit comments

Comments
 (0)