Skip to content

Commit 23d880b

Browse files
committed
rustc_driver: factor out computing the exit code
1 parent 8d16eeb commit 23d880b

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/librustc_driver/lib.rs

+12-7
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();
@@ -1233,7 +1243,7 @@ pub fn main() {
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,12 +1256,7 @@ 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());
12571262
process::exit(exit_code);

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)