Skip to content

Commit f0bc76a

Browse files
committed
Auto merge of rust-lang#91742 - cjgillot:force-backtrace, r=estebank
Print a backtrace when query forcing fails. The aim of this PR is to help debugging incremental compilation bugs where query forcing panics. For instance: rust-lang#90682 rust-lang#90697 rust-lang#90715 rust-lang#90739 rust-lang#91401 These bugs happen when the dep-graph attempts to force a dep-node whose fingerprint does not correspond to an actual DefPathHash. PR rust-lang#91741 attempts to hide this bug. I still don't know how to reproduce these bugs, so I sadly could not test this debugging device.
2 parents c5c7d2b + 870dd16 commit f0bc76a

File tree

1 file changed

+45
-11
lines changed
  • compiler/rustc_query_system/src/dep_graph

1 file changed

+45
-11
lines changed

compiler/rustc_query_system/src/dep_graph/graph.rs

+45-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_data_structures::sharded::{self, Sharded};
66
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
77
use rustc_data_structures::steal::Steal;
88
use rustc_data_structures::sync::{AtomicU32, AtomicU64, Lock, Lrc, Ordering};
9+
use rustc_data_structures::OnDrop;
910
use rustc_index::vec::IndexVec;
1011
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
1112
use smallvec::{smallvec, SmallVec};
@@ -671,17 +672,24 @@ impl<K: DepKind> DepGraph<K> {
671672
let prev_index = data.previous.node_to_index_opt(dep_node)?;
672673

673674
match data.colors.get(prev_index) {
674-
Some(DepNodeColor::Green(dep_node_index)) => Some((prev_index, dep_node_index)),
675-
Some(DepNodeColor::Red) => None,
676-
None => {
677-
// This DepNode and the corresponding query invocation existed
678-
// in the previous compilation session too, so we can try to
679-
// mark it as green by recursively marking all of its
680-
// dependencies green.
681-
self.try_mark_previous_green(qcx, data, prev_index, &dep_node)
682-
.map(|dep_node_index| (prev_index, dep_node_index))
683-
}
675+
Some(DepNodeColor::Green(dep_node_index)) => return Some((prev_index, dep_node_index)),
676+
Some(DepNodeColor::Red) => return None,
677+
None => {}
684678
}
679+
680+
let backtrace = backtrace_printer(qcx.dep_context().sess(), data, prev_index);
681+
682+
// This DepNode and the corresponding query invocation existed
683+
// in the previous compilation session too, so we can try to
684+
// mark it as green by recursively marking all of its
685+
// dependencies green.
686+
let ret = self
687+
.try_mark_previous_green(qcx, data, prev_index, &dep_node)
688+
.map(|dep_node_index| (prev_index, dep_node_index));
689+
690+
// We succeeded, no backtrace.
691+
backtrace.disable();
692+
return ret;
685693
}
686694

687695
#[instrument(skip(self, qcx, data, parent_dep_node_index), level = "debug")]
@@ -794,7 +802,10 @@ impl<K: DepKind> DepGraph<K> {
794802
let prev_deps = data.previous.edge_targets_from(prev_dep_node_index);
795803

796804
for &dep_dep_node_index in prev_deps {
797-
self.try_mark_parent_green(qcx, data, dep_dep_node_index, dep_node)?
805+
let backtrace = backtrace_printer(qcx.dep_context().sess(), data, dep_dep_node_index);
806+
let success = self.try_mark_parent_green(qcx, data, dep_dep_node_index, dep_node);
807+
backtrace.disable();
808+
success?;
798809
}
799810

800811
// If we got here without hitting a `return` that means that all
@@ -1364,3 +1375,26 @@ impl DepNodeColorMap {
13641375
)
13651376
}
13661377
}
1378+
1379+
fn backtrace_printer<'a, K: DepKind>(
1380+
sess: &'a rustc_session::Session,
1381+
graph: &'a DepGraphData<K>,
1382+
node: SerializedDepNodeIndex,
1383+
) -> OnDrop<impl Fn() + 'a> {
1384+
OnDrop(
1385+
#[inline(never)]
1386+
#[cold]
1387+
move || {
1388+
let node = graph.previous.index_to_node(node);
1389+
// Do not try to rely on DepNode's Debug implementation, since it may panic.
1390+
let diag = rustc_errors::Diagnostic::new(
1391+
rustc_errors::Level::FailureNote,
1392+
&format!(
1393+
"encountered while trying to mark dependency green: {:?}({})",
1394+
node.kind, node.hash
1395+
),
1396+
);
1397+
sess.diagnostic().force_print_diagnostic(diag);
1398+
},
1399+
)
1400+
}

0 commit comments

Comments
 (0)