Overhaul the handling of errors at the top-level.
Currently `emit_stashed_diagnostic` is called from four(!) different places: `print_error_count`, `DiagCtxtInner::drop`, `abort_if_errors`, and `compile_status`. And `flush_delayed` is called from two different places: `DiagCtxtInner::drop` and `Queries`. This is pretty gross! Each one should really be called from a single place, but there's a bunch of entanglements. This commit cleans up this mess. Specifically, it: - Removes all the existing calls to `emit_stashed_diagnostic`, and adds a single new call in `finish_diagnostics`. - Removes the early `flush_delayed` call in `codegen_and_build_linker`, replacing it with a simple early return if delayed bugs are present. - Changes `DiagCtxtInner::drop` and `DiagCtxtInner::flush_delayed` so they both assert that the stashed diagnostics are empty (i.e. processed beforehand). - Changes `interface::run_compiler` so that any errors emitted during `finish_diagnostics` (i.e. late-emitted stashed diagnostics) are counted and cannot be overlooked. This requires adding `ErrorGuaranteed` return values to several functions. - Removes the `stashed_err_count` call in `analysis`. This is possible now that we don't have to worry about calling `flush_delayed` early from `codegen_and_build_linker` when stashed diagnostics are pending. - Changes the `span_bug` case in `handle_tuple_field_pattern_match` to a `delayed_span_bug`, because it now can be reached due to the removal of the `stashed_err_count` call in `analysis`. - Slightly changes the expected output of three tests. If no errors are emitted but there are delayed bugs, the error count is no longer printed. This is because delayed bugs are now always printed after the error count is printed (or not printed, if the error count is zero). There is a lot going on in this commit. It's hard to break into smaller pieces because the existing code is very tangled. It took me a long time and a lot of effort to understand how the different pieces interact, and I think the new code is a lot simpler and easier to understand.
This commit is contained in:
parent
46f4983356
commit
72b172bdf6
9 changed files with 76 additions and 41 deletions
|
@ -423,18 +423,43 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
|
|||
Compiler { sess, codegen_backend, override_queries: config.override_queries };
|
||||
|
||||
rustc_span::set_source_map(compiler.sess.parse_sess.clone_source_map(), move || {
|
||||
let r = {
|
||||
let _sess_abort_error = defer(|| {
|
||||
compiler.sess.finish_diagnostics(&config.registry);
|
||||
// There are two paths out of `f`.
|
||||
// - Normal exit.
|
||||
// - Panic, e.g. triggered by `abort_if_errors`.
|
||||
//
|
||||
// We must run `finish_diagnostics` in both cases.
|
||||
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);
|
||||
});
|
||||
|
||||
f(&compiler)
|
||||
let res = f(&compiler);
|
||||
|
||||
// If `f` doesn't panic, `finish_diagnostics` will run
|
||||
// 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();
|
||||
}
|
||||
|
||||
res
|
||||
};
|
||||
|
||||
let prof = compiler.sess.prof.clone();
|
||||
|
||||
prof.generic_activity("drop_compiler").run(move || drop(compiler));
|
||||
r
|
||||
|
||||
res
|
||||
})
|
||||
},
|
||||
)
|
||||
|
|
|
@ -772,12 +772,11 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
|
|||
// lot of annoying errors in the ui tests (basically,
|
||||
// lint warnings and so on -- kindck used to do this abort, but
|
||||
// kindck is gone now). -nmatsakis
|
||||
if let Some(reported) = sess.dcx().has_errors_excluding_lint_errors() {
|
||||
return Err(reported);
|
||||
} else if sess.dcx().stashed_err_count() > 0 {
|
||||
// Without this case we sometimes get delayed bug ICEs and I don't
|
||||
// understand why. -nnethercote
|
||||
return Err(sess.dcx().delayed_bug("some stashed error is waiting for use"));
|
||||
//
|
||||
// But we exclude lint errors from this, because lint errors are typically
|
||||
// less serious and we're more likely to want to continue (#87337).
|
||||
if let Some(guar) = sess.dcx().has_errors_excluding_lint_errors() {
|
||||
return Err(guar);
|
||||
}
|
||||
|
||||
sess.time("misc_checking_3", || {
|
||||
|
|
|
@ -222,12 +222,12 @@ impl<'tcx> Queries<'tcx> {
|
|||
|
||||
pub fn codegen_and_build_linker(&'tcx self) -> Result<Linker> {
|
||||
self.global_ctxt()?.enter(|tcx| {
|
||||
// Don't do code generation if there were any errors
|
||||
self.compiler.sess.compile_status()?;
|
||||
|
||||
// If we have any delayed bugs, for example because we created TyKind::Error earlier,
|
||||
// it's likely that codegen will only cause more ICEs, obscuring the original problem
|
||||
self.compiler.sess.dcx().flush_delayed();
|
||||
// Don't do code generation if there were any errors. Likewise if
|
||||
// there were any delayed bugs, because codegen will likely cause
|
||||
// more ICEs, obscuring the original problem.
|
||||
if let Some(guar) = self.compiler.sess.dcx().has_errors_or_delayed_bugs() {
|
||||
return Err(guar);
|
||||
}
|
||||
|
||||
// Hook for UI tests.
|
||||
Self::check_for_rustc_errors_attr(tcx);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue