1
Fork 0

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:
Nicholas Nethercote 2024-02-19 10:00:19 +11:00
parent 46f4983356
commit 72b172bdf6
9 changed files with 76 additions and 41 deletions

View file

@ -258,7 +258,8 @@ impl Session {
}
}
fn check_miri_unleashed_features(&self) {
fn check_miri_unleashed_features(&self) -> Option<ErrorGuaranteed> {
let mut guar = None;
let unleashed_features = self.miri_unleashed_features.lock();
if !unleashed_features.is_empty() {
let mut must_err = false;
@ -279,18 +280,22 @@ impl Session {
// If we should err, make sure we did.
if must_err && self.dcx().has_errors().is_none() {
// We have skipped a feature gate, and not run into other errors... reject.
self.dcx().emit_err(errors::NotCircumventFeature);
guar = Some(self.dcx().emit_err(errors::NotCircumventFeature));
}
}
guar
}
/// Invoked all the way at the end to finish off diagnostics printing.
pub fn finish_diagnostics(&self, registry: &Registry) {
self.check_miri_unleashed_features();
pub fn finish_diagnostics(&self, registry: &Registry) -> Option<ErrorGuaranteed> {
let mut guar = None;
guar = guar.or(self.check_miri_unleashed_features());
guar = guar.or(self.dcx().emit_stashed_diagnostics());
self.dcx().print_error_count(registry);
if self.opts.json_future_incompat {
self.dcx().emit_future_breakage_report();
}
guar
}
/// Returns true if the crate is a testing one.
@ -314,7 +319,6 @@ impl Session {
pub fn compile_status(&self) -> Result<(), ErrorGuaranteed> {
if let Some(reported) = self.dcx().has_errors() {
self.dcx().emit_stashed_diagnostics();
Err(reported)
} else {
Ok(())