Skip to content

Commit 289c8f7

Browse files
committed
Consume diagnostics when emitting them.
1 parent d903a9d commit 289c8f7

File tree

12 files changed

+39
-34
lines changed

12 files changed

+39
-34
lines changed

src/librustc_codegen_ssa/back/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,7 @@ impl SharedEmitterMain {
16121612
if let Some(code) = diag.code {
16131613
d.code(code);
16141614
}
1615-
handler.emit_diagnostic(&d);
1615+
handler.emit_diagnostic(d);
16161616
}
16171617
Ok(SharedEmitterMessage::InlineAsmError(cookie, msg)) => {
16181618
sess.span_err(ExpnId::from_u32(cookie).expn_data().call_site, &msg)

src/librustc_driver/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
11731173
// it wants to print.
11741174
if !info.payload().is::<rustc_errors::ExplicitBug>() {
11751175
let d = rustc_errors::Diagnostic::new(rustc_errors::Level::Bug, "unexpected panic");
1176-
handler.emit_diagnostic(&d);
1176+
handler.emit_diagnostic(d);
11771177
}
11781178

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

src/librustc_errors/diagnostic_builder.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ impl<'a> DerefMut for DiagnosticBuilder<'a> {
9797
impl<'a> DiagnosticBuilder<'a> {
9898
/// Emit the diagnostic.
9999
pub fn emit(&mut self) {
100-
self.0.handler.emit_diagnostic(&self);
101-
self.cancel();
100+
let diag = std::mem::replace(&mut self.0.diagnostic, Diagnostic::new(Level::Cancelled, ""));
101+
self.0.handler.emit_diagnostic(diag);
102102
}
103103

104104
/// Emit the diagnostic unless `delay` is true,
@@ -179,8 +179,8 @@ impl<'a> DiagnosticBuilder<'a> {
179179
/// locally in whichever way makes the most sense.
180180
pub fn delay_as_bug(&mut self) {
181181
self.level = Level::Bug;
182-
self.0.handler.delay_as_bug(self.0.diagnostic.clone());
183-
self.cancel();
182+
let diag = std::mem::replace(&mut self.0.diagnostic, Diagnostic::new(Level::Cancelled, ""));
183+
self.0.handler.delay_as_bug(diag);
184184
}
185185

186186
/// Adds a span/label to be included in the resulting snippet.

src/librustc_errors/lib.rs

+21-17
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,12 @@ pub enum StashKey {
326326
ItemNoType,
327327
}
328328

329-
fn default_track_diagnostic(_: &Diagnostic) {}
329+
fn default_track_diagnostic(d: Diagnostic) -> Diagnostic {
330+
d
331+
}
330332

331-
pub static TRACK_DIAGNOSTICS: AtomicRef<fn(&Diagnostic)> =
332-
AtomicRef::new(&(default_track_diagnostic as fn(&_)));
333+
pub static TRACK_DIAGNOSTICS: AtomicRef<fn(Diagnostic) -> Diagnostic> =
334+
AtomicRef::new(&(default_track_diagnostic as fn(_) -> _));
333335

334336
#[derive(Copy, Clone, Default)]
335337
pub struct HandlerFlags {
@@ -359,8 +361,8 @@ impl Drop for HandlerInner {
359361
if !self.has_errors() {
360362
let bugs = std::mem::replace(&mut self.delayed_span_bugs, Vec::new());
361363
let has_bugs = !bugs.is_empty();
362-
for bug in bugs {
363-
self.emit_diagnostic(&bug);
364+
for bug in bugs.into_iter() {
365+
self.emit_diagnostic(bug);
364366
}
365367
if has_bugs {
366368
panic!("no errors encountered even though `delay_span_bug` issued");
@@ -691,13 +693,14 @@ impl Handler {
691693
self.inner.borrow_mut().force_print_diagnostic(db)
692694
}
693695

694-
pub fn emit_diagnostic(&self, diagnostic: &Diagnostic) {
696+
pub fn emit_diagnostic(&self, diagnostic: Diagnostic) {
695697
self.inner.borrow_mut().emit_diagnostic(diagnostic)
696698
}
697699

698700
fn emit_diag_at_span(&self, mut diag: Diagnostic, sp: impl Into<MultiSpan>) {
699701
let mut inner = self.inner.borrow_mut();
700-
inner.emit_diagnostic(diag.set_span(sp));
702+
diag.set_span(sp);
703+
inner.emit_diagnostic(diag);
701704
}
702705

703706
pub fn emit_artifact_notification(&self, path: &Path, artifact_type: &str) {
@@ -721,10 +724,10 @@ impl HandlerInner {
721724
/// Emit all stashed diagnostics.
722725
fn emit_stashed_diagnostics(&mut self) {
723726
let diags = self.stashed_diagnostics.drain(..).map(|x| x.1).collect::<Vec<_>>();
724-
diags.iter().for_each(|diag| self.emit_diagnostic(diag));
727+
diags.into_iter().for_each(|diag| self.emit_diagnostic(diag));
725728
}
726729

727-
fn emit_diagnostic(&mut self, diagnostic: &Diagnostic) {
730+
fn emit_diagnostic(&mut self, diagnostic: Diagnostic) {
728731
if diagnostic.cancelled() {
729732
return;
730733
}
@@ -733,7 +736,7 @@ impl HandlerInner {
733736
return;
734737
}
735738

736-
(*TRACK_DIAGNOSTICS)(diagnostic);
739+
let diagnostic = (*TRACK_DIAGNOSTICS)(diagnostic);
737740

738741
if let Some(ref code) = diagnostic.code {
739742
self.emitted_diagnostic_codes.insert(code.clone());
@@ -750,7 +753,7 @@ impl HandlerInner {
750753
// Only emit the diagnostic if we've been asked to deduplicate and
751754
// haven't already emitted an equivalent diagnostic.
752755
if !(self.flags.deduplicate_diagnostics && already_emitted(self)) {
753-
self.emitter.emit_diagnostic(diagnostic);
756+
self.emitter.emit_diagnostic(&diagnostic);
754757
if diagnostic.is_error() {
755758
self.deduplicated_err_count += 1;
756759
} else if diagnostic.level == Warning {
@@ -789,7 +792,7 @@ impl HandlerInner {
789792

790793
match (errors.len(), warnings.len()) {
791794
(0, 0) => return,
792-
(0, _) => self.emit_diagnostic(&Diagnostic::new(Level::Warning, &warnings)),
795+
(0, _) => self.emit_diagnostic(Diagnostic::new(Level::Warning, &warnings)),
793796
(_, 0) => {
794797
let _ = self.fatal(&errors);
795798
}
@@ -865,7 +868,8 @@ impl HandlerInner {
865868
}
866869

867870
fn emit_diag_at_span(&mut self, mut diag: Diagnostic, sp: impl Into<MultiSpan>) {
868-
self.emit_diagnostic(diag.set_span(sp));
871+
diag.set_span(sp);
872+
self.emit_diagnostic(diag);
869873
}
870874

871875
fn delay_span_bug(&mut self, sp: impl Into<MultiSpan>, msg: &str) {
@@ -882,7 +886,7 @@ impl HandlerInner {
882886
}
883887

884888
fn failure(&mut self, msg: &str) {
885-
self.emit_diagnostic(&Diagnostic::new(FailureNote, msg));
889+
self.emit_diagnostic(Diagnostic::new(FailureNote, msg));
886890
}
887891

888892
fn fatal(&mut self, msg: &str) -> FatalError {
@@ -899,17 +903,17 @@ impl HandlerInner {
899903
if self.treat_err_as_bug() {
900904
self.bug(msg);
901905
}
902-
self.emit_diagnostic(&Diagnostic::new(level, msg));
906+
self.emit_diagnostic(Diagnostic::new(level, msg));
903907
}
904908

905909
fn bug(&mut self, msg: &str) -> ! {
906-
self.emit_diagnostic(&Diagnostic::new(Bug, msg));
910+
self.emit_diagnostic(Diagnostic::new(Bug, msg));
907911
panic!(ExplicitBug);
908912
}
909913

910914
fn delay_as_bug(&mut self, diagnostic: Diagnostic) {
911915
if self.flags.report_delayed_bugs {
912-
self.emit_diagnostic(&diagnostic);
916+
self.emit_diagnostic(diagnostic.clone());
913917
}
914918
self.delayed_span_bugs.push(diagnostic);
915919
}

src/librustc_expand/proc_macro_server.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ impl server::Diagnostic for Rustc<'_> {
634634
diag.sub(level.to_internal(), msg, MultiSpan::from_spans(spans), None);
635635
}
636636
fn emit(&mut self, diag: Self::Diagnostic) {
637-
self.sess.span_diagnostic.emit_diagnostic(&diag);
637+
self.sess.span_diagnostic.emit_diagnostic(diag);
638638
}
639639
}
640640

src/librustc_interface/callbacks.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@ fn span_debug(span: rustc_span::Span, f: &mut fmt::Formatter<'_>) -> fmt::Result
2828
/// This is a callback from librustc_ast as it cannot access the implicit state
2929
/// in librustc_middle otherwise. It is used to when diagnostic messages are
3030
/// emitted and stores them in the current query, if there is one.
31-
fn track_diagnostic(diagnostic: &Diagnostic) {
31+
fn track_diagnostic(diagnostic: Diagnostic) -> Diagnostic {
3232
tls::with_context_opt(|icx| {
3333
if let Some(icx) = icx {
3434
if let Some(ref diagnostics) = icx.diagnostics {
3535
let mut diagnostics = diagnostics.lock();
3636
diagnostics.extend(Some(diagnostic.clone()));
3737
}
3838
}
39-
})
39+
});
40+
diagnostic
4041
}
4142

4243
/// This is a callback from librustc_hir as it cannot access the implicit state
@@ -57,5 +58,5 @@ fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) ->
5758
pub fn setup_callbacks() {
5859
rustc_span::SPAN_DEBUG.swap(&(span_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
5960
rustc_hir::def_id::DEF_ID_DEBUG.swap(&(def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
60-
TRACK_DIAGNOSTICS.swap(&(track_diagnostic as fn(&_)));
61+
TRACK_DIAGNOSTICS.swap(&(track_diagnostic as fn(_) -> _));
6162
}

src/librustc_mir/borrow_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ fn do_mir_borrowck<'a, 'tcx>(
441441
mbcx.errors_buffer.sort_by_key(|diag| diag.sort_span);
442442

443443
for diag in mbcx.errors_buffer.drain(..) {
444-
mbcx.infcx.tcx.sess.diagnostic().emit_diagnostic(&diag);
444+
mbcx.infcx.tcx.sess.diagnostic().emit_diagnostic(diag);
445445
}
446446
}
447447

src/librustc_parse/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ macro_rules! panictry_buffer {
4242
Ok(e) => e,
4343
Err(errs) => {
4444
for e in errs {
45-
$handler.emit_diagnostic(&e);
45+
$handler.emit_diagnostic(e);
4646
}
4747
FatalError.raise()
4848
}
@@ -176,7 +176,7 @@ fn file_to_source_file(sess: &ParseSess, path: &Path, spanopt: Option<Span>) ->
176176
match try_file_to_source_file(sess, path, spanopt) {
177177
Ok(source_file) => source_file,
178178
Err(d) => {
179-
sess.span_diagnostic.emit_diagnostic(&d);
179+
sess.span_diagnostic.emit_diagnostic(d);
180180
FatalError.raise();
181181
}
182182
}

src/librustc_query_system/dep_graph/graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ impl<K: DepKind> DepGraph<K> {
763763
let handle = tcx.diagnostic();
764764

765765
for diagnostic in diagnostics {
766-
handle.emit_diagnostic(&diagnostic);
766+
handle.emit_diagnostic(diagnostic);
767767
}
768768

769769
// Mark the node as green now that diagnostics are emitted

src/librustc_typeck/check/writeback.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
403403
if !errors_buffer.is_empty() {
404404
errors_buffer.sort_by_key(|diag| diag.span.primary_span());
405405
for diag in errors_buffer.drain(..) {
406-
self.tcx().sess.diagnostic().emit_diagnostic(&diag);
406+
self.tcx().sess.diagnostic().emit_diagnostic(diag);
407407
}
408408
}
409409
}

src/test/ui/consts/miri_unleashed/mutable_const2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error: internal compiler error: mutable allocation in constant
1414
LL | const MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616

17-
thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:366:17
17+
thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:368:17
1818
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1919

2020
error: internal compiler error: unexpected panic

src/tools/clippy/src/driver.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ fn report_clippy_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
258258
// it wants to print.
259259
if !info.payload().is::<rustc_errors::ExplicitBug>() {
260260
let d = rustc_errors::Diagnostic::new(rustc_errors::Level::Bug, "unexpected panic");
261-
handler.emit_diagnostic(&d);
261+
handler.emit_diagnostic(d);
262262
}
263263

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

0 commit comments

Comments
 (0)