Skip to content

Commit

Permalink
Rollup merge of #120735 - nnethercote:rm-some-unchecked_claims, r=oli…
Browse files Browse the repository at this point in the history
…-obk

Remove some `unchecked_claim_error_was_emitted` calls

We want to drive the number of these calls down as much as possible. This PR gets rid of a bunch of them.

r? `@oli-obk`
  • Loading branch information
Nadrieril authored Feb 7, 2024
2 parents 4a110f2 + 6889fe3 commit 600d3bb
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 154 deletions.
15 changes: 8 additions & 7 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ use rustc_data_structures::profiling::{
get_resident_set_size, print_time_passes_entry, TimePassesFormat,
};
use rustc_errors::registry::Registry;
use rustc_errors::{markdown, ColorConfig, DiagCtxt, ErrCode, ErrorGuaranteed, PResult};
use rustc_errors::{
markdown, ColorConfig, DiagCtxt, ErrCode, ErrorGuaranteed, FatalError, PResult,
};
use rustc_feature::find_gated_cfg;
use rustc_interface::util::{self, collect_crate_types, get_codegen_backend};
use rustc_interface::{interface, Queries};
Expand Down Expand Up @@ -1231,11 +1233,10 @@ fn parse_crate_attrs<'a>(sess: &'a Session) -> PResult<'a, ast::AttrVec> {
/// The compiler currently unwinds with a special sentinel value to abort
/// compilation on fatal errors. This function catches that sentinel and turns
/// the panic into a `Result` instead.
pub fn catch_fatal_errors<F: FnOnce() -> R, R>(f: F) -> Result<R, ErrorGuaranteed> {
pub fn catch_fatal_errors<F: FnOnce() -> R, R>(f: F) -> Result<R, FatalError> {
catch_unwind(panic::AssertUnwindSafe(f)).map_err(|value| {
if value.is::<rustc_errors::FatalErrorMarker>() {
#[allow(deprecated)]
ErrorGuaranteed::unchecked_claim_error_was_emitted()
FatalError
} else {
panic::resume_unwind(value);
}
Expand All @@ -1245,9 +1246,9 @@ pub fn catch_fatal_errors<F: FnOnce() -> R, R>(f: F) -> Result<R, ErrorGuarantee
/// Variant of `catch_fatal_errors` for the `interface::Result` return type
/// that also computes the exit code.
pub fn catch_with_exit_code(f: impl FnOnce() -> interface::Result<()>) -> i32 {
match catch_fatal_errors(f).flatten() {
Ok(()) => EXIT_SUCCESS,
Err(_) => EXIT_FAILURE,
match catch_fatal_errors(f) {
Ok(Ok(())) => EXIT_SUCCESS,
_ => EXIT_FAILURE,
}
}

Expand Down
16 changes: 10 additions & 6 deletions compiler/rustc_errors/src/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,20 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
}

/// `ErrorGuaranteed::emit_producing_guarantee` uses this.
// FIXME(eddyb) make `ErrorGuaranteed` impossible to create outside `.emit()`.
fn emit_producing_error_guaranteed(mut self) -> ErrorGuaranteed {
let diag = self.take_diag();

// Only allow a guarantee if the `level` wasn't switched to a
// non-error. The field isn't `pub`, but the whole `Diagnostic` can be
// overwritten with a new one, thanks to `DerefMut`.
// The only error levels that produce `ErrorGuaranteed` are
// `Error` and `DelayedBug`. But `DelayedBug` should never occur here
// because delayed bugs have their level changed to `Bug` when they are
// actually printed, so they produce an ICE.
//
// (Also, even though `level` isn't `pub`, the whole `Diagnostic` could
// be overwritten with a new one thanks to `DerefMut`. So this assert
// protects against that, too.)
assert!(
diag.is_error(),
"emitted non-error ({:?}) diagnostic from `DiagnosticBuilder<ErrorGuaranteed>`",
matches!(diag.level, Level::Error | Level::DelayedBug),
"invalid diagnostic level ({:?})",
diag.level,
);

Expand Down
31 changes: 17 additions & 14 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ impl DiagCtxt {
}

/// Emit all stashed diagnostics.
pub fn emit_stashed_diagnostics(&self) -> Option<ErrorGuaranteed> {
pub fn emit_stashed_diagnostics(&self) {
self.inner.borrow_mut().emit_stashed_diagnostics()
}

Expand Down Expand Up @@ -931,8 +931,9 @@ impl DiagCtxt {
/// This excludes lint errors and delayed bugs.
pub fn has_errors(&self) -> Option<ErrorGuaranteed> {
self.inner.borrow().has_errors().then(|| {
// FIXME(nnethercote) find a way to store an `ErrorGuaranteed`.
#[allow(deprecated)]
ErrorGuaranteed::unchecked_claim_error_was_emitted()
ErrorGuaranteed::unchecked_error_guaranteed()
})
}

Expand All @@ -942,8 +943,9 @@ impl DiagCtxt {
let inner = self.inner.borrow();
let result = inner.has_errors() || inner.lint_err_count > 0;
result.then(|| {
// FIXME(nnethercote) find a way to store an `ErrorGuaranteed`.
#[allow(deprecated)]
ErrorGuaranteed::unchecked_claim_error_was_emitted()
ErrorGuaranteed::unchecked_error_guaranteed()
})
}

Expand All @@ -954,8 +956,9 @@ impl DiagCtxt {
let result =
inner.has_errors() || inner.lint_err_count > 0 || !inner.delayed_bugs.is_empty();
result.then(|| {
// FIXME(nnethercote) find a way to store an `ErrorGuaranteed`.
#[allow(deprecated)]
ErrorGuaranteed::unchecked_claim_error_was_emitted()
ErrorGuaranteed::unchecked_error_guaranteed()
})
}

Expand Down Expand Up @@ -1216,9 +1219,8 @@ impl DiagCtxt {
// `DiagCtxtInner::foo`.
impl DiagCtxtInner {
/// Emit all stashed diagnostics.
fn emit_stashed_diagnostics(&mut self) -> Option<ErrorGuaranteed> {
fn emit_stashed_diagnostics(&mut self) {
let has_errors = self.has_errors();
let mut reported = None;
for (_, diag) in std::mem::take(&mut self.stashed_diagnostics).into_iter() {
// Decrement the count tracking the stash; emitting will increment it.
if diag.is_error() {
Expand All @@ -1235,12 +1237,11 @@ impl DiagCtxtInner {
continue;
}
}
let reported_this = self.emit_diagnostic(diag);
reported = reported.or(reported_this);
self.emit_diagnostic(diag);
}
reported
}

// Return value is only `Some` if the level is `Error` or `DelayedBug`.
fn emit_diagnostic(&mut self, mut diagnostic: Diagnostic) -> Option<ErrorGuaranteed> {
assert!(diagnostic.level.can_be_top_or_sub().0);

Expand Down Expand Up @@ -1285,7 +1286,7 @@ impl DiagCtxtInner {
let backtrace = std::backtrace::Backtrace::capture();
self.delayed_bugs.push(DelayedDiagnostic::with_backtrace(diagnostic, backtrace));
#[allow(deprecated)]
return Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
return Some(ErrorGuaranteed::unchecked_error_guaranteed());
}
GoodPathDelayedBug => {
let backtrace = std::backtrace::Backtrace::capture();
Expand Down Expand Up @@ -1319,6 +1320,7 @@ impl DiagCtxtInner {
!self.emitted_diagnostics.insert(diagnostic_hash)
};

let level = diagnostic.level;
let is_error = diagnostic.is_error();
let is_lint = diagnostic.is_lint.is_some();

Expand Down Expand Up @@ -1355,18 +1357,19 @@ impl DiagCtxtInner {

self.emitter.emit_diagnostic(diagnostic);
}

if is_error {
if is_lint {
self.lint_err_count += 1;
} else {
self.err_count += 1;
}
self.panic_if_treat_err_as_bug();
}

#[allow(deprecated)]
{
guaranteed = Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
}
#[allow(deprecated)]
if level == Level::Error {
guaranteed = Some(ErrorGuaranteed::unchecked_error_guaranteed());
}
});

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl Session {
pub fn compile_status(&self) -> Result<(), ErrorGuaranteed> {
// We must include lint errors here.
if let Some(reported) = self.dcx().has_errors_or_lint_errors() {
let _ = self.dcx().emit_stashed_diagnostics();
self.dcx().emit_stashed_diagnostics();
Err(reported)
} else {
Ok(())
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2477,10 +2477,9 @@ where
pub struct ErrorGuaranteed(());

impl ErrorGuaranteed {
/// To be used only if you really know what you are doing... ideally, we would find a way to
/// eliminate all calls to this method.
#[deprecated = "`Session::span_delayed_bug` should be preferred over this function"]
pub fn unchecked_claim_error_was_emitted() -> Self {
/// Don't use this outside of `DiagCtxtInner::emit_diagnostic`!
#[deprecated = "should only be used in `DiagCtxtInner::emit_diagnostic`"]
pub fn unchecked_error_guaranteed() -> Self {
ErrorGuaranteed(())
}
}
Expand Down
Loading

0 comments on commit 600d3bb

Please sign in to comment.