Skip to content

Commit 600d3bb

Browse files
authored
Rollup merge of #120735 - nnethercote:rm-some-unchecked_claims, r=oli-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`
2 parents 4a110f2 + 6889fe3 commit 600d3bb

File tree

8 files changed

+103
-154
lines changed

8 files changed

+103
-154
lines changed

compiler/rustc_driver_impl/src/lib.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ use rustc_data_structures::profiling::{
2424
get_resident_set_size, print_time_passes_entry, TimePassesFormat,
2525
};
2626
use rustc_errors::registry::Registry;
27-
use rustc_errors::{markdown, ColorConfig, DiagCtxt, ErrCode, ErrorGuaranteed, PResult};
27+
use rustc_errors::{
28+
markdown, ColorConfig, DiagCtxt, ErrCode, ErrorGuaranteed, FatalError, PResult,
29+
};
2830
use rustc_feature::find_gated_cfg;
2931
use rustc_interface::util::{self, collect_crate_types, get_codegen_backend};
3032
use rustc_interface::{interface, Queries};
@@ -1231,11 +1233,10 @@ fn parse_crate_attrs<'a>(sess: &'a Session) -> PResult<'a, ast::AttrVec> {
12311233
/// The compiler currently unwinds with a special sentinel value to abort
12321234
/// compilation on fatal errors. This function catches that sentinel and turns
12331235
/// the panic into a `Result` instead.
1234-
pub fn catch_fatal_errors<F: FnOnce() -> R, R>(f: F) -> Result<R, ErrorGuaranteed> {
1236+
pub fn catch_fatal_errors<F: FnOnce() -> R, R>(f: F) -> Result<R, FatalError> {
12351237
catch_unwind(panic::AssertUnwindSafe(f)).map_err(|value| {
12361238
if value.is::<rustc_errors::FatalErrorMarker>() {
1237-
#[allow(deprecated)]
1238-
ErrorGuaranteed::unchecked_claim_error_was_emitted()
1239+
FatalError
12391240
} else {
12401241
panic::resume_unwind(value);
12411242
}
@@ -1245,9 +1246,9 @@ pub fn catch_fatal_errors<F: FnOnce() -> R, R>(f: F) -> Result<R, ErrorGuarantee
12451246
/// Variant of `catch_fatal_errors` for the `interface::Result` return type
12461247
/// that also computes the exit code.
12471248
pub fn catch_with_exit_code(f: impl FnOnce() -> interface::Result<()>) -> i32 {
1248-
match catch_fatal_errors(f).flatten() {
1249-
Ok(()) => EXIT_SUCCESS,
1250-
Err(_) => EXIT_FAILURE,
1249+
match catch_fatal_errors(f) {
1250+
Ok(Ok(())) => EXIT_SUCCESS,
1251+
_ => EXIT_FAILURE,
12511252
}
12521253
}
12531254

compiler/rustc_errors/src/diagnostic_builder.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,20 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
9999
}
100100

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

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

compiler/rustc_errors/src/lib.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ impl DiagCtxt {
708708
}
709709

710710
/// Emit all stashed diagnostics.
711-
pub fn emit_stashed_diagnostics(&self) -> Option<ErrorGuaranteed> {
711+
pub fn emit_stashed_diagnostics(&self) {
712712
self.inner.borrow_mut().emit_stashed_diagnostics()
713713
}
714714

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

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

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

@@ -1216,9 +1219,8 @@ impl DiagCtxt {
12161219
// `DiagCtxtInner::foo`.
12171220
impl DiagCtxtInner {
12181221
/// Emit all stashed diagnostics.
1219-
fn emit_stashed_diagnostics(&mut self) -> Option<ErrorGuaranteed> {
1222+
fn emit_stashed_diagnostics(&mut self) {
12201223
let has_errors = self.has_errors();
1221-
let mut reported = None;
12221224
for (_, diag) in std::mem::take(&mut self.stashed_diagnostics).into_iter() {
12231225
// Decrement the count tracking the stash; emitting will increment it.
12241226
if diag.is_error() {
@@ -1235,12 +1237,11 @@ impl DiagCtxtInner {
12351237
continue;
12361238
}
12371239
}
1238-
let reported_this = self.emit_diagnostic(diag);
1239-
reported = reported.or(reported_this);
1240+
self.emit_diagnostic(diag);
12401241
}
1241-
reported
12421242
}
12431243

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

@@ -1285,7 +1286,7 @@ impl DiagCtxtInner {
12851286
let backtrace = std::backtrace::Backtrace::capture();
12861287
self.delayed_bugs.push(DelayedDiagnostic::with_backtrace(diagnostic, backtrace));
12871288
#[allow(deprecated)]
1288-
return Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
1289+
return Some(ErrorGuaranteed::unchecked_error_guaranteed());
12891290
}
12901291
GoodPathDelayedBug => {
12911292
let backtrace = std::backtrace::Backtrace::capture();
@@ -1319,6 +1320,7 @@ impl DiagCtxtInner {
13191320
!self.emitted_diagnostics.insert(diagnostic_hash)
13201321
};
13211322

1323+
let level = diagnostic.level;
13221324
let is_error = diagnostic.is_error();
13231325
let is_lint = diagnostic.is_lint.is_some();
13241326

@@ -1355,18 +1357,19 @@ impl DiagCtxtInner {
13551357

13561358
self.emitter.emit_diagnostic(diagnostic);
13571359
}
1360+
13581361
if is_error {
13591362
if is_lint {
13601363
self.lint_err_count += 1;
13611364
} else {
13621365
self.err_count += 1;
13631366
}
13641367
self.panic_if_treat_err_as_bug();
1368+
}
13651369

1366-
#[allow(deprecated)]
1367-
{
1368-
guaranteed = Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
1369-
}
1370+
#[allow(deprecated)]
1371+
if level == Level::Error {
1372+
guaranteed = Some(ErrorGuaranteed::unchecked_error_guaranteed());
13701373
}
13711374
});
13721375

compiler/rustc_session/src/session.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl Session {
315315
pub fn compile_status(&self) -> Result<(), ErrorGuaranteed> {
316316
// We must include lint errors here.
317317
if let Some(reported) = self.dcx().has_errors_or_lint_errors() {
318-
let _ = self.dcx().emit_stashed_diagnostics();
318+
self.dcx().emit_stashed_diagnostics();
319319
Err(reported)
320320
} else {
321321
Ok(())

compiler/rustc_span/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -2477,10 +2477,9 @@ where
24772477
pub struct ErrorGuaranteed(());
24782478

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

0 commit comments

Comments
 (0)