Skip to content

Commit 5bbae1e

Browse files
Rollup merge of #164045 - BoxyUwU:abby_s_region_vars_bound_vars, r=khyperia
-Zassumptions-on-binders: replace region vars with bound vars when handling alias outlives' r? @khyperia See added test. Previously when rewriting `<T as Trait<'?x>>::Assoc: 'b` we would just get `ambiguity`, we now produce `for<'x> <T as Trait<'x>>::Assoc: 'b` in the same way that we do when handling too-large-universe placeholders in alias outlives constraints
2 parents e1d10f3 + ecc137b commit 5bbae1e

2 files changed

Lines changed: 78 additions & 20 deletions

File tree

‎compiler/rustc_type_ir/src/region_constraint.rs‎

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ use crate::inherent::*;
5454
use crate::relate::{Relate, RelateResult, TypeRelation, VarianceDiagInfo};
5555
use crate::visit::TypeVisitableExt;
5656
use crate::{
57-
AliasTy, Binder, BoundRegion, BoundVar, BoundVariableKind, ClauseKind, DebruijnIndex,
58-
InferCtxtLike, Interner, IsRigid, OutlivesClause, Region, RegionKind, TyKind, TypeFoldable,
59-
TypeFolder, TypingMode, UniverseIndex, Variance, elaborate, max_universe,
57+
AliasTy, Binder, BoundRegion, BoundRegionKind, BoundVar, BoundVariableKind, ClauseKind,
58+
DebruijnIndex, InferCtxtLike, Interner, IsRigid, OutlivesClause, Region, RegionKind, TyKind,
59+
TypeFoldable, TypeFolder, TypingMode, UniverseIndex, Variance, elaborate, max_universe,
6060
set_aliases_to_non_rigid,
6161
};
6262

@@ -964,16 +964,16 @@ fn rewrite_alias_ty_outlives_constraints_in_universe_for_eager_placeholder_handl
964964
let higher_ranked_candidate_for_outlived_region = |region| {
965965
let bound_outlives = bound_outlives.map_bound(|(alias, _)| (alias, region));
966966

967-
let mut replacer = PlaceholderReplacer {
968-
cx: infcx.cx(),
969-
existing_var_count: bound_outlives.bound_vars().len(),
970-
bound_vars: IndexMap::default(),
967+
let mut replacer = MakeHigherRankederer {
968+
cx: infcx,
969+
preexisting_var_count: bound_outlives.bound_vars().len(),
970+
regions: IndexMap::default(),
971971
universe: u,
972972
current_index: DebruijnIndex::ZERO,
973973
};
974974
let escaping_outlives = bound_outlives.skip_binder().fold_with(&mut replacer);
975975
let bound_vars = bound_outlives.bound_vars().iter().chain(
976-
core::mem::take(&mut replacer.bound_vars)
976+
core::mem::take(&mut replacer.regions)
977977
.into_iter()
978978
.map(|(_, bound_region)| BoundVariableKind::Region(bound_region.kind)),
979979
);
@@ -982,7 +982,7 @@ fn rewrite_alias_ty_outlives_constraints_in_universe_for_eager_placeholder_handl
982982
I::BoundVarKinds::from_vars(infcx.cx(), bound_vars),
983983
);
984984

985-
// `PlaceholderReplacer` only folds regions. A non-lifetime binder can leave
985+
// `MakeHigherRankederer` only folds regions. A non-lifetime binder can leave
986986
// a placeholder type in `u`, so this type-outlives constraint cannot be
987987
// handled by the region-outlives-only eager placeholder machinery.
988988
if max_universe(infcx, bound_outlives) == u {
@@ -1078,30 +1078,52 @@ pub fn regions_outlived_by_placeholder<I: Interner>(
10781078
})
10791079
}
10801080

1081-
pub struct PlaceholderReplacer<I: Interner> {
1082-
cx: I,
1083-
existing_var_count: usize,
1084-
bound_vars: IndexMap<BoundVar, BoundRegion<I>>,
1081+
pub struct MakeHigherRankederer<'a, I: Interner, Infcx: InferCtxtLike<Interner = I>> {
1082+
cx: &'a Infcx,
1083+
preexisting_var_count: usize,
1084+
regions: IndexMap<Region<I>, BoundRegion<I>>,
10851085
universe: UniverseIndex,
10861086
current_index: DebruijnIndex,
10871087
}
10881088

1089-
impl<I: Interner> TypeFolder<I> for PlaceholderReplacer<I> {
1089+
impl<'a, I: Interner, Infcx: InferCtxtLike<Interner = I>> MakeHigherRankederer<'a, I, Infcx> {
1090+
fn existing_var_count(&self) -> usize {
1091+
self.preexisting_var_count + self.regions.len()
1092+
}
1093+
}
1094+
1095+
impl<'a, I: Interner, Infcx: InferCtxtLike<Interner = I>> TypeFolder<I>
1096+
for MakeHigherRankederer<'a, I, Infcx>
1097+
{
10901098
fn cx(&self) -> I {
1091-
self.cx
1099+
self.cx.cx()
10921100
}
10931101

10941102
fn fold_region(&mut self, r: Region<I>) -> Region<I> {
1103+
let cx = self.cx();
1104+
10951105
match r.kind() {
10961106
RegionKind::RePlaceholder(p) if p.universe == self.universe => {
1097-
let bound_vars_len = self.bound_vars.len();
1098-
let mapped_var = self.bound_vars.entry(p.bound.var).or_insert(BoundRegion {
1099-
var: BoundVar::from_usize(self.existing_var_count + bound_vars_len),
1107+
let existing_var_count = self.existing_var_count();
1108+
let mapped_var = self.regions.entry(r).or_insert(BoundRegion {
1109+
var: BoundVar::from_usize(existing_var_count),
11001110
kind: p.bound.kind,
11011111
});
1102-
Region::new_bound(self.cx, self.current_index, *mapped_var)
1112+
Region::new_bound(cx, self.current_index, *mapped_var)
1113+
}
1114+
RegionKind::ReVar(inf) => {
1115+
let u = self.cx.universe_of_region(inf).unwrap();
1116+
if u == self.universe {
1117+
let existing_var_count = self.existing_var_count();
1118+
let mapped_var = self.regions.entry(r).or_insert(BoundRegion {
1119+
var: BoundVar::from_usize(existing_var_count),
1120+
kind: BoundRegionKind::Anon,
1121+
});
1122+
Region::new_bound(cx, self.current_index, *mapped_var)
1123+
} else {
1124+
r
1125+
}
11031126
}
1104-
// FIXME(-Zassumptions-on-binders): We should be handling region variables here somehow
11051127
_ => r,
11061128
}
11071129
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//@ compile-flags: -Zassumptions-on-binders -Znext-solver=globally
2+
//@ check-pass
3+
4+
// test that when rewriting alias outlives constraints we will replace
5+
// region variables in the universe being handled, with (new) bound vars
6+
// on the alias outlives constraint's binder.
7+
//
8+
// we previously did not do this and would get ambiguity which was overly
9+
// conservative
10+
11+
#![feature(test_binder_constraints)]
12+
13+
trait Trait<'a> {
14+
type Assoc;
15+
}
16+
17+
core::test_binder_constraints! {
18+
impl<T: for<'a> Trait<'a>> {
19+
forall<'b>
20+
where
21+
for<'a> <T as Trait<'a>>::Assoc: 'b
22+
{
23+
forall<> {
24+
exists<'a> {
25+
for<> <T as Trait<'a>>::Assoc: 'b
26+
}
27+
} expect {
28+
for<'a> <T as Trait<'a>>::Assoc: 'b
29+
}
30+
} expect {
31+
or { and {} }
32+
}
33+
}
34+
}
35+
36+
fn main() {}

0 commit comments

Comments
 (0)