Remove DiagnosticBuilder::into_diagnostic from -Ztreat-err-as-bug consideration.

It seems very wrong to have a `-Ztreat-err-as-bug` check here before the
error is even emitted.

Once that's done:
- `into_diagnostic` is infallible, so its return type doesn't need the
  `Option`;
- the `&'a DiagCtxt` also isn't needed, because only one callsite uses
  it, and it already have access to it via `self.dcx`;
- the comments about dcx disabling buffering are no longer true, this is
  unconditional now;
- and the `debug!` seems unnecessary... the comment greatly overstates
  its importance because few diagnostics come through `into_diagnostic`,
  and `-Ztrack-diagnostics` exists anyway.
This commit is contained in:
Nicholas Nethercote 2024-01-10 15:00:20 +11:00
parent a0f5431e23
commit 552bed8048

View File

@ -255,35 +255,18 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
/// Stashes diagnostic for possible later improvement in a different,
/// later stage of the compiler. The diagnostic can be accessed with
/// the provided `span` and `key` through [`DiagCtxt::steal_diagnostic()`].
///
/// As with `buffer`, this is unless the dcx has disabled such buffering.
pub fn stash(self, span: Span, key: StashKey) {
if let Some((diag, dcx)) = self.into_diagnostic() {
dcx.stash_diagnostic(span, key, diag);
}
self.dcx.stash_diagnostic(span, key, self.into_diagnostic());
}
/// Converts the builder to a `Diagnostic` for later emission,
/// unless dcx has disabled such buffering.
fn into_diagnostic(mut self) -> Option<(Diagnostic, &'a DiagCtxt)> {
if self.dcx.inner.lock().flags.treat_err_as_bug.is_some() {
self.emit();
return None;
}
let diag = self.take_diag();
// Logging here is useful to help track down where in logs an error was
// actually emitted.
debug!("buffer: diag={:?}", diag);
Some((diag, self.dcx))
/// Converts the builder to a `Diagnostic` for later emission.
fn into_diagnostic(mut self) -> Diagnostic {
self.take_diag()
}
/// Buffers the diagnostic for later emission,
/// unless dcx has disabled such buffering.
/// Buffers the diagnostic for later emission.
pub fn buffer(self, buffered_diagnostics: &mut Vec<Diagnostic>) {
buffered_diagnostics.extend(self.into_diagnostic().map(|(diag, _)| diag));
buffered_diagnostics.push(self.into_diagnostic());
}
/// Delay emission of this diagnostic as a bug.