Skip to content

Commit 461a949

Browse files
committed
intern regionoutlivespredicate
1 parent 3e8280d commit 461a949

File tree

5 files changed

+86
-17
lines changed

5 files changed

+86
-17
lines changed

src/librustc_middle/ty/context.rs

+36-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ use crate::ty::{self, DefIdTree, Ty, TypeAndMut};
3030
use crate::ty::{AdtDef, AdtKind, Const, Region};
3131
use crate::ty::{BindingMode, BoundVar};
3232
use crate::ty::{ConstVid, FloatVar, FloatVid, IntVar, IntVid, TyVar, TyVid};
33-
use crate::ty::{ExistentialPredicate, InferTy, ParamTy, PolyFnSig, Predicate, ProjectionTy};
33+
use crate::ty::{
34+
ExistentialPredicate, InferTy, ParamTy, PolyFnSig, Predicate, ProjectionTy,
35+
RegionOutlivesPredicate,
36+
};
3437
use crate::ty::{InferConst, ParamConst};
3538
use crate::ty::{List, TyKind, TyS};
3639
use rustc_ast::ast;
@@ -91,6 +94,7 @@ pub struct CtxtInterners<'tcx> {
9194
canonical_var_infos: InternedSet<'tcx, List<CanonicalVarInfo>>,
9295
region: InternedSet<'tcx, RegionKind>,
9396
existential_predicates: InternedSet<'tcx, List<ExistentialPredicate<'tcx>>>,
97+
region_outlives_predicates: InternedSet<'tcx, List<RegionOutlivesPredicate<'tcx>>>,
9498
predicates: InternedSet<'tcx, List<Predicate<'tcx>>>,
9599
clauses: InternedSet<'tcx, List<Clause<'tcx>>>,
96100
goal: InternedSet<'tcx, GoalKind<'tcx>>,
@@ -109,6 +113,7 @@ impl<'tcx> CtxtInterners<'tcx> {
109113
substs: Default::default(),
110114
region: Default::default(),
111115
existential_predicates: Default::default(),
116+
region_outlives_predicates: Default::default(),
112117
canonical_var_infos: Default::default(),
113118
predicates: Default::default(),
114119
clauses: Default::default(),
@@ -1588,6 +1593,7 @@ nop_list_lift! {goal_list; Goal<'a> => Goal<'tcx>}
15881593
nop_list_lift! {clauses; Clause<'a> => Clause<'tcx>}
15891594
nop_list_lift! {type_list; Ty<'a> => Ty<'tcx>}
15901595
nop_list_lift! {existential_predicates; ExistentialPredicate<'a> => ExistentialPredicate<'tcx>}
1596+
nop_list_lift! {region_outlives_predicates; RegionOutlivesPredicate<'a> => RegionOutlivesPredicate<'tcx>}
15911597
nop_list_lift! {predicates; Predicate<'a> => Predicate<'tcx>}
15921598
nop_list_lift! {canonical_var_infos; CanonicalVarInfo => CanonicalVarInfo}
15931599
nop_list_lift! {projs; ProjectionKind => ProjectionKind}
@@ -2010,6 +2016,14 @@ impl<'tcx> Borrow<[ExistentialPredicate<'tcx>]>
20102016
}
20112017
}
20122018

