about summary refs log tree commit diff
path: root/compiler/rustc_interface/src/interface.rs
diff options
context:
space:
mode:
authorbjorn3 <17426603+bjorn3@users.noreply.github.com>2024-06-22 09:32:50 +0000
committerbjorn3 <17426603+bjorn3@users.noreply.github.com>2024-06-22 17:06:47 +0000
commite3ffbbd2263059fd7d11881540c214aeb000a7d3 (patch)
tree2c8faef554434ed79741ec74d374fa5dbf2c974c /compiler/rustc_interface/src/interface.rs
parent7332e79d5fca8ccc48fcb1668f95f6f1bd7ce7c9 (diff)
downloadrust-e3ffbbd2263059fd7d11881540c214aeb000a7d3.tar.gz
rust-e3ffbbd2263059fd7d11881540c214aeb000a7d3.zip
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.
Diffstat (limited to 'compiler/rustc_interface/src/interface.rs')
-rw-r--r--compiler/rustc_interface/src/interface.rs21
1 files changed, 9 insertions, 12 deletions
diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs
index 41c8b941717..dba20e4a335 100644
--- a/compiler/rustc_interface/src/interface.rs
+++ b/compiler/rustc_interface/src/interface.rs
@@ -495,9 +495,8 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
             let res = {
                 // If `f` panics, `finish_diagnostics` will run during
                 // unwinding because of the `defer`.
-                let mut guar = None;
                 let sess_abort_guard = defer(|| {
-                    guar = compiler.sess.finish_diagnostics(&config.registry);
+                    compiler.sess.finish_diagnostics(&config.registry);
                 });
 
                 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.
                 drop(sess_abort_guard);
 
-                // If `finish_diagnostics` emits errors (e.g. stashed
-                // errors) we can't return an error directly, because the
-                // return type of this function is `R`, not `Result<R, E>`.
-                // But we need to communicate the errors' existence to the
-                // caller, otherwise the caller might mistakenly think that
-                // no errors occurred and return a zero exit code. So we
-                // abort (panic) instead, similar to if `f` had panicked.
-                if guar.is_some() {
-                    compiler.sess.dcx().abort_if_errors();
-                }
+                // If error diagnostics have been emitted, we can't return an
+                // error directly, because the return type of this function
+                // is `R`, not `Result<R, E>`. But we need to communicate the
+                // errors' existence to the caller, otherwise the caller might
+                // mistakenly think that no errors occurred and return a zero
+                // exit code. So we abort (panic) instead, similar to if `f`
+                // had panicked.
+                compiler.sess.dcx().abort_if_errors();
 
                 res
             };