Skip to content

Commit 51671cd

Browse files
committed
add test for coinduction in new solver
1 parent 646e667 commit 51671cd

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

compiler/rustc_trait_selection/src/solve/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,18 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
265265
// call `exists<U> <T as Trait>::Assoc == U` to enable better caching. This goal
266266
// could constrain `U` to `u32` which would cause this check to result in a
267267
// solver cycle.
268-
if cfg!(debug_assertions) && has_changed && !self.in_projection_eq_hack {
268+
if cfg!(debug_assertions)
269+
&& has_changed
270+
&& !self.in_projection_eq_hack
271+
&& !self.search_graph.in_cycle()
272+
{
269273
let mut orig_values = OriginalQueryValues::default();
270274
let canonical_goal = self.infcx.canonicalize_query(goal, &mut orig_values);
271275
let canonical_response =
272276
EvalCtxt::evaluate_canonical_goal(self.tcx(), self.search_graph, canonical_goal)?;
273-
assert!(canonical_response.value.var_values.is_identity());
277+
if !canonical_response.value.var_values.is_identity() {
278+
bug!("unstable result: {goal:?} {canonical_goal:?} {canonical_response:?}");
279+
}
274280
assert_eq!(certainty, canonical_response.value.certainty);
275281
}
276282

compiler/rustc_trait_selection/src/solve/search_graph/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ impl<'tcx> SearchGraph<'tcx> {
4242
&& !self.overflow_data.did_overflow()
4343
}
4444

45+
/// Whether we're currently in a cycle. This should only be used
46+
/// for debug assertions.
47+
pub(super) fn in_cycle(&self) -> bool {
48+
if let Some(stack_depth) = self.stack.last() {
49+
// Either the current goal on the stack is the root of a cycle...
50+
if self.stack[stack_depth].has_been_used {
51+
return true;
52+
}
53+
54+
// ...or it depends on a goal with a lower depth.
55+
let current_goal = self.stack[stack_depth].goal;
56+
let entry_index = self.provisional_cache.lookup_table[&current_goal];
57+
self.provisional_cache.entries[entry_index].depth != stack_depth
58+
} else {
59+
false
60+
}
61+
}
62+
4563
/// Tries putting the new goal on the stack, returning an error if it is already cached.
4664
///
4765
/// This correctly updates the provisional cache if there is a cycle.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// check-pass
2+
// revisions: old new
3+
//[new] compile-flags: -Ztrait-solver=next
4+
5+
// If we use canonical goals during trait solving we have to reevaluate
6+
// the root goal of a cycle until we hit a fixpoint.
7+
//
8+
// Here `main` has a goal `(?0, ?1): Trait` which is canonicalized to
9+
// `exists<^0, ^1> (^0, ^1): Trait`.
10+
//
11+
// - `exists<^0, ^1> (^0, ^1): Trait` -instantiate-> `(?0, ?1): Trait`
12+
// -`(?1, ?0): Trait` -canonicalize-> `exists<^0, ^1> (^0, ^1): Trait`
13+
// - COINDUCTIVE CYCLE OK (no constraints)
14+
// - `(): ConstrainToU32<?0>` -canonicalize-> `exists<^0> (): ConstrainToU32<^0>`
15+
// - OK (^0 = u32 -apply-> ?0 = u32)
16+
// - OK (?0 = u32 -canonicalize-> ^0 = u32)
17+
// - coinductive cycle with provisional result != final result, rerun
18+
//
19+
// - `exists<^0, ^1> (^0, ^1): Trait` -instantiate-> `(?0, ?1): Trait`
20+
// -`(?1, ?0): Trait` -canonicalize-> `exists<^0, ^1> (^0, ^1): Trait`
21+
// - COINDUCTIVE CYCLE OK (^0 = u32 -apply-> ?1 = u32)
22+
// - `(): ConstrainToU32<?0>` -canonicalize-> `exists<^0> (): ConstrainToU32<^0>`
23+
// - OK (^0 = u32 -apply-> ?1 = u32)
24+
// - OK (?0 = u32, ?1 = u32 -canonicalize-> ^0 = u32, ^1 = u32)
25+
// - coinductive cycle with provisional result != final result, rerun
26+
//
27+
// - `exists<^0, ^1> (^0, ^1): Trait` -instantiate-> `(?0, ?1): Trait`
28+
// -`(?1, ?0): Trait` -canonicalize-> `exists<^0, ^1> (^0, ^1): Trait`
29+
// - COINDUCTIVE CYCLE OK (^0 = u32, ^1 = u32 -apply-> ?1 = u32, ?0 = u32)
30+
// - `(): ConstrainToU32<?0>` -canonicalize-> `exists<^0> (): ConstrainToU32<^0>`
31+
// - OK (^0 = u32 -apply-> ?1 = u32)
32+
// - OK (?0 = u32, ?1 = u32 -canonicalize-> ^0 = u32, ^1 = u32)
33+
// - coinductive cycle with provisional result == final result, DONE
34+
#![feature(rustc_attrs)]
35+
#[rustc_coinductive]
36+
trait Trait {}
37+
38+
impl<T, U> Trait for (T, U)
39+
where
40+
(U, T): Trait,
41+
(): ConstrainToU32<T>,
42+
{}
43+
44+
trait ConstrainToU32<T> {}
45+
impl ConstrainToU32<u32> for () {}
46+
47+
fn impls_trait<T, U>()
48+
where
49+
(T, U): Trait,
50+
{}
51+
52+
fn main() {
53+
impls_trait::<_, _>();
54+
}

0 commit comments

Comments
 (0)