Skip to content

Commit e97103f

Browse files
authored
Rollup merge of rust-lang#125308 - lcnr:search-graph-5, r=compiler-errors
track cycle participants per root The search graph may have multiple roots, e.g. in ``` A :- B B :- A, C C :- D D :- C ``` we first encounter the `A -> B -> A` cycle which causes `A` to be a root. We then later encounter the `C -> D -> C` cycle as a nested goal of `B`. This cycle is completely separate and `C` will get moved to the global cache. This previously caused us to use `[B, D]` as the `cycle_participants` for `C` and `[]` for `A`. split off from rust-lang#125167 as I would like to merge this change separately and will rebase that PR on top of this one. There is no test for this issue and I don't quite know how to write one. It is probably worth it to generalize the search graph to enable us to write unit tests for it. r? `@compiler-errors`
2 parents 4b26045 + f99c9ff commit e97103f

File tree

5 files changed

+289
-156
lines changed

5 files changed

+289
-156
lines changed

compiler/rustc_middle/src/traits/solve/cache.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ pub struct EvaluationCache<'tcx> {
1414
map: Lock<FxHashMap<CanonicalInput<'tcx>, CacheEntry<'tcx>>>,
1515
}
1616

17-
#[derive(PartialEq, Eq)]
17+
#[derive(Debug, PartialEq, Eq)]
1818
pub struct CacheData<'tcx> {
1919
pub result: QueryResult<'tcx>,
2020
pub proof_tree: Option<&'tcx [inspect::GoalEvaluationStep<TyCtxt<'tcx>>]>,
21-
pub reached_depth: usize,
21+
pub additional_depth: usize,
2222
pub encountered_overflow: bool,
2323
}
2424

@@ -29,7 +29,7 @@ impl<'tcx> EvaluationCache<'tcx> {
2929
tcx: TyCtxt<'tcx>,
3030
key: CanonicalInput<'tcx>,
3131
proof_tree: Option<&'tcx [inspect::GoalEvaluationStep<TyCtxt<'tcx>>]>,
32-
reached_depth: usize,
32+
additional_depth: usize,
3333
encountered_overflow: bool,
3434
cycle_participants: FxHashSet<CanonicalInput<'tcx>>,
3535
dep_node: DepNodeIndex,
@@ -40,17 +40,17 @@ impl<'tcx> EvaluationCache<'tcx> {
4040
let data = WithDepNode::new(dep_node, QueryData { result, proof_tree });
4141
entry.cycle_participants.extend(cycle_participants);
4242
if encountered_overflow {
43-
entry.with_overflow.insert(reached_depth, data);
43+
entry.with_overflow.insert(additional_depth, data);
4444
} else {
45-
entry.success = Some(Success { data, reached_depth });
45+
entry.success = Some(Success { data, additional_depth });
4646
}
4747

4848
if cfg!(debug_assertions) {
4949
drop(map);
50-
if Some(CacheData { result, proof_tree, reached_depth, encountered_overflow })
51-
!= self.get(tcx, key, |_| false, Limit(reached_depth))
52-
{
53-
bug!("unable to retrieve inserted element from cache: {key:?}");
50+
let expected = CacheData { result, proof_tree, additional_depth, encountered_overflow };
51+
let actual = self.get(tcx, key, [], Limit(additional_depth));
52+
if !actual.as_ref().is_some_and(|actual| expected == *actual) {
53+
bug!("failed to lookup inserted element for {key:?}: {expected:?} != {actual:?}");
5454
}
5555
}
5656
}
@@ -63,23 +63,25 @@ impl<'tcx> EvaluationCache<'tcx> {
6363
&self,
6464
tcx: TyCtxt<'tcx>,
6565
key: CanonicalInput<'tcx>,
66-
cycle_participant_in_stack: impl FnOnce(&FxHashSet<CanonicalInput<'tcx>>) -> bool,
66+
stack_entries: impl IntoIterator<Item = CanonicalInput<'tcx>>,
6767
available_depth: Limit,
6868
) -> Option<CacheData<'tcx>> {
6969
let map = self.map.borrow();
7070
let entry = map.get(&key)?;
7171

72-
if cycle_participant_in_stack(&entry.cycle_participants) {
73-
return None;
72+
for stack_entry in stack_entries {
73+
if entry.cycle_participants.contains(&stack_entry) {
74+
return None;
75+
}
7476
}
7577

7678
if let Some(ref success) = entry.success {
77-
if available_depth.value_within_limit(success.reached_depth) {
79+
if available_depth.value_within_limit(success.additional_depth) {
7880
let QueryData { result, proof_tree } = success.data.get(tcx);
7981
return Some(CacheData {
8082
result,
8183
proof_tree,
82-
reached_depth: success.reached_depth,
84+
additional_depth: success.additional_depth,
8385
encountered_overflow: false,
8486
});
8587
}
@@ -90,7 +92,7 @@ impl<'tcx> EvaluationCache<'tcx> {
9092
CacheData {
9193
result,
9294
proof_tree,
93-
reached_depth: available_depth.0,
95+
additional_depth: available_depth.0,
9496
encountered_overflow: true,
9597
}
9698
})
@@ -99,7 +101,7 @@ impl<'tcx> EvaluationCache<'tcx> {
99101

100102
struct Success<'tcx> {
101103
data: WithDepNode<QueryData<'tcx>>,
102-
reached_depth: usize,
104+
additional_depth: usize,
103105
}
104106

105107
#[derive(Clone, Copy)]

0 commit comments

Comments
 (0)