Remove HandlerInner::emit.

This is weird: `HandlerInner::emit` calls
`HandlerInner::emit_diagnostic`, but only after doing a
`treat-err-as-bug` check. Which is fine, *except* that there are
multiple others paths for an `Error` or `Fatal` diagnostic to be passed
to `HandlerInner::emit_diagnostic` without going through
`HandlerInner::emit`, e.g. `Handler::span_err` call
`Handler::emit_diag_at_span`, which calls `emit_diagnostic`.
So that suggests that the coverage for `treat-err-as-bug` is incomplete.

This commit removes `HandlerInner::emit` and moves the
`treat-err-as-bug` check to `HandlerInner::emit_diagnostic`, so it
cannot by bypassed.
This commit is contained in:
Nicholas Nethercote 2023-12-04 14:27:43 +11:00
parent a8ff867d5c
commit 883bdb7fda

View File

@ -1096,7 +1096,10 @@ pub fn fatal(&self, msg: impl Into<DiagnosticMessage>) -> ! {
#[rustc_lint_diagnostics]
pub fn err(&self, msg: impl Into<DiagnosticMessage>) -> ErrorGuaranteed {
self.inner.borrow_mut().emit(Error { lint: false }, msg)
self.inner
.borrow_mut()
.emit_diagnostic(&mut Diagnostic::new(Error { lint: false }, msg))
.unwrap()
}
#[rustc_lint_diagnostics]
@ -1443,6 +1446,11 @@ fn emit_stashed_diagnostics(&mut self) -> Option<ErrorGuaranteed> {
// FIXME(eddyb) this should ideally take `diagnostic` by value.
fn emit_diagnostic(&mut self, diagnostic: &mut Diagnostic) -> Option<ErrorGuaranteed> {
if matches!(diagnostic.level, Level::Error { .. } | Level::Fatal) && self.treat_err_as_bug()
{
diagnostic.level = Level::Bug;
}
// The `LintExpectationId` can be stable or unstable depending on when it was created.
// Diagnostics created before the definition of `HirId`s are unstable and can not yet
// be stored. Instead, they are buffered until the `LintExpectationId` is replaced by
@ -1589,18 +1597,10 @@ fn failure_note(&mut self, msg: impl Into<DiagnosticMessage>) {
// Note: unlike `Handler::fatal`, this doesn't return `!`, because that is
// inappropriate for some of its call sites.
fn fatal_no_raise(&mut self, msg: impl Into<DiagnosticMessage>) -> FatalError {
self.emit(Fatal, msg);
self.emit_diagnostic(&mut Diagnostic::new(Fatal, msg));
FatalError
}
/// Emit an error; level should be `Error` or `Fatal`.
fn emit(&mut self, level: Level, msg: impl Into<DiagnosticMessage>) -> ErrorGuaranteed {
if self.treat_err_as_bug() {
self.bug(msg);
}
self.emit_diagnostic(&mut Diagnostic::new(level, msg)).unwrap()
}
fn bug(&mut self, msg: impl Into<DiagnosticMessage>) -> ! {
self.emit_diagnostic(&mut Diagnostic::new(Bug, msg));
panic::panic_any(ExplicitBug);