Skip to content

Commit 4719cb3

Browse files
committed
Ignore span references from diagnostics.
The diagnostics are replayed at the correct place anyway.
1 parent 40c8165 commit 4719cb3

File tree

3 files changed

+66
-51
lines changed

3 files changed

+66
-51
lines changed

compiler/rustc_errors/src/lib.rs

+51-47
Original file line numberDiff line numberDiff line change
@@ -473,10 +473,12 @@ pub enum StashKey {
473473
CallAssocMethod,
474474
}
475475

476-
fn default_track_diagnostic(_: &Diagnostic) {}
476+
fn default_track_diagnostic(d: &mut Diagnostic, f: &mut dyn FnMut(&mut Diagnostic)) {
477+
(*f)(d)
478+
}
477479

478-
pub static TRACK_DIAGNOSTICS: AtomicRef<fn(&Diagnostic)> =
479-
AtomicRef::new(&(default_track_diagnostic as fn(&_)));
480+
pub static TRACK_DIAGNOSTICS: AtomicRef<fn(&mut Diagnostic, &mut dyn FnMut(&mut Diagnostic))> =
481+
AtomicRef::new(&(default_track_diagnostic as _));
480482

481483
#[derive(Copy, Clone, Default)]
482484
pub struct HandlerFlags {
@@ -1290,67 +1292,69 @@ impl HandlerInner {
12901292
&& !diagnostic.is_force_warn()
12911293
{
12921294
if diagnostic.has_future_breakage() {
1293-
(*TRACK_DIAGNOSTICS)(diagnostic);
1295+
(*TRACK_DIAGNOSTICS)(diagnostic, &mut |_| {});
12941296
}
12951297
return None;
12961298
}
12971299

1298-
(*TRACK_DIAGNOSTICS)(diagnostic);
1299-
13001300
if matches!(diagnostic.level, Level::Expect(_) | Level::Allow) {
1301+
(*TRACK_DIAGNOSTICS)(diagnostic, &mut |_| {});
13011302
return None;
13021303
}
13031304

1304-
if let Some(ref code) = diagnostic.code {
1305-
self.emitted_diagnostic_codes.insert(code.clone());
1306-
}
1307-
1308-
let already_emitted = |this: &mut Self| {
1309-
let mut hasher = StableHasher::new();
1310-
diagnostic.hash(&mut hasher);
1311-
let diagnostic_hash = hasher.finish();
1312-
!this.emitted_diagnostics.insert(diagnostic_hash)
1313-
};
1305+
let mut guaranteed = None;
1306+
(*TRACK_DIAGNOSTICS)(diagnostic, &mut |diagnostic| {
1307+
if let Some(ref code) = diagnostic.code {
1308+
self.emitted_diagnostic_codes.insert(code.clone());
1309+
}
13141310

1315-
// Only emit the diagnostic if we've been asked to deduplicate or
1316-
// haven't already emitted an equivalent diagnostic.
1317-
if !(self.flags.deduplicate_diagnostics && already_emitted(self)) {
1318-
debug!(?diagnostic);
1319-
debug!(?self.emitted_diagnostics);
1320-
let already_emitted_sub = |sub: &mut SubDiagnostic| {
1321-
debug!(?sub);
1322-
if sub.level != Level::OnceNote {
1323-
return false;
1324-
}
1311+
let already_emitted = |this: &mut Self| {
13251312
let mut hasher = StableHasher::new();
1326-
sub.hash(&mut hasher);
1313+
diagnostic.hash(&mut hasher);
13271314
let diagnostic_hash = hasher.finish();
1328-
debug!(?diagnostic_hash);
1329-
!self.emitted_diagnostics.insert(diagnostic_hash)
1315+
!this.emitted_diagnostics.insert(diagnostic_hash)
13301316
};
13311317

1332-
diagnostic.children.drain_filter(already_emitted_sub).for_each(|_| {});
1333-
1334-
self.emitter.emit_diagnostic(diagnostic);
1335-
if diagnostic.is_error() {
1336-
self.deduplicated_err_count += 1;
1337-
} else if let Warning(_) = diagnostic.level {
1338-
self.deduplicated_warn_count += 1;
1318+
// Only emit the diagnostic if we've been asked to deduplicate or
1319+
// haven't already emitted an equivalent diagnostic.
1320+
if !(self.flags.deduplicate_diagnostics && already_emitted(self)) {
1321+
debug!(?diagnostic);
1322+
debug!(?self.emitted_diagnostics);
1323+
let already_emitted_sub = |sub: &mut SubDiagnostic| {
1324+
debug!(?sub);
1325+
if sub.level != Level::OnceNote {
1326+
return false;
1327+
}
1328+
let mut hasher = StableHasher::new();
1329+
sub.hash(&mut hasher);
1330+
let diagnostic_hash = hasher.finish();
1331+
debug!(?diagnostic_hash);
1332+
!self.emitted_diagnostics.insert(diagnostic_hash)
1333+
};
1334+
1335+
diagnostic.children.drain_filter(already_emitted_sub).for_each(|_| {});
1336+
1337+
self.emitter.emit_diagnostic(diagnostic);
1338+
if diagnostic.is_error() {
1339+
self.deduplicated_err_count += 1;
1340+
} else if let Warning(_) = diagnostic.level {
1341+
self.deduplicated_warn_count += 1;
1342+
}
13391343
}
1340-
}
1341-
if diagnostic.is_error() {
1342-
if matches!(diagnostic.level, Level::Error { lint: true }) {
1343-
self.bump_lint_err_count();
1344+
if diagnostic.is_error() {
1345+
if matches!(diagnostic.level, Level::Error { lint: true }) {
1346+
self.bump_lint_err_count();
1347+
} else {
1348+
self.bump_err_count();
1349+
}
1350+
1351+
guaranteed = Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
13441352
} else {
1345-
self.bump_err_count();
1353+
self.bump_warn_count();
13461354
}
1355+
});
13471356

1348-
Some(ErrorGuaranteed::unchecked_claim_error_was_emitted())
1349-
} else {
1350-
self.bump_warn_count();
1351-
1352-
None
1353-
}
1357+
guaranteed
13541358
}
13551359

13561360
fn emit_artifact_notification(&mut self, path: &Path, artifact_type: &str) {

compiler/rustc_interface/src/callbacks.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//! origin crate when the `TyCtxt` is not present in TLS.
1111
1212
use rustc_errors::{Diagnostic, TRACK_DIAGNOSTICS};
13+
use rustc_middle::dep_graph::TaskDepsRef;
1314
use rustc_middle::ty::tls;
1415
use std::fmt;
1516

@@ -26,14 +27,22 @@ fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
2627
/// This is a callback from `rustc_ast` as it cannot access the implicit state
2728
/// in `rustc_middle` otherwise. It is used when diagnostic messages are
2829
/// emitted and stores them in the current query, if there is one.
29-
fn track_diagnostic(diagnostic: &Diagnostic) {
30+
fn track_diagnostic(diagnostic: &mut Diagnostic, f: &mut dyn FnMut(&mut Diagnostic)) {
3031
tls::with_context_opt(|icx| {
3132
if let Some(icx) = icx {
3233
if let Some(diagnostics) = icx.diagnostics {
3334
let mut diagnostics = diagnostics.lock();
3435
diagnostics.extend(Some(diagnostic.clone()));
36+
std::mem::drop(diagnostics);
3537
}
38+
39+
// Diagnostics are tracked, we can ignore the dependency.
40+
let icx = tls::ImplicitCtxt { task_deps: TaskDepsRef::Ignore, ..icx.clone() };
41+
return tls::enter_context(&icx, move |_| (*f)(diagnostic));
3642
}
43+
44+
// In any other case, invoke diagnostics anyway.
45+
(*f)(diagnostic);
3746
})
3847
}
3948

@@ -55,5 +64,5 @@ fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) ->
5564
pub fn setup_callbacks() {
5665
rustc_span::SPAN_TRACK.swap(&(track_span_parent as fn(_)));
5766
rustc_hir::def_id::DEF_ID_DEBUG.swap(&(def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
58-
TRACK_DIAGNOSTICS.swap(&(track_diagnostic as fn(&_)));
67+
TRACK_DIAGNOSTICS.swap(&(track_diagnostic as _));
5968
}

compiler/rustc_query_system/src/dep_graph/graph.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ impl<K: DepKind> DepGraph<K> {
634634
if dep_node_debug.borrow().contains_key(&dep_node) {
635635
return;
636636
}
637-
let debug_str = debug_str_gen();
637+
let debug_str = self.with_ignore(debug_str_gen);
638638
dep_node_debug.borrow_mut().insert(dep_node, debug_str);
639639
}
640640

@@ -829,7 +829,9 @@ impl<K: DepKind> DepGraph<K> {
829829
);
830830

831831
if !side_effects.is_empty() {
832-
self.emit_side_effects(qcx, data, dep_node_index, side_effects);
832+
self.with_query_deserialization(|| {
833+
self.emit_side_effects(qcx, data, dep_node_index, side_effects)
834+
});
833835
}
834836

835837
// ... and finally storing a "Green" entry in the color map.

0 commit comments

Comments
 (0)