1
Fork 0

Ensure run_compiler always aborts on errors

Before if the closure passed to run_compiler emitted an error without
calling abort_if_errors and no diagnostics have been stashed,
run_compiler would return normally as if no error had occured.
This commit is contained in:
bjorn3 2024-06-22 09:32:50 +00:00
parent 7332e79d5f
commit e3ffbbd226

View file

@ -495,9 +495,8 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
let res = { let res = {
// If `f` panics, `finish_diagnostics` will run during // If `f` panics, `finish_diagnostics` will run during
// unwinding because of the `defer`. // unwinding because of the `defer`.
let mut guar = None;
let sess_abort_guard = defer(|| { let sess_abort_guard = defer(|| {
guar = compiler.sess.finish_diagnostics(&config.registry); compiler.sess.finish_diagnostics(&config.registry);
}); });
let res = f(&compiler); let res = f(&compiler);
@ -506,16 +505,14 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
// normally when `sess_abort_guard` is dropped. // normally when `sess_abort_guard` is dropped.
drop(sess_abort_guard); drop(sess_abort_guard);
// If `finish_diagnostics` emits errors (e.g. stashed // If error diagnostics have been emitted, we can't return an
// errors) we can't return an error directly, because the // error directly, because the return type of this function
// return type of this function is `R`, not `Result<R, E>`. // is `R`, not `Result<R, E>`. But we need to communicate the
// But we need to communicate the errors' existence to the // errors' existence to the caller, otherwise the caller might
// caller, otherwise the caller might mistakenly think that // mistakenly think that no errors occurred and return a zero
// no errors occurred and return a zero exit code. So we // exit code. So we abort (panic) instead, similar to if `f`
// abort (panic) instead, similar to if `f` had panicked. // had panicked.
if guar.is_some() { compiler.sess.dcx().abort_if_errors();
compiler.sess.dcx().abort_if_errors();
}
res res
}; };