Skip to content

Commit 9a78412

Browse files
committed
Split Handler::emit_diagnostic in two.
Currently, `emit_diagnostic` takes `&mut self`. This commit changes it so `emit_diagnostic` takes `self` and the new `emit_diagnostic_without_consuming` function takes `&mut self`. I find the distinction useful. The former case is much more common, and avoids a bunch of `mut` and `&mut` occurrences. We can also restrict the latter with `pub(crate)` which is nice.
1 parent 2c2c7f1 commit 9a78412

File tree

12 files changed

+64
-45
lines changed

12 files changed

+64
-45
lines changed

compiler/rustc_borrowck/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2502,8 +2502,8 @@ mod error {
25022502
if !self.errors.buffered.is_empty() {
25032503
self.errors.buffered.sort_by_key(|diag| diag.sort_span);
25042504

2505-
for mut diag in self.errors.buffered.drain(..) {
2506-
self.infcx.tcx.sess.diagnostic().emit_diagnostic(&mut diag);
2505+
for diag in self.errors.buffered.drain(..) {
2506+
self.infcx.tcx.sess.diagnostic().emit_diagnostic(diag);
25072507
}
25082508
}
25092509

compiler/rustc_codegen_ssa/src/back/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1848,7 +1848,7 @@ impl SharedEmitterMain {
18481848
d.code(code);
18491849
}
18501850
d.replace_args(diag.args);
1851-
handler.emit_diagnostic(&mut d);
1851+
handler.emit_diagnostic(d);
18521852
}
18531853
Ok(SharedEmitterMessage::InlineAsmError(cookie, msg, level, source)) => {
18541854
let msg = msg.strip_prefix("error: ").unwrap_or(&msg).to_string();

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
277277
// "secondary" errors if they occurred.
278278
let secondary_errors = mem::take(&mut self.secondary_errors);
279279
if self.error_emitted.is_none() {
280-
for mut error in secondary_errors {
281-
self.tcx.sess.diagnostic().emit_diagnostic(&mut error);
280+
for error in secondary_errors {
281+
self.tcx.sess.diagnostic().emit_diagnostic(error);
282282
}
283283
} else {
284284
assert!(self.tcx.sess.has_errors().is_some());

compiler/rustc_errors/src/diagnostic_builder.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl EmissionGuarantee for ErrorGuaranteed {
132132
DiagnosticBuilderState::Emittable(handler) => {
133133
db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation;
134134

135-
let guar = handler.emit_diagnostic(&mut db.inner.diagnostic);
135+
let guar = handler.emit_diagnostic_without_consuming(&mut db.inner.diagnostic);
136136

137137
// Only allow a guarantee if the `level` wasn't switched to a
138138
// non-error - the field isn't `pub`, but the whole `Diagnostic`
@@ -181,7 +181,7 @@ impl EmissionGuarantee for () {
181181
DiagnosticBuilderState::Emittable(handler) => {
182182
db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation;
183183

184-
handler.emit_diagnostic(&mut db.inner.diagnostic);
184+
handler.emit_diagnostic_without_consuming(&mut db.inner.diagnostic);
185185
}
186186
// `.emit()` was previously called, disallowed from repeating it.
187187
DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {}
@@ -207,7 +207,7 @@ impl EmissionGuarantee for Noted {
207207
// First `.emit()` call, the `&Handler` is still available.
208208
DiagnosticBuilderState::Emittable(handler) => {
209209
db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation;
210-
handler.emit_diagnostic(&mut db.inner.diagnostic);
210+
handler.emit_diagnostic_without_consuming(&mut db.inner.diagnostic);
211211
}
212212
// `.emit()` was previously called, disallowed from repeating it.
213213
DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {}
@@ -236,7 +236,7 @@ impl EmissionGuarantee for Bug {
236236
DiagnosticBuilderState::Emittable(handler) => {
237237
db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation;
238238

239-
handler.emit_diagnostic(&mut db.inner.diagnostic);
239+
handler.emit_diagnostic_without_consuming(&mut db.inner.diagnostic);
240240
}
241241
// `.emit()` was previously called, disallowed from repeating it.
242242
DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {}
@@ -260,7 +260,7 @@ impl EmissionGuarantee for ! {
260260
DiagnosticBuilderState::Emittable(handler) => {
261261
db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation;
262262

263-
handler.emit_diagnostic(&mut db.inner.diagnostic);
263+
handler.emit_diagnostic_without_consuming(&mut db.inner.diagnostic);
264264
}
265265
// `.emit()` was previously called, disallowed from repeating it.
266266
DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {}
@@ -284,7 +284,7 @@ impl EmissionGuarantee for rustc_span::fatal_error::FatalError {
284284
DiagnosticBuilderState::Emittable(handler) => {
285285
db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation;
286286

287-
handler.emit_diagnostic(&mut db.inner.diagnostic);
287+
handler.emit_diagnostic_without_consuming(&mut db.inner.diagnostic);
288288
}
289289
// `.emit()` was previously called, disallowed from repeating it.
290290
DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {}
@@ -365,7 +365,9 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
365365
}
366366
}
367367

368-
/// Emit the diagnostic.
368+
/// Emit the diagnostic. Does not consume `self`, which may be surprising,
369+
/// but there are various places that rely on continuing to use `self`
370+
/// after calling `emit`.
369371
#[track_caller]
370372
pub fn emit(&mut self) -> G {
371373
G::diagnostic_builder_emit_producing_guarantee(self)
@@ -640,13 +642,13 @@ impl Drop for DiagnosticBuilderInner<'_> {
640642
// No `.emit()` or `.cancel()` calls.
641643
DiagnosticBuilderState::Emittable(handler) => {
642644
if !panicking() {
643-
handler.emit_diagnostic(&mut Diagnostic::new(
645+
handler.emit_diagnostic(Diagnostic::new(
644646
Level::Bug,
645647
DiagnosticMessage::from(
646648
"the following error was constructed but not emitted",
647649
),
648650
));
649-
handler.emit_diagnostic(&mut self.diagnostic);
651+
handler.emit_diagnostic_without_consuming(&mut self.diagnostic);
650652
panic!("error was constructed but not emitted");
651653
}
652654
}

compiler/rustc_errors/src/emitter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ impl Emitter for SilentEmitter {
581581
if let Some(ref note) = self.fatal_note {
582582
d.note(note.clone());
583583
}
584-
self.fatal_handler.emit_diagnostic(&mut d);
584+
self.fatal_handler.emit_diagnostic(d);
585585
}
586586
}
587587
}

compiler/rustc_errors/src/lib.rs

+35-16
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ impl Handler {
10541054
}
10551055
let mut diagnostic = Diagnostic::new(Level::DelayedBug, msg);
10561056
diagnostic.set_span(sp);
1057-
inner.emit_diagnostic(&mut diagnostic).unwrap()
1057+
inner.emit_diagnostic(diagnostic).unwrap()
10581058
}
10591059

10601060
// FIXME(eddyb) note the comment inside `impl Drop for HandlerInner`, that's
@@ -1064,15 +1064,17 @@ impl Handler {
10641064

10651065
let mut diagnostic = Diagnostic::new(Level::DelayedBug, msg);
10661066
if inner.flags.report_delayed_bugs {
1067-
inner.emit_diagnostic(&mut diagnostic);
1067+
inner.emit_diagnostic_without_consuming(&mut diagnostic);
10681068
}
10691069
let backtrace = std::backtrace::Backtrace::capture();
10701070
inner.good_path_delayed_bugs.push(DelayedDiagnostic::with_backtrace(diagnostic, backtrace));
10711071
}
10721072

10731073
#[track_caller]
10741074
pub fn span_bug_no_panic(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) {
1075-
self.emit_diagnostic(Diagnostic::new(Bug, msg).set_span(span));
1075+
let mut diag = Diagnostic::new(Bug, msg);
1076+
diag.set_span(span);
1077+
self.emit_diagnostic(diag);
10761078
}
10771079

10781080
#[track_caller]
@@ -1185,10 +1187,10 @@ impl Handler {
11851187
DiagnosticMessage::Str(warnings),
11861188
)),
11871189
(_, 0) => {
1188-
inner.emit_diagnostic(&mut Diagnostic::new(Fatal, errors));
1190+
inner.emit_diagnostic(Diagnostic::new(Fatal, errors));
11891191
}
11901192
(_, _) => {
1191-
inner.emit_diagnostic(&mut Diagnostic::new(Fatal, format!("{errors}; {warnings}")));
1193+
inner.emit_diagnostic(Diagnostic::new(Fatal, format!("{errors}; {warnings}")));
11921194
}
11931195
}
11941196

@@ -1255,8 +1257,17 @@ impl Handler {
12551257
self.inner.borrow_mut().emitter.emit_diagnostic(&db);
12561258
}
12571259

1258-
pub fn emit_diagnostic(&self, diagnostic: &mut Diagnostic) -> Option<ErrorGuaranteed> {
1259-
self.inner.borrow_mut().emit_diagnostic(diagnostic)
1260+
pub fn emit_diagnostic(&self, mut diagnostic: Diagnostic) -> Option<ErrorGuaranteed> {
1261+
self.emit_diagnostic_without_consuming(&mut diagnostic)
1262+
}
1263+
1264+
// It's unfortunate this exists. `emit_diagnostic` is preferred, because it
1265+
// consumes the diagnostic, thus ensuring it is emitted just once.
1266+
pub(crate) fn emit_diagnostic_without_consuming(
1267+
&self,
1268+
diagnostic: &mut Diagnostic,
1269+
) -> Option<ErrorGuaranteed> {
1270+
self.inner.borrow_mut().emit_diagnostic_without_consuming(diagnostic)
12601271
}
12611272

12621273
pub fn emit_err<'a>(&'a self, err: impl IntoDiagnostic<'a>) -> ErrorGuaranteed {
@@ -1370,7 +1381,7 @@ impl Handler {
13701381
// Here the diagnostic is given back to `emit_diagnostic` where it was first
13711382
// intercepted. Now it should be processed as usual, since the unstable expectation
13721383
// id is now stable.
1373-
inner.emit_diagnostic(&mut diag);
1384+
inner.emit_diagnostic(diag);
13741385
}
13751386
}
13761387

@@ -1412,7 +1423,7 @@ impl HandlerInner {
14121423
let has_errors = self.has_errors();
14131424
let diags = self.stashed_diagnostics.drain(..).map(|x| x.1).collect::<Vec<_>>();
14141425
let mut reported = None;
1415-
for mut diag in diags {
1426+
for diag in diags {
14161427
// Decrement the count tracking the stash; emitting will increment it.
14171428
if diag.is_error() {
14181429
if matches!(diag.level, Level::Error { lint: true }) {
@@ -1432,14 +1443,20 @@ impl HandlerInner {
14321443
}
14331444
}
14341445
}
1435-
let reported_this = self.emit_diagnostic(&mut diag);
1446+
let reported_this = self.emit_diagnostic(diag);
14361447
reported = reported.or(reported_this);
14371448
}
14381449
reported
14391450
}
14401451

1441-
// FIXME(eddyb) this should ideally take `diagnostic` by value.
1442-
fn emit_diagnostic(&mut self, diagnostic: &mut Diagnostic) -> Option<ErrorGuaranteed> {
1452+
fn emit_diagnostic(&mut self, mut diagnostic: Diagnostic) -> Option<ErrorGuaranteed> {
1453+
self.emit_diagnostic_without_consuming(&mut diagnostic)
1454+
}
1455+
1456+
fn emit_diagnostic_without_consuming(
1457+
&mut self,
1458+
diagnostic: &mut Diagnostic,
1459+
) -> Option<ErrorGuaranteed> {
14431460
if matches!(diagnostic.level, Level::Error { .. } | Level::Fatal) && self.treat_err_as_bug()
14441461
{
14451462
diagnostic.level = Level::Bug;
@@ -1576,12 +1593,14 @@ impl HandlerInner {
15761593

15771594
#[track_caller]
15781595
fn span_bug(&mut self, sp: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) -> ! {
1579-
self.emit_diagnostic(Diagnostic::new(Bug, msg).set_span(sp));
1596+
let mut diag = Diagnostic::new(Bug, msg);
1597+
diag.set_span(sp);
1598+
self.emit_diagnostic(diag);
15801599
panic::panic_any(ExplicitBug);
15811600
}
15821601

15831602
fn failure_note(&mut self, msg: impl Into<DiagnosticMessage>) {
1584-
self.emit_diagnostic(&mut Diagnostic::new(FailureNote, msg));
1603+
self.emit_diagnostic(Diagnostic::new(FailureNote, msg));
15851604
}
15861605

15871606
fn flush_delayed(
@@ -1613,7 +1632,7 @@ impl HandlerInner {
16131632
if no_bugs {
16141633
// Put the overall explanation before the `DelayedBug`s, to
16151634
// frame them better (e.g. separate warnings from them).
1616-
self.emit_diagnostic(&mut Diagnostic::new(Bug, explanation));
1635+
self.emit_diagnostic(Diagnostic::new(Bug, explanation));
16171636
no_bugs = false;
16181637
}
16191638

@@ -1628,7 +1647,7 @@ impl HandlerInner {
16281647
}
16291648
bug.level = Level::Bug;
16301649

1631-
self.emit_diagnostic(&mut bug);
1650+
self.emit_diagnostic(bug);
16321651
}
16331652

16341653
// Panic with `DelayedBugPanic` to avoid "unexpected panic" messages.

compiler/rustc_expand/src/proc_macro_server.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ impl server::FreeFunctions for Rustc<'_, '_> {
502502
None,
503503
);
504504
}
505-
self.sess().span_diagnostic.emit_diagnostic(&mut diag);
505+
self.sess().span_diagnostic.emit_diagnostic(diag);
506506
}
507507
}
508508

compiler/rustc_hir_typeck/src/writeback.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,8 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
504504

505505
if !errors_buffer.is_empty() {
506506
errors_buffer.sort_by_key(|diag| diag.span.primary_span());
507-
for mut diag in errors_buffer {
508-
self.tcx().sess.diagnostic().emit_diagnostic(&mut diag);
507+
for diag in errors_buffer {
508+
self.tcx().sess.diagnostic().emit_diagnostic(diag);
509509
}
510510
}
511511
}

compiler/rustc_parse/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ macro_rules! panictry_buffer {
5151
match $e {
5252
Ok(e) => e,
5353
Err(errs) => {
54-
for mut e in errs {
55-
$handler.emit_diagnostic(&mut e);
54+
for e in errs {
55+
$handler.emit_diagnostic(e);
5656
}
5757
FatalError.raise()
5858
}
@@ -165,8 +165,8 @@ fn try_file_to_source_file(
165165
fn file_to_source_file(sess: &ParseSess, path: &Path, spanopt: Option<Span>) -> Lrc<SourceFile> {
166166
match try_file_to_source_file(sess, path, spanopt) {
167167
Ok(source_file) => source_file,
168-
Err(mut d) => {
169-
sess.span_diagnostic.emit_diagnostic(&mut d);
168+
Err(d) => {
169+
sess.span_diagnostic.emit_diagnostic(d);
170170
FatalError.raise();
171171
}
172172
}

compiler/rustc_query_system/src/dep_graph/graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -926,8 +926,8 @@ impl<D: Deps> DepGraphData<D> {
926926

927927
let handle = qcx.dep_context().sess().diagnostic();
928928

929-
for mut diagnostic in side_effects.diagnostics {
930-
handle.emit_diagnostic(&mut diagnostic);
929+
for diagnostic in side_effects.diagnostics {
930+
handle.emit_diagnostic(diagnostic);
931931
}
932932
}
933933
}

src/tools/miri/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ pub fn report_msg<'tcx>(
512512
}
513513
}
514514

515-
handler.emit_diagnostic(&mut err);
515+
handler.emit_diagnostic(err);
516516
}
517517

518518
impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {

src/tools/rustfmt/src/parse/session.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,8 @@ impl ParseSess {
284284
// Methods that should be restricted within the parse module.
285285
impl ParseSess {
286286
pub(super) fn emit_diagnostics(&self, diagnostics: Vec<Diagnostic>) {
287-
for mut diagnostic in diagnostics {
288-
self.parse_sess
289-
.span_diagnostic
290-
.emit_diagnostic(&mut diagnostic);
287+
for diagnostic in diagnostics {
288+
self.parse_sess.span_diagnostic.emit_diagnostic(diagnostic);
291289
}
292290
}
293291

0 commit comments

Comments
 (0)