2019+
impl<'tcx> Borrow<[RegionOutlivesPredicate<'tcx>]>
2020+
for Interned<'tcx, List<RegionOutlivesPredicate<'tcx>>>
2021+
{
2022+
fn borrow<'a>(&'a self) -> &'a [RegionOutlivesPredicate<'tcx>] {
2023+
&self.0[..]
2024+
}
2025+
}
2026+
20132027
impl<'tcx> Borrow<[Predicate<'tcx>]> for Interned<'tcx, List<Predicate<'tcx>>> {
20142028
fn borrow<'a>(&'a self) -> &'a [Predicate<'tcx>] {
20152029
&self.0[..]
@@ -2083,6 +2097,7 @@ slice_interners!(
20832097
substs: _intern_substs(GenericArg<'tcx>),
20842098
canonical_var_infos: _intern_canonical_var_infos(CanonicalVarInfo),
20852099
existential_predicates: _intern_existential_predicates(ExistentialPredicate<'tcx>),
2100+
region_outlives_predicates: _intern_region_outlive_predicates(RegionOutlivesPredicate<'tcx>),
20862101
predicates: _intern_predicates(Predicate<'tcx>),
20872102
clauses: _intern_clauses(Clause<'tcx>),
20882103
goal_list: _intern_goals(Goal<'tcx>),
@@ -2332,7 +2347,7 @@ impl<'tcx> TyCtxt<'tcx> {
23322347
pub fn mk_generator_witness(
23332348
self,
23342349
types: ty::Binder<&'tcx List<Ty<'tcx>>>,
2335-
region_outlives: ty::Binder<&'tcx List<Predicate<'tcx>>>,
2350+
region_outlives: ty::Binder<&'tcx List<RegionOutlivesPredicate<'tcx>>>,
23362351
) -> Ty<'tcx> {
23372352
self.mk_ty(GeneratorWitness(types, region_outlives))
23382353
}
@@ -2445,6 +2460,16 @@ impl<'tcx> TyCtxt<'tcx> {
24452460
self._intern_existential_predicates(eps)
24462461
}
24472462

2463+
pub fn intern_region_outlives_predicates(
2464+
self,
2465+
predicates: &[RegionOutlivesPredicate<'tcx>],
2466+
) -> &'tcx List<RegionOutlivesPredicate<'tcx>> {
2467+
if predicates.is_empty() {
2468+
List::empty()
2469+
} else {
2470+
self._intern_region_outlive_predicates(predicates)
2471+
}
2472+
}
24482473
pub fn intern_predicates(self, preds: &[Predicate<'tcx>]) -> &'tcx List<Predicate<'tcx>> {
24492474
// FIXME consider asking the input slice to be sorted to avoid
24502475
// re-interning permutations, in which case that would be asserted
@@ -2513,6 +2538,15 @@ impl<'tcx> TyCtxt<'tcx> {
25132538
iter.intern_with(|xs| self.intern_existential_predicates(xs))
25142539
}
25152540

2541+
pub fn mk_region_outlives_predicates<
2542+
I: InternAs<[RegionOutlivesPredicate<'tcx>], &'tcx List<RegionOutlivesPredicate<'tcx>>>,
2543+
>(
2544+
self,
2545+
iter: I,
2546+
) -> I::Output {
2547+
iter.intern_with(|xs| self.intern_region_outlives_predicates(xs))
2548+
}
2549+
25162550
pub fn mk_predicates<I: InternAs<[Predicate<'tcx>], &'tcx List<Predicate<'tcx>>>>(
25172551
self,
25182552
iter: I,

src/librustc_middle/ty/relate.rs

+34-10
Original file line numberDiff line numberDiff line change
@@ -308,19 +308,38 @@ impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
308308
}
309309
}
310310

311+
impl<'tcx> Relate<'tcx> for ty::RegionOutlivesPredicate<'tcx> {
312+
fn relate<R: TypeRelation<'tcx>>(
313+
relation: &mut R,
314+
a: &Self,
315+
b: &Self,
316+
) -> RelateResult<'tcx, Self> {
317+
let a_region = relation.relate(&a.0, &b.0)?;
318+
let b_region = relation.relate(&a.1, &b.1)?;
319+
Ok(ty::OutlivesPredicate(a_region, b_region))
320+
}
321+
}
322+
311323
#[derive(Debug, Clone, TypeFoldable)]
312-
struct GeneratorWitness<'tcx>(&'tcx ty::List<Ty<'tcx>>);
324+
struct GeneratorWitness<'tcx>(
325+
&'tcx ty::List<Ty<'tcx>>,
326+
&'tcx ty::List<ty::RegionOutlivesPredicate<'tcx>>,
327+
);
313328

314329
impl<'tcx> Relate<'tcx> for GeneratorWitness<'tcx> {
315330
fn relate<R: TypeRelation<'tcx>>(
316331
relation: &mut R,
317-
a: &GeneratorWitness<'tcx>,
318-
b: &GeneratorWitness<'tcx>,
319-
) -> RelateResult<'tcx, GeneratorWitness<'tcx>> {
332+
a: &Self,
333+
b: &Self,
334+
) -> RelateResult<'tcx, Self> {
320335
assert_eq!(a.0.len(), b.0.len());
336+
assert_eq!(a.1.len(), b.1.len());
321337
let tcx = relation.tcx();
322338
let types = tcx.mk_type_list(a.0.iter().zip(b.0).map(|(a, b)| relation.relate(a, b)))?;
323-
Ok(GeneratorWitness(types))
339+
let predicates = tcx.mk_region_outlives_predicates(
340+
a.1.iter().zip(b.1).map(|(a, b)| relation.relate(a, b)),
341+
)?;
342+
Ok(GeneratorWitness(types, predicates))
324343
}
325344
}
326345

@@ -396,14 +415,19 @@ pub fn super_relate_tys<R: TypeRelation<'tcx>>(
396415
Ok(tcx.mk_generator(a_id, substs, movability))
397416
}
398417

399-
(&ty::GeneratorWitness(a_types, _), &ty::GeneratorWitness(b_types, _)) => {
418+
(
419+
&ty::GeneratorWitness(a_types, a_outlive_predicates),
420+
&ty::GeneratorWitness(b_types, b_outlive_predicates),
421+
) => {
400422
// Wrap our types with a temporary GeneratorWitness struct
401423
// inside the binder so we can related them
402-
let a_types = a_types.map_bound(GeneratorWitness);
403-
let b_types = b_types.map_bound(GeneratorWitness);
424+
let a_witness = a_types.fuse(a_outlive_predicates, GeneratorWitness);
425+
let b_witness = b_types.fuse(b_outlive_predicates, GeneratorWitness);
404426
// Then remove the GeneratorWitness for the result
405-
let types = relation.relate(&a_types, &b_types)?.map_bound(|witness| witness.0);
406-
Ok(tcx.mk_generator_witness(types, ty::Binder::dummy()))
427+
let types = relation.relate(&a_witness, &b_witness)?.map_bound(|witness| witness.0);
428+
let predicates =
429+
relation.relate(&a_witness, &b_witness)?.map_bound(|witness| witness.1);
430+
Ok(tcx.mk_generator_witness(types, predicates))
407431
}
408432

409433
(&ty::Closure(a_id, a_substs), &ty::Closure(b_id, b_substs)) if a_id == b_id => {

src/librustc_middle/ty/structural_impls.rs

+10
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,16 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::ExistentialPredicate<'tcx>>
800800
}
801801
}
802802

803+
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::RegionOutlivesPredicate<'tcx>> {
804+
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
805+
fold_list(*self, folder, |tcx, v| tcx.intern_region_outlives_predicates(v))
806+
}
807+
808+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
809+
self.iter().any(|p| p.visit_with(visitor))
810+
}
811+
}
812+
803813
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
804814
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
805815
fold_list(*self, folder, |tcx, v| tcx.intern_type_list(v))

