Skip to content

Commit 4d84a6e

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 bfb54c6 commit 4d84a6e

File tree

13 files changed

+62
-43
lines changed

13 files changed

+62
-43
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

+32-15
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ impl Handler {
10611061
}
10621062
let mut diagnostic = Diagnostic::new(Level::DelayedBug, msg);
10631063
diagnostic.set_span(sp);
1064-
inner.emit_diagnostic(&mut diagnostic).unwrap()
1064+
inner.emit_diagnostic(diagnostic).unwrap()
10651065
}
10661066

10671067
// FIXME(eddyb) note the comment inside `impl Drop for HandlerInner`, that's
@@ -1071,7 +1071,7 @@ impl Handler {
10711071

10721072
let mut diagnostic = Diagnostic::new(Level::DelayedBug, msg);
10731073
if inner.flags.report_delayed_bugs {
1074-
inner.emit_diagnostic(&mut diagnostic);
1074+
inner.emit_diagnostic_without_consuming(&mut diagnostic);
10751075
}
10761076
let backtrace = std::backtrace::Backtrace::capture();
10771077
inner.good_path_delayed_bugs.push(DelayedDiagnostic::with_backtrace(diagnostic, backtrace));
@@ -1186,10 +1186,10 @@ impl Handler {
11861186
DiagnosticMessage::Str(warnings),
11871187
)),
11881188
(_, 0) => {
1189-
inner.emit_diagnostic(&mut Diagnostic::new(Fatal, errors));
1189+
inner.emit_diagnostic(Diagnostic::new(Fatal, errors));
11901190
}
11911191
(_, _) => {
1192-
inner.emit_diagnostic(&mut Diagnostic::new(Fatal, format!("{errors}; {warnings}")));
1192+
inner.emit_diagnostic(Diagnostic::new(Fatal, format!("{errors}; {warnings}")));
11931193
}
11941194
}
11951195

@@ -1256,8 +1256,17 @@ impl Handler {
12561256
self.inner.borrow_mut().emitter.emit_diagnostic(&db);
12571257
}
12581258

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

12631272
pub fn emit_err<'a>(&'a self, err: impl IntoDiagnostic<'a>) -> ErrorGuaranteed {
@@ -1371,7 +1380,7 @@ impl Handler {
13711380
// Here the diagnostic is given back to `emit_diagnostic` where it was first
13721381
// intercepted. Now it should be processed as usual, since the unstable expectation
13731382
// id is now stable.
1374-
inner.emit_diagnostic(&mut diag);
1383+
inner.emit_diagnostic(diag);
13751384
}
13761385
}
13771386

@@ -1413,7 +1422,7 @@ impl HandlerInner {
14131422
let has_errors = self.has_errors();
14141423
let diags = self.stashed_diagnostics.drain(..).map(|x| x.1).collect::<Vec<_>>();
14151424
let mut reported = None;
1416-
for mut diag in diags {
1425+
for diag in diags {
14171426
// Decrement the count tracking the stash; emitting will increment it.
14181427
if diag.is_error() {
14191428
if matches!(diag.level, Level::Error { lint: true }) {
@@ -1433,14 +1442,20 @@ impl HandlerInner {
14331442
}
14341443
}
14351444
}
1436-
let reported_this = self.emit_diagnostic(&mut diag);
1445+
let reported_this = self.emit_diagnostic(diag);
14371446
reported = reported.or(reported_this);
14381447
}
14391448
reported
14401449
}
14411450

1442-
// FIXME(eddyb) this should ideally take `diagnostic` by value.
1443-
fn emit_diagnostic(&mut self, diagnostic: &mut Diagnostic) -> Option<ErrorGuaranteed> {
1451+
fn emit_diagnostic(&mut self, mut diagnostic: Diagnostic) -> Option<ErrorGuaranteed> {
1452+
self.emit_diagnostic_without_consuming(&mut diagnostic)
1453+
}
1454+
1455+
fn emit_diagnostic_without_consuming(
1456+
&mut self,
1457+
diagnostic: &mut Diagnostic,
1458+
) -> Option<ErrorGuaranteed> {
14441459
if matches!(diagnostic.level, Level::Error { .. } | Level::Fatal) && self.treat_err_as_bug()
14451460
{
14461461
diagnostic.level = Level::Bug;
@@ -1577,12 +1592,14 @@ impl HandlerInner {
15771592

15781593
#[track_caller]
15791594
fn span_bug(&mut self, sp: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) -> ! {
1580-
self.emit_diagnostic(Diagnostic::new(Bug, msg).set_span(sp));
1595+
let mut diag = Diagnostic::new(Bug, msg);
1596+
diag.set_span(sp);
1597+
self.emit_diagnostic(diag);
15811598
panic::panic_any(ExplicitBug);
15821599
}
15831600

15841601
fn failure_note(&mut self, msg: impl Into<DiagnosticMessage>) {
1585-
self.emit_diagnostic(&mut Diagnostic::new(FailureNote, msg));
1602+
self.emit_diagnostic(Diagnostic::new(FailureNote, msg));
15861603
}
15871604

15881605
fn flush_delayed(
@@ -1614,7 +1631,7 @@ impl HandlerInner {
16141631
if no_bugs {
16151632
// Put the overall explanation before the `DelayedBug`s, to
16161633
// frame them better (e.g. separate warnings from them).
1617-
self.emit_diagnostic(&mut Diagnostic::new(Bug, explanation));
1634+
self.emit_diagnostic(Diagnostic::new(Bug, explanation));
16181635
no_bugs = false;
16191636
}
16201637

@@ -1629,7 +1646,7 @@ impl HandlerInner {
16291646
}
16301647
bug.level = Level::Bug;
16311648

1632-
self.emit_diagnostic(&mut bug);
1649+
self.emit_diagnostic(bug);
16331650
}
16341651

16351652
// 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/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ fn fatally_break_rust(tcx: TyCtxt<'_>) {
416416
let handler = tcx.sess.diagnostic();
417417
// We normally use `handler.bug()` for bugs, but this is an exceptional case, so we do this
418418
// instead to avoid an abort.
419-
handler.emit_diagnostic(&mut Diagnostic::new(
419+
handler.emit_diagnostic(Diagnostic::new(
420420
Level::Bug,
421421
"It looks like you're trying to break rust; would you like some ICE?",
422422
));

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-2
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,10 @@ 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 {
287+
for diagnostic in diagnostics {
288288
self.parse_sess
289289
.span_diagnostic
290-
.emit_diagnostic(&mut diagnostic);
290+
.emit_diagnostic(diagnostic);
291291
}
292292
}
293293

0 commit comments

Comments
 (0)