Skip to content

Commit 056951d

Browse files
committed
Take &mut Diagnostic in emit_diagnostic.
Taking a Diagnostic by move would break the usual pattern `diag.label(..).emit()`.
1 parent 4767cce commit 056951d

File tree

13 files changed

+43
-41
lines changed

13 files changed

+43
-41
lines changed

compiler/rustc_borrowck/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2361,8 +2361,8 @@ mod error {
23612361
if !self.errors.buffered.is_empty() {
23622362
self.errors.buffered.sort_by_key(|diag| diag.sort_span);
23632363

2364-
for diag in self.errors.buffered.drain(..) {
2365-
self.infcx.tcx.sess.diagnostic().emit_diagnostic(&diag);
2364+
for mut diag in self.errors.buffered.drain(..) {
2365+
self.infcx.tcx.sess.diagnostic().emit_diagnostic(&mut diag);
23662366
}
23672367
}
23682368

compiler/rustc_codegen_ssa/src/back/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,7 @@ impl SharedEmitterMain {
17481748
if let Some(code) = diag.code {
17491749
d.code(code);
17501750
}
1751-
handler.emit_diagnostic(&d);
1751+
handler.emit_diagnostic(&mut d);
17521752
}
17531753
Ok(SharedEmitterMessage::InlineAsmError(cookie, msg, level, source)) => {
17541754
let msg = msg.strip_prefix("error: ").unwrap_or(&msg);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
255255
// "secondary" errors if they occurred.
256256
let secondary_errors = mem::take(&mut self.secondary_errors);
257257
if self.error_emitted.is_none() {
258-
for error in secondary_errors {
259-
self.tcx.sess.diagnostic().emit_diagnostic(&error);
258+
for mut error in secondary_errors {
259+
self.tcx.sess.diagnostic().emit_diagnostic(&mut error);
260260
}
261261
} else {
262262
assert!(self.tcx.sess.has_errors().is_some());

compiler/rustc_driver/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,8 +1181,8 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
11811181
// a .span_bug or .bug call has already printed what
11821182
// it wants to print.
11831183
if !info.payload().is::<rustc_errors::ExplicitBug>() {
1184-
let d = rustc_errors::Diagnostic::new(rustc_errors::Level::Bug, "unexpected panic");
1185-
handler.emit_diagnostic(&d);
1184+
let mut d = rustc_errors::Diagnostic::new(rustc_errors::Level::Bug, "unexpected panic");
1185+
handler.emit_diagnostic(&mut d);
11861186
}
11871187

11881188
let mut xs: Vec<Cow<'static, str>> = vec![

compiler/rustc_errors/src/diagnostic_builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl EmissionGuarantee for ErrorGuaranteed {
128128
DiagnosticBuilderState::Emittable(handler) => {
129129
db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation;
130130

131-
let guar = handler.emit_diagnostic(&db.inner.diagnostic);
131+
let guar = handler.emit_diagnostic(&mut db.inner.diagnostic);
132132

133133
// Only allow a guarantee if the `level` wasn't switched to a
134134
// non-error - the field isn't `pub`, but the whole `Diagnostic`
@@ -190,7 +190,7 @@ impl EmissionGuarantee for () {
190190
DiagnosticBuilderState::Emittable(handler) => {
191191
db.inner.state = DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation;
192192

193-
handler.emit_diagnostic(&db.inner.diagnostic);
193+
handler.emit_diagnostic(&mut db.inner.diagnostic);
194194
}
195195
// `.emit()` was previously called, disallowed from repeating it.
196196
DiagnosticBuilderState::AlreadyEmittedOrDuringCancellation => {}
@@ -500,11 +500,11 @@ impl Drop for DiagnosticBuilderInner<'_> {
500500
// No `.emit()` or `.cancel()` calls.
501501
DiagnosticBuilderState::Emittable(handler) => {
502502
if !panicking() {
503-
handler.emit_diagnostic(&Diagnostic::new(
503+
handler.emit_diagnostic(&mut Diagnostic::new(
504504
Level::Bug,
505505
"the following error was constructed but not emitted",
506506
));
507-
handler.emit_diagnostic(&self.diagnostic);
507+
handler.emit_diagnostic(&mut self.diagnostic);
508508
panic!();
509509
}
510510
}

compiler/rustc_errors/src/emitter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ impl Emitter for SilentEmitter {
542542
if let Some(ref note) = self.fatal_note {
543543
d.note(note);
544544
}
545-
self.fatal_handler.emit_diagnostic(&d);
545+
self.fatal_handler.emit_diagnostic(&mut d);
546546
}
547547
}
548548
}

compiler/rustc_errors/src/lib.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ impl Handler {
919919
self.inner.borrow_mut().force_print_diagnostic(db)
920920
}
921921

922-
pub fn emit_diagnostic(&self, diagnostic: &Diagnostic) -> Option<ErrorGuaranteed> {
922+
pub fn emit_diagnostic(&self, diagnostic: &mut Diagnostic) -> Option<ErrorGuaranteed> {
923923
self.inner.borrow_mut().emit_diagnostic(diagnostic)
924924
}
925925

@@ -993,25 +993,25 @@ impl HandlerInner {
993993
self.taught_diagnostics.insert(code.clone())
994994
}
995995

996-
fn force_print_diagnostic(&mut self, db: Diagnostic) {
997-
self.emitter.emit_diagnostic(&db);
996+
fn force_print_diagnostic(&mut self, mut db: Diagnostic) {
997+
self.emitter.emit_diagnostic(&mut db);
998998
}
999999

10001000
/// Emit all stashed diagnostics.
10011001
fn emit_stashed_diagnostics(&mut self) -> Option<ErrorGuaranteed> {
10021002
let diags = self.stashed_diagnostics.drain(..).map(|x| x.1).collect::<Vec<_>>();
10031003
let mut reported = None;
1004-
diags.iter().for_each(|diag| {
1004+
for mut diag in diags {
10051005
if diag.is_error() {
10061006
reported = Some(ErrorGuaranteed(()));
10071007
}
1008-
self.emit_diagnostic(diag);
1009-
});
1008+
self.emit_diagnostic(&mut diag);
1009+
}
10101010
reported
10111011
}
10121012

10131013
// FIXME(eddyb) this should ideally take `diagnostic` by value.
1014-
fn emit_diagnostic(&mut self, diagnostic: &Diagnostic) -> Option<ErrorGuaranteed> {
1014+
fn emit_diagnostic(&mut self, diagnostic: &mut Diagnostic) -> Option<ErrorGuaranteed> {
10151015
if diagnostic.level == Level::DelayedBug {
10161016
// FIXME(eddyb) this should check for `has_errors` and stop pushing
10171017
// once *any* errors were emitted (and truncate `delayed_span_bugs`
@@ -1221,22 +1221,22 @@ impl HandlerInner {
12211221
let mut diagnostic = Diagnostic::new(Level::DelayedBug, msg);
12221222
diagnostic.set_span(sp.into());
12231223
diagnostic.note(&format!("delayed at {}", std::panic::Location::caller()));
1224-
self.emit_diagnostic(&diagnostic).unwrap()
1224+
self.emit_diagnostic(&mut diagnostic).unwrap()
12251225
}
12261226

12271227
// FIXME(eddyb) note the comment inside `impl Drop for HandlerInner`, that's
12281228
// where the explanation of what "good path" is (also, it should be renamed).
12291229
fn delay_good_path_bug(&mut self, msg: &str) {
1230-
let diagnostic = Diagnostic::new(Level::DelayedBug, msg);
1230+
let mut diagnostic = Diagnostic::new(Level::DelayedBug, msg);
12311231
if self.flags.report_delayed_bugs {
1232-
self.emit_diagnostic(&diagnostic);
1232+
self.emit_diagnostic(&mut diagnostic);
12331233
}
12341234
let backtrace = std::backtrace::Backtrace::force_capture();
12351235
self.delayed_good_path_bugs.push(DelayedDiagnostic::with_backtrace(diagnostic, backtrace));
12361236
}
12371237

12381238
fn failure(&mut self, msg: &str) {
1239-
self.emit_diagnostic(&Diagnostic::new(FailureNote, msg));
1239+
self.emit_diagnostic(&mut Diagnostic::new(FailureNote, msg));
12401240
}
12411241

12421242
fn fatal(&mut self, msg: &str) -> FatalError {
@@ -1253,11 +1253,11 @@ impl HandlerInner {
12531253
if self.treat_err_as_bug() {
12541254
self.bug(msg);
12551255
}
1256-
self.emit_diagnostic(&Diagnostic::new(level, msg)).unwrap()
1256+
self.emit_diagnostic(&mut Diagnostic::new(level, msg)).unwrap()
12571257
}
12581258

12591259
fn bug(&mut self, msg: &str) -> ! {
1260-
self.emit_diagnostic(&Diagnostic::new(Bug, msg));
1260+
self.emit_diagnostic(&mut Diagnostic::new(Bug, msg));
12611261
panic::panic_any(ExplicitBug);
12621262
}
12631263

@@ -1267,7 +1267,7 @@ impl HandlerInner {
12671267
if no_bugs {
12681268
// Put the overall explanation before the `DelayedBug`s, to
12691269
// frame them better (e.g. separate warnings from them).
1270-
self.emit_diagnostic(&Diagnostic::new(Bug, explanation));
1270+
self.emit_diagnostic(&mut Diagnostic::new(Bug, explanation));
12711271
no_bugs = false;
12721272
}
12731273

@@ -1283,7 +1283,7 @@ impl HandlerInner {
12831283
}
12841284
bug.level = Level::Bug;
12851285

1286-
self.emit_diagnostic(&bug);
1286+
self.emit_diagnostic(&mut bug);
12871287
}
12881288

12891289
// Panic with `ExplicitBug` to avoid "unexpected panic" messages.

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,8 +771,8 @@ impl server::Diagnostic for Rustc<'_, '_> {
771771
) {
772772
diag.sub(level.to_internal(), msg, MultiSpan::from_spans(spans), None);
773773
}
774-
fn emit(&mut self, diag: Self::Diagnostic) {
775-
self.sess().span_diagnostic.emit_diagnostic(&diag);
774+
fn emit(&mut self, mut diag: Self::Diagnostic) {
775+
self.sess().span_diagnostic.emit_diagnostic(&mut diag);
776776
}
777777
}
778778

compiler/rustc_parse/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ macro_rules! panictry_buffer {
4949
match $e {
5050
Ok(e) => e,
5151
Err(errs) => {
52-
for e in errs {
53-
$handler.emit_diagnostic(&e);
52+
for mut e in errs {
53+
$handler.emit_diagnostic(&mut e);
5454
}
5555
FatalError.raise()
5656
}
@@ -167,8 +167,8 @@ fn try_file_to_source_file(
167167
fn file_to_source_file(sess: &ParseSess, path: &Path, spanopt: Option<Span>) -> Lrc<SourceFile> {
168168
match try_file_to_source_file(sess, path, spanopt) {
169169
Ok(source_file) => source_file,
170-
Err(d) => {
171-
sess.span_diagnostic.emit_diagnostic(&d);
170+
Err(mut d) => {
171+
sess.span_diagnostic.emit_diagnostic(&mut d);
172172
FatalError.raise();
173173
}
174174
}

compiler/rustc_query_system/src/dep_graph/graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,8 +784,8 @@ impl<K: DepKind> DepGraph<K> {
784784

785785
let handle = tcx.dep_context().sess().diagnostic();
786786

787-
for diagnostic in side_effects.diagnostics {
788-
handle.emit_diagnostic(&diagnostic);
787+
for mut diagnostic in side_effects.diagnostics {
788+
handle.emit_diagnostic(&mut diagnostic);
789789
}
790790
}
791791
}

compiler/rustc_typeck/src/check/writeback.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,8 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
467467

468468
if !errors_buffer.is_empty() {
469469
errors_buffer.sort_by_key(|diag| diag.span.primary_span());
470-
for diag in errors_buffer.drain(..) {
471-
self.tcx().sess.diagnostic().emit_diagnostic(&diag);
470+
for mut diag in errors_buffer.drain(..) {
471+
self.tcx().sess.diagnostic().emit_diagnostic(&mut diag);
472472
}
473473
}
474474
}

src/tools/clippy/src/driver.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ fn report_clippy_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
178178
// a .span_bug or .bug call has already printed what
179179
// it wants to print.
180180
if !info.payload().is::<rustc_errors::ExplicitBug>() {
181-
let d = rustc_errors::Diagnostic::new(rustc_errors::Level::Bug, "unexpected panic");
182-
handler.emit_diagnostic(&d);
181+
let mut d = rustc_errors::Diagnostic::new(rustc_errors::Level::Bug, "unexpected panic");
182+
handler.emit_diagnostic(&mut d);
183183
}
184184

185185
let version_info = rustc_tools_util::get_version_info!();

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,10 @@ impl ParseSess {
225225
// Methods that should be restricted within the parse module.
226226
impl ParseSess {
227227
pub(super) fn emit_diagnostics(&self, diagnostics: Vec<Diagnostic>) {
228-
for diagnostic in diagnostics {
229-
self.parse_sess.span_diagnostic.emit_diagnostic(&diagnostic);
228+
for mut diagnostic in diagnostics {
229+
self.parse_sess
230+
.span_diagnostic
231+
.emit_diagnostic(&mut diagnostic);
230232
}
231233
}
232234

0 commit comments

Comments
 (0)