Skip to content

Commit

Permalink
Remove Handler::emit_diag_at_span.
Browse files Browse the repository at this point in the history
Compare `Handler::warn` and `Handler::span_warn`. Conceptually they are
almost identical. But their implementations are weirdly different.

`warn`:
- calls `DiagnosticBuilder::<()>::new(self, Warning(None), msg)`, then `emit()`
- which calls `G::diagnostic_builder_emit_producing_guarantee(self)`
- which calls `handler.emit_diagnostic(&mut db.inner.diagnostic)`

`span_warn`:
- calls `self.emit_diag_at_span(Diagnostic::new(Warning(None), msg), span)`
- which calls `self.emit_diagnostic(diag.set_span(sp))`

I.e. they both end up at `emit_diagnostic`, but take very different
routes to get there.

This commit changes `span_*` and similar ones to not use
`emit_diag_at_span`. Instead they just call `struct_span_*` + `emit`.

Some nice side-effects of this:
- `span_fatal` and `span_fatal_with_code` don't need
  `FatalError.raise()`, because `emit` does that.
- `span_err` and `span_err_with_code` doesn't need `unwrap`.
- `struct_span_note`'s `span` arg type is changed from `Span` to
  `impl Into<MultiSpan>` like all the other functions.
  • Loading branch information
nnethercote committed Dec 14, 2023
1 parent b0d5b44 commit 2c2c7f1
Showing 1 changed file with 9 additions and 23 deletions.
32 changes: 9 additions & 23 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -975,8 +975,7 @@ impl Handler {
#[rustc_lint_diagnostics]
#[track_caller]
pub fn span_fatal(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) -> ! {
self.emit_diag_at_span(Diagnostic::new(Fatal, msg), span);
FatalError.raise()
self.struct_span_fatal(span, msg).emit()
}

#[rustc_lint_diagnostics]
Expand All @@ -987,8 +986,7 @@ impl Handler {
msg: impl Into<DiagnosticMessage>,
code: DiagnosticId,
) -> ! {
self.emit_diag_at_span(Diagnostic::new_with_code(Fatal, Some(code), msg), span);
FatalError.raise()
self.struct_span_fatal_with_code(span, msg, code).emit()
}

#[rustc_lint_diagnostics]
Expand All @@ -998,7 +996,7 @@ impl Handler {
span: impl Into<MultiSpan>,
msg: impl Into<DiagnosticMessage>,
) -> ErrorGuaranteed {
self.emit_diag_at_span(Diagnostic::new(Error { lint: false }, msg), span).unwrap()
self.struct_span_err(span, msg).emit()
}

#[rustc_lint_diagnostics]
Expand All @@ -1009,17 +1007,13 @@ impl Handler {
msg: impl Into<DiagnosticMessage>,
code: DiagnosticId,
) -> ErrorGuaranteed {
self.emit_diag_at_span(
Diagnostic::new_with_code(Error { lint: false }, Some(code), msg),
span,
)
.unwrap()
self.struct_span_err_with_code(span, msg, code).emit()
}

#[rustc_lint_diagnostics]
#[track_caller]
pub fn span_warn(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) {
self.emit_diag_at_span(Diagnostic::new(Warning(None), msg), span);
self.struct_span_warn(span, msg).emit()
}

#[rustc_lint_diagnostics]
Expand All @@ -1030,7 +1024,7 @@ impl Handler {
msg: impl Into<DiagnosticMessage>,
code: DiagnosticId,
) {
self.emit_diag_at_span(Diagnostic::new_with_code(Warning(None), Some(code), msg), span);
self.struct_span_warn_with_code(span, msg, code).emit()
}

pub fn span_bug(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) -> ! {
Expand Down Expand Up @@ -1078,20 +1072,20 @@ impl Handler {

#[track_caller]
pub fn span_bug_no_panic(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) {
self.emit_diag_at_span(Diagnostic::new(Bug, msg), span);
self.emit_diagnostic(Diagnostic::new(Bug, msg).set_span(span));
}

#[track_caller]
#[rustc_lint_diagnostics]
pub fn span_note(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) {
self.emit_diag_at_span(Diagnostic::new(Note, msg), span);
self.struct_span_note(span, msg).emit()
}

#[track_caller]
#[rustc_lint_diagnostics]
pub fn struct_span_note(
&self,
span: Span,
span: impl Into<MultiSpan>,
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, ()> {
let mut db = DiagnosticBuilder::new(self, Note, msg);
Expand Down Expand Up @@ -1337,14 +1331,6 @@ impl Handler {
note.into_diagnostic(self)
}

fn emit_diag_at_span(
&self,
mut diag: Diagnostic,
sp: impl Into<MultiSpan>,
) -> Option<ErrorGuaranteed> {
self.emit_diagnostic(diag.set_span(sp))
}

pub fn emit_artifact_notification(&self, path: &Path, artifact_type: &str) {
self.inner.borrow_mut().emitter.emit_artifact_notification(path, artifact_type);
}
Expand Down

0 comments on commit 2c2c7f1

Please sign in to comment.