src/librustc_middle/ty/sty.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,10 @@ pub enum TyKind<'tcx> {
168168

169169
/// A type representin the types stored inside a generator.
170170
/// This should only appear in GeneratorInteriors.
171-
GeneratorWitness(Binder<&'tcx List<Ty<'tcx>>>, Binder<&'tcx List<ty::Predicate<'tcx>>>),
171+
GeneratorWitness(
172+
Binder<&'tcx List<Ty<'tcx>>>,
173+
Binder<&'tcx List<ty::RegionOutlivesPredicate<'tcx>>>,
174+
),
172175

173176
/// The never type `!`
174177
Never,

src/librustc_typeck/check/generator_interior.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,12 @@ pub fn resolve_interior<'a, 'tcx>(
192192
constraints_data
193193
.constraints
194194
.keys()
195-
.map(|constraints| {
196-
ty::Binder::bind(constraints.to_region_outlives_predicate(fcx.tcx)).to_predicate()
197-
})
195+
.map(|constraints| constraints.to_region_outlives_predicate(fcx.tcx))
198196
.collect::<Vec<_>>()
199197
});
200198
debug!("region outlives inside generator: {:?}", region_constraints);
201199

202-
let region_outlives_list = fcx.tcx.mk_predicates(region_constraints.iter());
200+
let region_outlives_list = fcx.tcx.mk_region_outlives_predicates(region_constraints.iter());
203201

204202
let witness = fcx
205203
.tcx

0 commit comments

Comments
 (0)