Skip to content

Commit 8e1cabc

Browse files
Generalize the kinds of predicates we stash in the generator interior
1 parent 87d1317 commit 8e1cabc

File tree

16 files changed

+185
-133
lines changed

16 files changed

+185
-133
lines changed

compiler/rustc_middle/src/ty/codec.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -381,19 +381,19 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<ty::BoundVaria
381381
}
382382
}
383383

384-
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<ty::ProjectionPredicate<'tcx>> {
384+
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<ty::GeneratorPredicate<'tcx>> {
385385
fn decode(decoder: &mut D) -> &'tcx Self {
386386
let len = decoder.read_usize();
387-
decoder.tcx().mk_projection_predicates(
388-
(0..len).map::<ty::ProjectionPredicate<'tcx>, _>(|_| Decodable::decode(decoder)),
387+
decoder.tcx().mk_generator_predicates(
388+
(0..len).map::<ty::GeneratorPredicate<'tcx>, _>(|_| Decodable::decode(decoder)),
389389
)
390390
}
391391
}
392392

393393
impl_decodable_via_ref! {
394394
&'tcx ty::TypeckResults<'tcx>,
395395
&'tcx ty::List<Ty<'tcx>>,
396-
&'tcx ty::List<ty::ProjectionPredicate<'tcx>>,
396+
&'tcx ty::List<ty::GeneratorPredicate<'tcx>>,
397397
&'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
398398
&'tcx Allocation,
399399
&'tcx mir::Body<'tcx>,
@@ -514,7 +514,7 @@ macro_rules! impl_binder_encode_decode {
514514

515515
impl_binder_encode_decode! {
516516
&'tcx ty::List<Ty<'tcx>>,
517-
&'tcx ty::List<ty::ProjectionPredicate<'tcx>>,
517+
&'tcx ty::List<ty::GeneratorPredicate<'tcx>>,
518518
ty::GeneratorWitnessInner<'tcx>,
519519
ty::FnSig<'tcx>,
520520
ty::ExistentialPredicate<'tcx>,

compiler/rustc_middle/src/ty/context.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ use crate::ty::TyKind::*;
1717
use crate::ty::{
1818
self, AdtDef, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig,
1919
ClosureSizeProfileData, Const, ConstVid, DefIdTree, ExistentialPredicate, FloatTy, FloatVar,
20-
FloatVid, GeneratorWitnessInner, GenericParamDefKind, InferConst, InferTy, IntTy, IntVar,
21-
IntVid, List, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind,
22-
ProjectionPredicate, ProjectionTy, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty,
23-
TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy,
20+
FloatVid, GeneratorPredicate, GeneratorWitnessInner, GenericParamDefKind, InferConst, InferTy,
21+
IntTy, IntVar, IntVid, List, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner,
22+
PredicateKind, ProjectionTy, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind,
23+
TyS, TyVar, TyVid, TypeAndMut, UintTy,
2424
};
2525
use rustc_ast as ast;
2626
use rustc_attr as attr;
@@ -103,7 +103,7 @@ pub struct CtxtInterners<'tcx> {
103103
substs: InternedSet<'tcx, InternalSubsts<'tcx>>,
104104
canonical_var_infos: InternedSet<'tcx, List<CanonicalVarInfo<'tcx>>>,
105105
region: InternedSet<'tcx, RegionKind>,
106-
projection_predicates: InternedSet<'tcx, List<ProjectionPredicate<'tcx>>>,
106+
generator_predicates: InternedSet<'tcx, List<GeneratorPredicate<'tcx>>>,
107107
poly_existential_predicates:
108108
InternedSet<'tcx, List<ty::Binder<'tcx, ExistentialPredicate<'tcx>>>>,
109109
predicate: InternedSet<'tcx, PredicateInner<'tcx>>,
@@ -131,7 +131,7 @@ impl<'tcx> CtxtInterners<'tcx> {
131131
type_list: Default::default(),
132132
substs: Default::default(),
133133
region: Default::default(),
134-
projection_predicates: Default::default(),
134+
generator_predicates: Default::default(),
135135
poly_existential_predicates: Default::default(),
136136
canonical_var_infos: Default::default(),
137137
predicate: Default::default(),
@@ -1657,7 +1657,7 @@ nop_lift! {const_allocation; &'a Allocation => &'tcx Allocation}
16571657
nop_lift! {predicate; &'a PredicateInner<'a> => &'tcx PredicateInner<'tcx>}
16581658

16591659
nop_list_lift! {type_list; Ty<'a> => Ty<'tcx>}
1660-
nop_list_lift! {projection_predicates; ProjectionPredicate<'a> => ProjectionPredicate<'tcx>}
1660+
nop_list_lift! {generator_predicates; GeneratorPredicate<'a> => GeneratorPredicate<'tcx>}
16611661
nop_list_lift! {poly_existential_predicates; ty::Binder<'a, ExistentialPredicate<'a>> => ty::Binder<'tcx, ExistentialPredicate<'tcx>>}
16621662
nop_list_lift! {predicates; Predicate<'a> => Predicate<'tcx>}
16631663
nop_list_lift! {canonical_var_infos; CanonicalVarInfo<'a> => CanonicalVarInfo<'tcx>}
@@ -2119,7 +2119,7 @@ slice_interners!(
21192119
type_list: _intern_type_list(Ty<'tcx>),
21202120
substs: _intern_substs(GenericArg<'tcx>),
21212121
canonical_var_infos: _intern_canonical_var_infos(CanonicalVarInfo<'tcx>),
2122-
projection_predicates: _intern_projection_predicates(ProjectionPredicate<'tcx>),
2122+
generator_predicates: _intern_generator_predicates(GeneratorPredicate<'tcx>),
21232123
poly_existential_predicates:
21242124
_intern_poly_existential_predicates(ty::Binder<'tcx, ExistentialPredicate<'tcx>>),
21252125
predicates: _intern_predicates(Predicate<'tcx>),
@@ -2517,14 +2517,14 @@ impl<'tcx> TyCtxt<'tcx> {
25172517
Place { local: place.local, projection: self.intern_place_elems(&projection) }
25182518
}
25192519

2520-
pub fn intern_projection_predicates(
2520+
pub fn intern_generator_predicates(
25212521
self,
2522-
predicates: &[ProjectionPredicate<'tcx>],
2523-
) -> &'tcx List<ProjectionPredicate<'tcx>> {
2522+
predicates: &[GeneratorPredicate<'tcx>],
2523+
) -> &'tcx List<GeneratorPredicate<'tcx>> {
25242524
if predicates.is_empty() {
25252525
List::empty()
25262526
} else {
2527-
self._intern_projection_predicates(predicates)
2527+
self._intern_generator_predicates(predicates)
25282528
}
25292529
}
25302530

@@ -2602,13 +2602,13 @@ impl<'tcx> TyCtxt<'tcx> {
26022602
})
26032603
}
26042604

2605-
pub fn mk_projection_predicates<
2606-
I: InternAs<[ProjectionPredicate<'tcx>], &'tcx List<ProjectionPredicate<'tcx>>>,
2605+
pub fn mk_generator_predicates<
2606+
I: InternAs<[GeneratorPredicate<'tcx>], &'tcx List<GeneratorPredicate<'tcx>>>,
26072607
>(
26082608
self,
26092609
iter: I,
26102610
) -> I::Output {
2611-
iter.intern_with(|xs| self.intern_projection_predicates(xs))
2611+
iter.intern_with(|xs| self.intern_generator_predicates(xs))
26122612
}
26132613

26142614
pub fn mk_poly_existential_predicates<

compiler/rustc_middle/src/ty/flags.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ impl FlagComputation {
120120
&ty::GeneratorWitness(inner) => {
121121
self.bound_computation(inner, |computation, inner| {
122122
computation.add_tys(&inner.tys);
123-
for predicate in inner.structural_predicates {
124-
computation.add_predicate_atom(ty::PredicateKind::Projection(predicate));
123+
for predicate in inner.predicates {
124+
computation.add_predicate_atom(predicate.to_predicate_atom());
125125
}
126126
});
127127
}

compiler/rustc_middle/src/ty/mod.rs

+36-14
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,8 @@ pub struct ImplHeader<'tcx> {
170170
pub predicates: Vec<Predicate<'tcx>>,
171171
}
172172

173-
#[derive(
174-
Copy,
175-
Clone,
176-
PartialEq,
177-
Eq,
178-
Hash,
179-
TyEncodable,
180-
TyDecodable,
181-
HashStable,
182-
Debug,
183-
TypeFoldable
184-
)]
173+
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
174+
#[derive(TyEncodable, TyDecodable, HashStable, Debug, TypeFoldable)]
185175
pub enum ImplPolarity {
186176
/// `impl Trait for Type`
187177
Positive,
@@ -225,7 +215,8 @@ pub enum Visibility {
225215
Invisible,
226216
}
227217

228-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable)]
218+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
219+
#[derive(Hash, HashStable, TyEncodable, TyDecodable)]
229220
pub enum BoundConstness {
230221
/// `T: Trait`
231222
NotConst,
@@ -744,7 +735,7 @@ impl<'tcx> Predicate<'tcx> {
744735
}
745736
}
746737

747-
#[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
738+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)]
748739
#[derive(HashStable, TypeFoldable)]
749740
pub struct TraitPredicate<'tcx> {
750741
pub trait_ref: TraitRef<'tcx>,
@@ -997,6 +988,37 @@ impl<'tcx> Predicate<'tcx> {
997988
}
998989
}
999990

991+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
992+
#[derive(Hash, HashStable, TyEncodable, TyDecodable, TypeFoldable)]
993+
pub enum GeneratorPredicate<'tcx> {
994+
Trait(TraitPredicate<'tcx>),
995+
RegionOutlives(RegionOutlivesPredicate<'tcx>),
996+
TypeOutlives(TypeOutlivesPredicate<'tcx>),
997+
Projection(ProjectionPredicate<'tcx>),
998+
}
999+
1000+
impl<'tcx> GeneratorPredicate<'tcx> {
1001+
pub fn to_predicate_atom(self) -> PredicateKind<'tcx> {
1002+
match self {
1003+
GeneratorPredicate::Trait(p) => PredicateKind::Trait(p),
1004+
GeneratorPredicate::RegionOutlives(p) => PredicateKind::RegionOutlives(p),
1005+
GeneratorPredicate::TypeOutlives(p) => PredicateKind::TypeOutlives(p),
1006+
GeneratorPredicate::Projection(p) => PredicateKind::Projection(p),
1007+
}
1008+
}
1009+
}
1010+
1011+
impl<'tcx> ToPredicate<'tcx> for Binder<'tcx, GeneratorPredicate<'tcx>> {
1012+
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1013+
match self.skip_binder() {
1014+
GeneratorPredicate::Trait(p) => self.rebind(p).to_predicate(tcx),
1015+
GeneratorPredicate::RegionOutlives(p) => self.rebind(p).to_predicate(tcx),
1016+
GeneratorPredicate::TypeOutlives(p) => self.rebind(p).to_predicate(tcx),
1017+
GeneratorPredicate::Projection(p) => self.rebind(p).to_predicate(tcx),
1018+
}
1019+
}
1020+
}
1021+
10001022
/// Represents the bounds declared on a particular set of type
10011023
/// parameters. Should eventually be generalized into a flag list of
10021024
/// where-clauses. You can obtain an `InstantiatedPredicates` list from a

compiler/rustc_middle/src/ty/relate.rs

+42-6
Original file line numberDiff line numberDiff line change
@@ -347,16 +347,13 @@ impl<'tcx> Relate<'tcx> for ty::GeneratorWitnessInner<'tcx> {
347347
b: ty::GeneratorWitnessInner<'tcx>,
348348
) -> RelateResult<'tcx, ty::GeneratorWitnessInner<'tcx>> {
349349
assert_eq!(a.tys.len(), b.tys.len());
350-
assert_eq!(a.structural_predicates.len(), b.structural_predicates.len());
350+
assert_eq!(a.predicates.len(), b.predicates.len());
351351
Ok(ty::GeneratorWitnessInner {
352352
tys: relation
353353
.tcx()
354354
.mk_type_list(a.tys.iter().zip(b.tys).map(|(a, b)| relation.relate(a, b)))?,
355-
structural_predicates: relation.tcx().mk_projection_predicates(
356-
a.structural_predicates
357-
.iter()
358-
.zip(b.structural_predicates)
359-
.map(|(a, b)| relation.relate(a, b)),
355+
predicates: relation.tcx().mk_generator_predicates(
356+
a.predicates.iter().zip(b.predicates).map(|(a, b)| relation.relate(a, b)),
360357
)?,
361358
})
362359
}
@@ -860,6 +857,45 @@ impl<'tcx> Relate<'tcx> for ty::ProjectionPredicate<'tcx> {
860857
}
861858
}
862859

860+
impl<'tcx> Relate<'tcx> for ty::GeneratorPredicate<'tcx> {
861+
fn relate<R: TypeRelation<'tcx>>(
862+
relation: &mut R,
863+
a: ty::GeneratorPredicate<'tcx>,
864+
b: ty::GeneratorPredicate<'tcx>,
865+
) -> RelateResult<'tcx, ty::GeneratorPredicate<'tcx>> {
866+
Ok(match (a, b) {
867+
(ty::GeneratorPredicate::Trait(a), ty::GeneratorPredicate::Trait(b)) => {
868+
ty::GeneratorPredicate::Trait(relation.relate(a, b)?)
869+
}
870+
(
871+
ty::GeneratorPredicate::RegionOutlives(a),
872+
ty::GeneratorPredicate::RegionOutlives(b),
873+
) => ty::GeneratorPredicate::RegionOutlives(relation.relate(a, b)?),
874+
(ty::GeneratorPredicate::TypeOutlives(a), ty::GeneratorPredicate::TypeOutlives(b)) => {
875+
ty::GeneratorPredicate::TypeOutlives(relation.relate(a, b)?)
876+
}
877+
(ty::GeneratorPredicate::Projection(a), ty::GeneratorPredicate::Projection(b)) => {
878+
ty::GeneratorPredicate::Projection(relation.relate(a, b)?)
879+
}
880+
_ => bug!("cannot relate {:?} and {:?}", a, b),
881+
})
882+
}
883+
}
884+
885+
impl<'tcx, A, B> Relate<'tcx> for ty::OutlivesPredicate<A, B>
886+
where
887+
A: Relate<'tcx>,
888+
B: Relate<'tcx>,
889+
{
890+
fn relate<R: TypeRelation<'tcx>>(
891+
relation: &mut R,
892+
a: ty::OutlivesPredicate<A, B>,
893+
b: ty::OutlivesPredicate<A, B>,
894+
) -> RelateResult<'tcx, Self> {
895+
Ok(ty::OutlivesPredicate(relation.relate(a.0, b.0)?, relation.relate(a.1, b.1)?))
896+
}
897+
}
898+
863899
///////////////////////////////////////////////////////////////////////////
864900
// Error handling
865901

compiler/rustc_middle/src/ty/structural_impls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -908,12 +908,12 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
908908
}
909909
}
910910

911-
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::ProjectionPredicate<'tcx>> {
911+
impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::GeneratorPredicate<'tcx>> {
912912
fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
913913
self,
914914
folder: &mut F,
915915
) -> Result<Self, F::Error> {
916-
ty::util::fold_list(self, folder, |tcx, v| tcx.intern_projection_predicates(v))
916+
ty::util::fold_list(self, folder, |tcx, v| tcx.intern_generator_predicates(v))
917917
}
918918

919919
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {

compiler/rustc_middle/src/ty/sty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2313,5 +2313,5 @@ impl<'tcx> VarianceDiagInfo<'tcx> {
23132313
#[derive(TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable)]
23142314
pub struct GeneratorWitnessInner<'tcx> {
23152315
pub tys: &'tcx List<Ty<'tcx>>,
2316-
pub structural_predicates: &'tcx List<ty::ProjectionPredicate<'tcx>>,
2316+
pub predicates: &'tcx List<ty::GeneratorPredicate<'tcx>>,
23172317
}

compiler/rustc_monomorphize/src/polymorphize.rs

+1
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
329329
self.visit_child_body(def_id, substs);
330330
ControlFlow::CONTINUE
331331
}
332+
ty::GeneratorWitness(inner) => inner.map_bound(|inner| inner.tys).visit_with(self),
332333
ty::Param(param) => {
333334
debug!(?param);
334335
self.unused_parameters.clear(param.index);

0 commit comments

Comments
 (0)