Skip to content

Commit 9d3cc46

Browse files
authored
Rollup merge of #161988 - Dnreikronos:trait_solver/reflexive_region_constraints, r=BoxyUwU
trait solver: Handle reflexive region constraints Fixes rust-lang/project-assumptions-on-binders#19 A reflexive placeholder constraint like `'a: 'a` can show up after we compute transitive region constraints. We then try to pull it out of the current universe by looking for lower-universe candidates. With none to choose from it becomes false, even though `'a: 'a` was true the whole time. I first had this buried in the binder region work, which made a green test pretty meaningless because too much else was changing around it. Split out on its own, the fix is just to accept equal regions before doing the universe rewrite. I think that is the sensible place to stop: it handles the direct case and the reflexive edges made by the transitive pass. I added a regression for the direct case. cc/ @BoxyUwU o/
2 parents a92214c + b532a13 commit 9d3cc46

5 files changed

Lines changed: 122 additions & 6 deletions

File tree

‎compiler/rustc_type_ir/src/region_constraint.rs‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@ impl<I: Interner, S: Clone + std::fmt::Debug + Eq + std::hash::Hash> LeafRegionC
231231
/// An OR of AND of LEAF constraints. Always in "canonical form" meaning:
232232
/// - No two ANDs are equivalent
233233
/// - All ANDs are in canonical form
234+
/// - If any AND is empty, i.e. trivially true, it is the only AND
235+
///
236+
/// FIXME(-Zassumptions-on-binders): We should consider a more general canonical form which also
237+
/// drops any AND that is a superset of another AND. Proving the superset requires strictly more
238+
/// than proving the subset, so it can never be the candidate which makes the OR hold. E.g.
239+
/// `OR(AND('a: 'b), AND('a: 'b, 'b: 'c))` really ought to just be `OR(AND('a: 'b))`. Only keeping
240+
/// an empty AND is the degenerate case of that rule.
234241
pub struct Or<I: Interner, S: Clone + std::fmt::Debug = ()>(pub Box<[And<I, S>]>);
235242
impl<I: Interner> Or<I> {
236243
pub fn with_spans<S: Clone + std::fmt::Debug + Eq + std::hash::Hash>(
@@ -270,6 +277,17 @@ impl<I: Interner, S: Clone + std::hash::Hash + std::fmt::Debug + Eq> Or<I, S> {
270277
let mut new_ands: Vec<And<I, S>> = Vec::new();
271278

272279
for and in ands {
280+
// An empty AND is trivially true, which makes the whole OR true no matter what the
281+
// other candidates are. `And::new` discards leaf constraints which are trivially
282+
// true, so this is how e.g. a reflexive `'a: 'a` candidate discharges an OR.
283+
if and.0.is_empty() {
284+
return Self::new_true();
285+
}
286+
287+
// FIXME(-Zassumptions-on-binders): We only discard an AND which is equivalent to one
288+
// we already have. More generally we should discard any AND which is a superset of
289+
// another, as it requires strictly more to hold. E.g. the second AND in
290+
// `OR(AND('a: 'b), AND('a: 'b, 'b: 'c))` is never the one which makes the OR true.
273291
if new_ands.iter().all(|c| !c.is_and_equivalent_to(&and)) {
274292
new_ands.push(and)
275293
}
@@ -283,7 +301,7 @@ impl<I: Interner, S: Clone + std::hash::Hash + std::fmt::Debug + Eq> Or<I, S> {
283301
}
284302

285303
pub fn new_leaf(l: LeafRegionConstraint<I, S>) -> Self {
286-
Or(Box::new([And(Box::new([l]))]))
304+
Or::new([And::new([l])])
287305
}
288306

289307
pub fn build_and(a: Or<I, S>, b: Or<I, S>) -> Self {
@@ -315,6 +333,7 @@ impl<I: Interner, S: Clone + std::hash::Hash + std::fmt::Debug + Eq> Or<I, S> {
315333
#[cfg_attr(feature = "nightly", derive(StableHash_NoContext))]
316334
/// An AND of leaf constraints. Always in "canonical form", meaning:
317335
/// - No leaf constraints are present twice in this AND
336+
/// - No leaf constraint is trivially true, i.e. a reflexive `'a: 'a`
318337
pub struct And<I: Interner, S: Clone + std::fmt::Debug = ()>(pub Box<[LeafRegionConstraint<I, S>]>);
319338
impl<I: Interner> And<I> {
320339
pub fn with_spans<S: Clone + std::fmt::Debug + Eq + std::hash::Hash>(
@@ -334,6 +353,15 @@ impl<I: Interner, S: Clone + std::hash::Hash + std::fmt::Debug + Eq> And<I, S> {
334353
And(i
335354
.into_iter()
336355
.filter(|leaf| {
356+
// Outlives is reflexive so a `'a: 'a` leaf is always true and carries no
357+
// information. Dropping it here keeps the rest of the code from having to special
358+
// case it, and is what lets an OR with a reflexive candidate be recognized as true.
359+
if let LeafRegionConstraint::RegionOutlives(r1, r2, _) = leaf
360+
&& r1 == r2
361+
{
362+
return false;
363+
}
364+
337365
if seen.contains(&leaf.clone().without_span()) {
338366
false
339367
} else {
@@ -479,7 +507,7 @@ impl<I: Interner, S: Clone + std::fmt::Debug + Eq + std::hash::Hash> RegionConst
479507
}
480508

481509
pub fn new_leaf(l: LeafRegionConstraint<I, S>) -> Self {
482-
RegionConstraint { and_constraint: And(Box::new([l])), or_constraint: Or::new_true() }
510+
RegionConstraint { and_constraint: And::new([l]), or_constraint: Or::new_true() }
483511
}
484512
}
485513

@@ -725,6 +753,14 @@ fn pull_region_outlives_constraints_out_of_universe<
725753
}
726754
};
727755

756+
// The constraint may already be entailed by the assumptions of the binder we are
757+
// leaving, e.g. `for<'a, 'b> where 'b: 'a { 'b: 'a }`. There is nothing to lift into
758+
// a smaller universe in that case, and looking for lower universe candidates would
759+
// wrongly result in `Or([])` whenever the placeholders have no lower universe bounds.
760+
if regions_outlived_by(region_1, assumptions).any(|r| r == region_2) {
761+
continue;
762+
}
763+
728764
let mut candidates = vec![];
729765

730766
for ub in regions_outlived_by(region_1, assumptions)
@@ -1056,7 +1092,6 @@ pub fn regions_outlived_by<I: Interner>(
10561092
r: Region<I>,
10571093
assumptions: &Assumptions<I>,
10581094
) -> impl Iterator<Item = Region<I>> {
1059-
// FIXME(-Zassumptions-on-binders): do we need to be adding the reflexive edge here?
10601095
assumptions.region_outlives.reachable_from(r).into_iter().chain([r])
10611096
}
10621097

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ check-pass
2+
//@ compile-flags: -Zassumptions-on-binders
3+
4+
#![feature(test_binder_constraints, non_lifetime_binders)]
5+
#![expect(incomplete_features)]
6+
7+
// Root type outlives constraints are destructured into an OR over every region the type is known
8+
// to outlive. The reflexive `'b: 'b` candidate makes this OR true even though the unrelated
9+
// `'a: 'b` candidate does not hold. Reflexive leaves are dropped when building an AND, which
10+
// leaves an empty, i.e. trivially true, AND as one of the candidates of the OR.
11+
core::test_binder_constraints! {
12+
impl<'a, 'b, T: 'a + 'b> {
13+
T: 'b
14+
}
15+
}
16+
17+
fn main() {}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//@ check-pass
2+
//@ compile-flags: -Zassumptions-on-binders
3+
4+
#![feature(test_binder_constraints, non_lifetime_binders)]
5+
#![expect(incomplete_features)]
6+
7+
// Regression test for rust-lang/project-assumptions-on-binders#19.
8+
//
9+
// When leaving a binder we lift its region constraints into a smaller universe. Constraints
10+
// which already hold inside of the binder have no lower universe candidates to be lifted to,
11+
// so they used to turn into `Or([])`, i.e. `false`. They have to be discharged instead.
12+
13+
// Outlives is reflexive.
14+
core::test_binder_constraints! {
15+
impl<> {
16+
forall<'a> {
17+
'a: 'a
18+
} expect {
19+
}
20+
}
21+
}
22+
23+
// Directly entailed by an assumption of the binder we're leaving.
24+
core::test_binder_constraints! {
25+
impl<> {
26+
forall<'a, 'b> where 'b: 'a {
27+
'b: 'a
28+
} expect {
29+
}
30+
}
31+
}
32+
33+
// Transitively entailed by the assumptions of the binder we're leaving.
34+
core::test_binder_constraints! {
35+
impl<> {
36+
forall<'a, 'b, 'c> where 'c: 'b, 'b: 'a {
37+
'c: 'a
38+
} expect {
39+
}
40+
}
41+
}
42+
43+
// Discharging entailed constraints must not swallow the ones which still have to be lifted
44+
// into the outer universe. Here `'a: 'a` and `'b: 'a` are discharged inside the binder while
45+
// `'c: 'a` is lifted, as `'c` outlives every lower universe region that `'a` outlives.
46+
//
47+
// FIXME(-Zassumptions-on-binders): this should be `impl<'b, 'c: 'b>`, not
48+
// `impl<'b, 'c: 'b + 'static>`, but OR isn't actually implemented yet
49+
core::test_binder_constraints! {
50+
impl<'b, 'c: 'b + 'static> {
51+
forall<'a> where 'b: 'a {
52+
'a: 'a,
53+
'b: 'a,
54+
'c: 'a,
55+
} expect {
56+
or {
57+
'c: 'b,
58+
'c: 'static,
59+
}
60+
}
61+
}
62+
}
63+
64+
fn main() {}

‎tests/ui/assumptions_on_binders/test-infra-fails-properly.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ core::test_binder_constraints! {
6060
} expect {
6161
or {
6262
'c: 'b,
63-
'c: 'c,
63+
'b: 'c,
6464
//~^ ERROR forall expect clause failed
6565
}
6666
}

‎tests/ui/assumptions_on_binders/test-infra-fails-properly.stderr‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ LL | T: 'a,
5353
error: forall expect clause failed
5454
--> $DIR/test-infra-fails-properly.rs:63:17
5555
|
56-
LL | 'c: 'c,
56+
LL | 'b: 'c,
5757
| ^^^^^^
5858
|
5959
note: constraint from here
@@ -62,7 +62,7 @@ note: constraint from here
6262
LL | forall<'a> where 'b: 'a {
6363
| ^^^^^^
6464
= note: expected: RegionOutlives(
65-
'c/#1,
65+
'b/#0,
6666
'c/#1,
6767
$DIR/test-infra-fails-properly.rs:63:17: 63:23 (#0),
6868
)

0 commit comments

Comments
 (0)