Skip to content

Commit 90e3ba3

Browse files
Rollup merge of #163185 - sjwang05:depth-budget-fix, r=lcnr
properly decrement available_depth on cycles and provisional cache hits cc https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/overflow.20fcw.20depends.20on.20cached.20results.20with.20cycles/with/625345435 With this change, the fuzzer doesn't seem to catch any mismatches or errors. If we decrement only on a cycle and not on a provisional cache hit, the fuzzer does report some mismatches. I decided to directly pass `available_depth` instead of using the `lower_available_depth` arg since `allowed_depth_for_nested` already computes `available_depth` based on `lower_available_depth`. r? lcnr
2 parents e2026da + e9824e4 commit 90e3ba3

5 files changed

Lines changed: 118 additions & 17 deletions

File tree

‎compiler/rustc_type_ir/src/search_graph/mod.rs‎

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,8 @@ pub struct SearchGraph<D: Delegate<Cx = X>, X: Cx = <D as Delegate>::Cx> {
628628
/// don't need to track the nested goals used while computing a provisional
629629
/// cache entry.
630630
enum UpdateParentGoalCtxt<'a, X: Cx> {
631-
Ordinary { nested_goals: &'a NestedGoals<X>, min_reachable_available_depth: AvailableDepth },
632-
CycleOnStack(X::Input),
631+
Ordinary { nested_goals: &'a NestedGoals<X> },
632+
CycleOnStack { head: X::Input },
633633
ProvisionalCacheHit,
634634
}
635635

@@ -653,9 +653,12 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
653653
heads: impl Iterator<Item = (StackDepth, CycleHead)>,
654654
encountered_overflow: bool,
655655
context: UpdateParentGoalCtxt<'_, X>,
656+
min_reachable_available_depth: AvailableDepth,
656657
) {
657658
if let Some((parent_index, parent)) = stack.last_mut_with_index() {
658659
parent.encountered_overflow |= encountered_overflow;
660+
parent.min_reached_available_depth =
661+
parent.min_reached_available_depth.min(min_reachable_available_depth);
659662

660663
for (head_index, head) in heads {
661664
if let Some(candidate_usages) = &mut parent.candidate_usages {
@@ -679,13 +682,11 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
679682
}
680683
}
681684
let parent_depends_on_cycle = match context {
682-
UpdateParentGoalCtxt::Ordinary { nested_goals, min_reachable_available_depth } => {
683-
parent.min_reached_available_depth =
684-
parent.min_reached_available_depth.min(min_reachable_available_depth);
685+
UpdateParentGoalCtxt::Ordinary { nested_goals } => {
685686
parent.nested_goals.extend_from_child(step_kind_from_parent, nested_goals);
686687
!nested_goals.is_empty()
687688
}
688-
UpdateParentGoalCtxt::CycleOnStack(head) => {
689+
UpdateParentGoalCtxt::CycleOnStack { head } => {
689690
// We lookup provisional cache entries before detecting cycles.
690691
// We therefore can't use a global cache entry if it contains a cycle
691692
// whose head is in the provisional cache.
@@ -811,7 +812,9 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
811812
// - A
812813
// - BA cycle
813814
// - CB :x:
814-
if let Some(result) = self.lookup_provisional_cache(input, step_kind_from_parent) {
815+
if let Some(result) =
816+
self.lookup_provisional_cache(input, step_kind_from_parent, available_depth)
817+
{
815818
return result;
816819
}
817820

@@ -839,7 +842,9 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
839842
// avoid iterating over the stack in case a goal has already been computed.
840843
// This may not have an actual performance impact and we could reorder them
841844
// as it may reduce the number of `nested_goals` we need to track.
842-
if let Some(result) = self.check_cycle_on_stack(cx, input, step_kind_from_parent) {
845+
if let Some(result) =
846+
self.check_cycle_on_stack(cx, input, step_kind_from_parent, available_depth)
847+
{
843848
debug_assert!(validate_cache.is_none(), "global cache and cycle on stack: {input:?}");
844849
return result;
845850
}
@@ -874,10 +879,8 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
874879
step_kind_from_parent,
875880
evaluation_result.heads.iter(),
876881
evaluation_result.encountered_overflow,
877-
UpdateParentGoalCtxt::Ordinary {
878-
nested_goals: &evaluation_result.nested_goals,
879-
min_reachable_available_depth: available_depth - evaluation_result.required_depth,
880-
},
882+
UpdateParentGoalCtxt::Ordinary { nested_goals: &evaluation_result.nested_goals },
883+
available_depth - evaluation_result.required_depth,
881884
);
882885
let result = evaluation_result.result;
883886

@@ -1119,6 +1122,7 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D, X> {
11191122
&mut self,
11201123
input: X::Input,
11211124
step_kind_from_parent: PathKind,
1125+
available_depth: AvailableDepth,
11221126
) -> Option<X::Result> {
11231127
if !D::ENABLE_PROVISIONAL_CACHE {
11241128
return None;
@@ -1157,6 +1161,7 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D, X> {
11571161
heads.iter(),
11581162
encountered_overflow,
11591163
UpdateParentGoalCtxt::ProvisionalCacheHit,
1164+
available_depth,
11601165
);
11611166
debug!(?head_index, ?path_from_head, "provisional cache hit");
11621167
return Some(result);
@@ -1278,10 +1283,8 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D, X> {
12781283
step_kind_from_parent,
12791284
heads,
12801285
encountered_overflow,
1281-
UpdateParentGoalCtxt::Ordinary {
1282-
nested_goals,
1283-
min_reachable_available_depth: available_depth - required_depth,
1284-
},
1286+
UpdateParentGoalCtxt::Ordinary { nested_goals },
1287+
available_depth - required_depth,
12851288
);
12861289

12871290
debug!(?required_depth, "global cache hit");
@@ -1294,6 +1297,7 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D, X> {
12941297
cx: X,
12951298
input: X::Input,
12961299
step_kind_from_parent: PathKind,
1300+
available_depth: AvailableDepth,
12971301
) -> Option<X::Result> {
12981302
let head_index = self.stack.find(input)?;
12991303
// We have a nested goal which directly relies on a goal deeper in the stack.
@@ -1312,7 +1316,8 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D, X> {
13121316
step_kind_from_parent,
13131317
iter::once((head_index, head)),
13141318
false,
1315-
UpdateParentGoalCtxt::CycleOnStack(input),
1319+
UpdateParentGoalCtxt::CycleOnStack { head: input },
1320+
available_depth,
13161321
);
13171322

13181323
// Return the provisional result or, if we're in the first iteration,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ compile-flags: -Znext-solver
2+
//@ check-pass
3+
4+
// We must properly decrement available_depth when encountering cycles.
5+
// Proving `Foo: Send` results in a cycle:
6+
// - Foo: Send
7+
// ->PhantomData<Foo>: Send
8+
// ->Foo: Send
9+
// and if we cache this result with too small of a required depth, the second
10+
// `require` call will succeed with the cached result where it would fail if
11+
// the goal were freshly evaluated.
12+
13+
#![recursion_limit = "6"]
14+
15+
use std::marker::PhantomData;
16+
17+
pub struct Foo(PhantomData<Foo>);
18+
pub struct W<T>(T);
19+
20+
fn require<T: Send>() {}
21+
22+
fn main() {
23+
require::<Foo>();
24+
require::<W<W<W<W<W<Foo>>>>>>();
25+
//~^ WARN overflow evaluating the requirement `W<W<W<W<W<Foo>>>>>: Send`
26+
//~| WARN this was previously accepted by the compiler but is being phased out
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
warning: overflow evaluating the requirement `W<W<W<W<W<Foo>>>>>: Send`
2+
--> $DIR/cycle-required-depth.rs:24:5
3+
|
4+
LL | require::<W<W<W<W<W<Foo>>>>>>();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: which requires `W<W<W<W<Foo>>>>: Send`
8+
= note: which requires `W<W<W<Foo>>>: Send`
9+
= note: which requires `W<W<Foo>>: Send`
10+
= note: which requires `W<Foo>: Send`
11+
= note: which requires `Foo: Send`
12+
= note: and so on...
13+
= help: consider adding a manual `impl` of auto traits like `Send` for intermediate types, if auto traits are involved
14+
= help: or consider increasing the recursion limit by adding a `#![recursion_limit = "12"]` attribute to your crate (`cycle_required_depth`)
15+
= note: this lint is attached to the whole crate and can't be disabled on a per-function basis
16+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
17+
= note: for more information, see issue #159228 <https://github.com/rust-lang/rust/issues/159228>
18+
= note: `#[warn(recursion_depth_exceeding_limit)]` (part of `#[warn(future_incompatible)]`) on by default
19+
20+
warning: 1 warning emitted
21+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ compile-flags: -Znext-solver
2+
//@ check-pass
3+
4+
#![recursion_limit = "6"]
5+
use std::marker::PhantomData;
6+
7+
// We need to decrement available_depth on provisional cache hits.
8+
// e.g., in the following example:
9+
// A: Send, avail = 6
10+
// -> PhantomData<A>: Send, avail = 5
11+
// -> A: Send, avail = 4
12+
// -> B: Send, avail = 5
13+
// -> C: Send, avail = 4
14+
// -> PhantomData<A>: Send, provisional cache hit, avail = 4 if we don't decrement
15+
struct A(PhantomData<A>, B);
16+
struct B(C);
17+
struct C(PhantomData<A>);
18+
struct W<T>(T);
19+
20+
fn require<T: Send>() {}
21+
22+
fn main() {
23+
require::<A>();
24+
require::<W<W<W<W<A>>>>>();
25+
//~^ WARN overflow evaluating the requirement `W<W<W<W<A>>>>: Send`
26+
//~| WARN this was previously accepted by the compiler but is being phased out
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
warning: overflow evaluating the requirement `W<W<W<W<A>>>>: Send`
2+
--> $DIR/provisional-cache-required-depth.rs:24:5
3+
|
4+
LL | require::<W<W<W<W<A>>>>>();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: which requires `W<W<W<A>>>: Send`
8+
= note: which requires `W<W<A>>: Send`
9+
= note: which requires `W<A>: Send`
10+
= note: which requires `A: Send`
11+
= note: which requires `B: Send`
12+
= note: and so on...
13+
= help: consider adding a manual `impl` of auto traits like `Send` for intermediate types, if auto traits are involved
14+
= help: or consider increasing the recursion limit by adding a `#![recursion_limit = "12"]` attribute to your crate (`provisional_cache_required_depth`)
15+
= note: this lint is attached to the whole crate and can't be disabled on a per-function basis
16+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
17+
= note: for more information, see issue #159228 <https://github.com/rust-lang/rust/issues/159228>
18+
= note: `#[warn(recursion_depth_exceeding_limit)]` (part of `#[warn(future_incompatible)]`) on by default
19+
20+
warning: 1 warning emitted
21+

0 commit comments

Comments
 (0)