Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly check auto traits on generator interiors #92449

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions compiler/rustc_middle/src/ty/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,19 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<ty::BoundVaria
}
}

impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<ty::GeneratorPredicate<'tcx>> {
fn decode(decoder: &mut D) -> &'tcx Self {
let len = decoder.read_usize();
decoder.tcx().mk_generator_predicates(
(0..len).map::<ty::GeneratorPredicate<'tcx>, _>(|_| Decodable::decode(decoder)),
)
}
}

impl_decodable_via_ref! {
&'tcx ty::TypeckResults<'tcx>,
&'tcx ty::List<Ty<'tcx>>,
&'tcx ty::List<ty::GeneratorPredicate<'tcx>>,
&'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
&'tcx Allocation,
&'tcx mir::Body<'tcx>,
Expand Down Expand Up @@ -504,6 +514,8 @@ macro_rules! impl_binder_encode_decode {

impl_binder_encode_decode! {
&'tcx ty::List<Ty<'tcx>>,
&'tcx ty::List<ty::GeneratorPredicate<'tcx>>,
ty::GeneratorInterior<'tcx>,
ty::FnSig<'tcx>,
ty::ExistentialPredicate<'tcx>,
ty::TraitRef<'tcx>,
Expand Down
38 changes: 33 additions & 5 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ use crate::ty::TyKind::*;
use crate::ty::{
self, AdtDef, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig,
ClosureSizeProfileData, Const, ConstVid, DefIdTree, ExistentialPredicate, FloatTy, FloatVar,
FloatVid, GenericParamDefKind, InferConst, InferTy, IntTy, IntVar, IntVid, List, ParamConst,
ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind, ProjectionTy, Region, RegionKind,
ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy,
FloatVid, GeneratorInterior, GeneratorPredicate, GenericParamDefKind, InferConst, InferTy,
IntTy, IntVar, IntVid, List, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner,
PredicateKind, ProjectionTy, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind,
TyS, TyVar, TyVid, TypeAndMut, UintTy,
};
use rustc_ast as ast;
use rustc_attr as attr;
Expand Down Expand Up @@ -102,6 +103,7 @@ pub struct CtxtInterners<'tcx> {
substs: InternedSet<'tcx, InternalSubsts<'tcx>>,
canonical_var_infos: InternedSet<'tcx, List<CanonicalVarInfo<'tcx>>>,
region: InternedSet<'tcx, RegionKind>,
generator_predicates: InternedSet<'tcx, List<GeneratorPredicate<'tcx>>>,
poly_existential_predicates:
InternedSet<'tcx, List<ty::Binder<'tcx, ExistentialPredicate<'tcx>>>>,
predicate: InternedSet<'tcx, PredicateInner<'tcx>>,
Expand Down Expand Up @@ -129,6 +131,7 @@ impl<'tcx> CtxtInterners<'tcx> {
type_list: Default::default(),
substs: Default::default(),
region: Default::default(),
generator_predicates: Default::default(),
poly_existential_predicates: Default::default(),
canonical_var_infos: Default::default(),
predicate: Default::default(),
Expand Down Expand Up @@ -1654,6 +1657,7 @@ nop_lift! {const_allocation; &'a Allocation => &'tcx Allocation}
nop_lift! {predicate; &'a PredicateInner<'a> => &'tcx PredicateInner<'tcx>}

nop_list_lift! {type_list; Ty<'a> => Ty<'tcx>}
nop_list_lift! {generator_predicates; GeneratorPredicate<'a> => GeneratorPredicate<'tcx>}
nop_list_lift! {poly_existential_predicates; ty::Binder<'a, ExistentialPredicate<'a>> => ty::Binder<'tcx, ExistentialPredicate<'tcx>>}
nop_list_lift! {predicates; Predicate<'a> => Predicate<'tcx>}
nop_list_lift! {canonical_var_infos; CanonicalVarInfo<'a> => CanonicalVarInfo<'tcx>}
Expand Down Expand Up @@ -2115,6 +2119,7 @@ slice_interners!(
type_list: _intern_type_list(Ty<'tcx>),
substs: _intern_substs(GenericArg<'tcx>),
canonical_var_infos: _intern_canonical_var_infos(CanonicalVarInfo<'tcx>),
generator_predicates: _intern_generator_predicates(GeneratorPredicate<'tcx>),
poly_existential_predicates:
_intern_poly_existential_predicates(ty::Binder<'tcx, ExistentialPredicate<'tcx>>),
predicates: _intern_predicates(Predicate<'tcx>),
Expand Down Expand Up @@ -2406,8 +2411,11 @@ impl<'tcx> TyCtxt<'tcx> {
}

#[inline]
pub fn mk_generator_witness(self, types: ty::Binder<'tcx, &'tcx List<Ty<'tcx>>>) -> Ty<'tcx> {
self.mk_ty(GeneratorWitness(types))
pub fn mk_generator_witness(
self,
inner: ty::Binder<'tcx, GeneratorInterior<'tcx>>,
) -> Ty<'tcx> {
self.mk_ty(GeneratorWitness(inner))
}

#[inline]
Expand Down Expand Up @@ -2509,6 +2517,17 @@ impl<'tcx> TyCtxt<'tcx> {
Place { local: place.local, projection: self.intern_place_elems(&projection) }
}

pub fn intern_generator_predicates(
self,
predicates: &[GeneratorPredicate<'tcx>],
) -> &'tcx List<GeneratorPredicate<'tcx>> {
if predicates.is_empty() {
List::empty()
} else {
self._intern_generator_predicates(predicates)
}
}

pub fn intern_poly_existential_predicates(
self,
eps: &[ty::Binder<'tcx, ExistentialPredicate<'tcx>>],
Expand Down Expand Up @@ -2583,6 +2602,15 @@ impl<'tcx> TyCtxt<'tcx> {
})
}

pub fn mk_generator_predicates<
I: InternAs<[GeneratorPredicate<'tcx>], &'tcx List<GeneratorPredicate<'tcx>>>,
>(
self,
iter: I,
) -> I::Output {
iter.intern_with(|xs| self.intern_generator_predicates(xs))
}

pub fn mk_poly_existential_predicates<
I: InternAs<
[ty::Binder<'tcx, ExistentialPredicate<'tcx>>],
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/fast_reject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ pub fn simplify_type(
}
ty::FnDef(def_id, _) | ty::Closure(def_id, _) => Some(ClosureSimplifiedType(def_id)),
ty::Generator(def_id, _, _) => Some(GeneratorSimplifiedType(def_id)),
ty::GeneratorWitness(ref tys) => {
Some(GeneratorWitnessSimplifiedType(tys.skip_binder().len()))
ty::GeneratorWitness(ref inner, ..) => {
Some(GeneratorWitnessSimplifiedType(inner.as_ref().skip_binder().tys.len()))
}
ty::Never => Some(NeverSimplifiedType),
ty::Tuple(ref tys) => Some(TupleSimplifiedType(tys.len())),
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_middle/src/ty/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,13 @@ impl FlagComputation {
self.add_ty(substs.tupled_upvars_ty());
}

&ty::GeneratorWitness(ts) => {
self.bound_computation(ts, |flags, ts| flags.add_tys(ts));
&ty::GeneratorWitness(inner) => {
self.bound_computation(inner, |computation, inner| {
computation.add_tys(&inner.tys);
for predicate in inner.predicates {
computation.add_predicate_atom(predicate.to_predicate_atom());
}
});
}

&ty::Closure(_, substs) => {
Expand Down
60 changes: 41 additions & 19 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ pub use self::sty::{
Binder, BoundRegion, BoundRegionKind, BoundTy, BoundTyKind, BoundVar, BoundVariableKind,
CanonicalPolyFnSig, ClosureSubsts, ClosureSubstsParts, ConstVid, EarlyBoundRegion,
ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, FnSig, FreeRegion, GenSig,
GeneratorSubsts, GeneratorSubstsParts, InlineConstSubsts, InlineConstSubstsParts, ParamConst,
ParamTy, PolyExistentialProjection, PolyExistentialTraitRef, PolyFnSig, PolyGenSig,
PolyTraitRef, ProjectionTy, Region, RegionKind, RegionVid, TraitRef, TyKind, TypeAndMut,
UpvarSubsts, VarianceDiagInfo,
GeneratorInterior, GeneratorSubsts, GeneratorSubstsParts, InlineConstSubsts,
InlineConstSubstsParts, ParamConst, ParamTy, PolyExistentialProjection,
PolyExistentialTraitRef, PolyFnSig, PolyGenSig, PolyTraitRef, ProjectionTy, Region, RegionKind,
RegionVid, TraitRef, TyKind, TypeAndMut, UpvarSubsts, VarianceDiagInfo,
};
pub use self::trait_def::TraitDef;

Expand Down Expand Up @@ -170,18 +170,8 @@ pub struct ImplHeader<'tcx> {
pub predicates: Vec<Predicate<'tcx>>,
}

#[derive(
Copy,
Clone,
PartialEq,
Eq,
Hash,
TyEncodable,
TyDecodable,
HashStable,
Debug,
TypeFoldable
)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(TyEncodable, TyDecodable, HashStable, Debug, TypeFoldable)]
pub enum ImplPolarity {
/// `impl Trait for Type`
Positive,
Expand Down Expand Up @@ -225,7 +215,8 @@ pub enum Visibility {
Invisible,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Hash, HashStable, TyEncodable, TyDecodable)]
pub enum BoundConstness {
/// `T: Trait`
NotConst,
Expand Down Expand Up @@ -744,7 +735,7 @@ impl<'tcx> Predicate<'tcx> {
}
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)]
#[derive(HashStable, TypeFoldable)]
pub struct TraitPredicate<'tcx> {
pub trait_ref: TraitRef<'tcx>,
Expand Down Expand Up @@ -871,7 +862,7 @@ impl<'tcx> Term<'tcx> {
/// equality between arbitrary types. Processing an instance of
/// Form #2 eventually yields one of these `ProjectionPredicate`
/// instances to normalize the LHS.
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)]
#[derive(HashStable, TypeFoldable)]
pub struct ProjectionPredicate<'tcx> {
pub projection_ty: ProjectionTy<'tcx>,
Expand Down Expand Up @@ -997,6 +988,37 @@ impl<'tcx> Predicate<'tcx> {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Hash, HashStable, TyEncodable, TyDecodable, TypeFoldable)]
pub enum GeneratorPredicate<'tcx> {
Trait(TraitPredicate<'tcx>),
RegionOutlives(RegionOutlivesPredicate<'tcx>),
TypeOutlives(TypeOutlivesPredicate<'tcx>),
Projection(ProjectionPredicate<'tcx>),
}

impl<'tcx> GeneratorPredicate<'tcx> {
pub fn to_predicate_atom(self) -> PredicateKind<'tcx> {
match self {
GeneratorPredicate::Trait(p) => PredicateKind::Trait(p),
GeneratorPredicate::RegionOutlives(p) => PredicateKind::RegionOutlives(p),
GeneratorPredicate::TypeOutlives(p) => PredicateKind::TypeOutlives(p),
GeneratorPredicate::Projection(p) => PredicateKind::Projection(p),
}
}
}

impl<'tcx> ToPredicate<'tcx> for Binder<'tcx, GeneratorPredicate<'tcx>> {
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
match self.skip_binder() {
GeneratorPredicate::Trait(p) => self.rebind(p).to_predicate(tcx),
GeneratorPredicate::RegionOutlives(p) => self.rebind(p).to_predicate(tcx),
GeneratorPredicate::TypeOutlives(p) => self.rebind(p).to_predicate(tcx),
GeneratorPredicate::Projection(p) => self.rebind(p).to_predicate(tcx),
}
}
}

/// Represents the bounds declared on a particular set of type
/// parameters. Should eventually be generalized into a flag list of
/// where-clauses. You can obtain an `InstantiatedPredicates` list from a
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,8 @@ pub trait PrettyPrinter<'tcx>:

p!("]")
}
ty::GeneratorWitness(types) => {
p!(in_binder(&types));
ty::GeneratorWitness(inner, ..) => {
p!(in_binder(&inner.map_bound(|inner| inner.tys)));
}
ty::Closure(did, substs) => {
p!(write("["));
Expand Down
74 changes: 55 additions & 19 deletions compiler/rustc_middle/src/ty/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,19 +340,22 @@ impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
}
}

#[derive(Copy, Debug, Clone, TypeFoldable)]
struct GeneratorWitness<'tcx>(&'tcx ty::List<Ty<'tcx>>);

impl<'tcx> Relate<'tcx> for GeneratorWitness<'tcx> {
impl<'tcx> Relate<'tcx> for ty::GeneratorInterior<'tcx> {
fn relate<R: TypeRelation<'tcx>>(
relation: &mut R,
a: GeneratorWitness<'tcx>,
b: GeneratorWitness<'tcx>,
) -> RelateResult<'tcx, GeneratorWitness<'tcx>> {
assert_eq!(a.0.len(), b.0.len());
let tcx = relation.tcx();
let types = tcx.mk_type_list(iter::zip(a.0, b.0).map(|(a, b)| relation.relate(a, b)))?;
Ok(GeneratorWitness(types))
a: ty::GeneratorInterior<'tcx>,
b: ty::GeneratorInterior<'tcx>,
) -> RelateResult<'tcx, ty::GeneratorInterior<'tcx>> {
assert_eq!(a.tys.len(), b.tys.len());
assert_eq!(a.predicates.len(), b.predicates.len());
Ok(ty::GeneratorInterior {
tys: relation
.tcx()
.mk_type_list(a.tys.iter().zip(b.tys).map(|(a, b)| relation.relate(a, b)))?,
predicates: relation.tcx().mk_generator_predicates(
a.predicates.iter().zip(b.predicates).map(|(a, b)| relation.relate(a, b)),
)?,
})
}
}

Expand Down Expand Up @@ -434,14 +437,8 @@ pub fn super_relate_tys<'tcx, R: TypeRelation<'tcx>>(
Ok(tcx.mk_generator(a_id, substs, movability))
}

(&ty::GeneratorWitness(a_types), &ty::GeneratorWitness(b_types)) => {
// Wrap our types with a temporary GeneratorWitness struct
// inside the binder so we can related them
let a_types = a_types.map_bound(GeneratorWitness);
let b_types = b_types.map_bound(GeneratorWitness);
// Then remove the GeneratorWitness for the result
let types = relation.relate(a_types, b_types)?.map_bound(|witness| witness.0);
Ok(tcx.mk_generator_witness(types))
(&ty::GeneratorWitness(a_interior), &ty::GeneratorWitness(b_interior)) => {
Ok(tcx.mk_generator_witness(relation.relate(a_interior, b_interior)?))
}

(&ty::Closure(a_id, a_substs), &ty::Closure(b_id, b_substs)) if a_id == b_id => {
Expand Down Expand Up @@ -860,6 +857,45 @@ impl<'tcx> Relate<'tcx> for ty::ProjectionPredicate<'tcx> {
}
}

impl<'tcx> Relate<'tcx> for ty::GeneratorPredicate<'tcx> {
fn relate<R: TypeRelation<'tcx>>(
relation: &mut R,
a: ty::GeneratorPredicate<'tcx>,
b: ty::GeneratorPredicate<'tcx>,
) -> RelateResult<'tcx, ty::GeneratorPredicate<'tcx>> {
Ok(match (a, b) {
(ty::GeneratorPredicate::Trait(a), ty::GeneratorPredicate::Trait(b)) => {
ty::GeneratorPredicate::Trait(relation.relate(a, b)?)
}
(
ty::GeneratorPredicate::RegionOutlives(a),
ty::GeneratorPredicate::RegionOutlives(b),
) => ty::GeneratorPredicate::RegionOutlives(relation.relate(a, b)?),
(ty::GeneratorPredicate::TypeOutlives(a), ty::GeneratorPredicate::TypeOutlives(b)) => {
ty::GeneratorPredicate::TypeOutlives(relation.relate(a, b)?)
}
(ty::GeneratorPredicate::Projection(a), ty::GeneratorPredicate::Projection(b)) => {
ty::GeneratorPredicate::Projection(relation.relate(a, b)?)
}
_ => bug!("cannot relate {:?} and {:?}", a, b),
})
}
}

impl<'tcx, A, B> Relate<'tcx> for ty::OutlivesPredicate<A, B>
where
A: Relate<'tcx>,
B: Relate<'tcx>,
{
fn relate<R: TypeRelation<'tcx>>(
relation: &mut R,
a: ty::OutlivesPredicate<A, B>,
b: ty::OutlivesPredicate<A, B>,
) -> RelateResult<'tcx, Self> {
Ok(ty::OutlivesPredicate(relation.relate(a.0, b.0)?, relation.relate(a.1, b.1)?))
}
}

///////////////////////////////////////////////////////////////////////////
// Error handling

Expand Down
17 changes: 15 additions & 2 deletions compiler/rustc_middle/src/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,19 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<Ty<'tcx>> {
}
}

impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::GeneratorPredicate<'tcx>> {
fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
self,
folder: &mut F,
) -> Result<Self, F::Error> {
ty::util::fold_list(self, folder, |tcx, v| tcx.intern_generator_predicates(v))
}

fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
self.iter().try_for_each(|t| t.visit_with(visitor))
}
}

impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind> {
fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
self,
Expand Down Expand Up @@ -1007,7 +1020,7 @@ impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
ty::Generator(did, substs, movability) => {
ty::Generator(did, substs.try_fold_with(folder)?, movability)
}
ty::GeneratorWitness(types) => ty::GeneratorWitness(types.try_fold_with(folder)?),
ty::GeneratorWitness(inner) => ty::GeneratorWitness(inner.try_fold_with(folder)?),
ty::Closure(did, substs) => ty::Closure(did, substs.try_fold_with(folder)?),
ty::Projection(data) => ty::Projection(data.try_fold_with(folder)?),
ty::Opaque(did, substs) => ty::Opaque(did, substs.try_fold_with(folder)?),
Expand Down Expand Up @@ -1055,7 +1068,7 @@ impl<'tcx> TypeFoldable<'tcx> for Ty<'tcx> {
ty.visit_with(visitor)
}
ty::Generator(_did, ref substs, _) => substs.visit_with(visitor),
ty::GeneratorWitness(ref types) => types.visit_with(visitor),
ty::GeneratorWitness(ref inner, ..) => inner.visit_with(visitor),
ty::Closure(_did, ref substs) => substs.visit_with(visitor),
ty::Projection(ref data) => data.visit_with(visitor),
ty::Opaque(_, ref substs) => substs.visit_with(visitor),
Expand Down
Loading