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 1 commit
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::ProjectionPredicate<'tcx>> {
fn decode(decoder: &mut D) -> &'tcx Self {
let len = decoder.read_usize();
decoder.tcx().mk_projection_predicates(
(0..len).map::<ty::ProjectionPredicate<'tcx>, _>(|_| Decodable::decode(decoder)),
)
}
}

impl_decodable_via_ref! {
&'tcx ty::TypeckResults<'tcx>,
&'tcx ty::List<Ty<'tcx>>,
&'tcx ty::List<ty::ProjectionPredicate<'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::ProjectionPredicate<'tcx>>,
ty::GeneratorWitnessInner<'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, GeneratorWitnessInner, GenericParamDefKind, InferConst, InferTy, IntTy, IntVar,
IntVid, List, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind,
ProjectionPredicate, 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>,
projection_predicates: InternedSet<'tcx, List<ProjectionPredicate<'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(),
projection_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! {projection_predicates; ProjectionPredicate<'a> => ProjectionPredicate<'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>),
projection_predicates: _intern_projection_predicates(ProjectionPredicate<'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, GeneratorWitnessInner<'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_projection_predicates(
self,
predicates: &[ProjectionPredicate<'tcx>],
) -> &'tcx List<ProjectionPredicate<'tcx>> {
if predicates.is_empty() {
List::empty()
} else {
self._intern_projection_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_projection_predicates<
I: InternAs<[ProjectionPredicate<'tcx>], &'tcx List<ProjectionPredicate<'tcx>>>,
>(
self,
iter: I,
) -> I::Output {
iter.intern_with(|xs| self.intern_projection_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.structural_predicates {
computation.add_predicate_atom(ty::PredicateKind::Projection(predicate));
}
});
}

&ty::Closure(_, substs) => {
Expand Down
10 changes: 5 additions & 5 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,
GeneratorSubsts, GeneratorSubstsParts, GeneratorWitnessInner, 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 @@ -871,7 +871,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
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
38 changes: 19 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,25 @@ 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::GeneratorWitnessInner<'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::GeneratorWitnessInner<'tcx>,
b: ty::GeneratorWitnessInner<'tcx>,
) -> RelateResult<'tcx, ty::GeneratorWitnessInner<'tcx>> {
assert_eq!(a.tys.len(), b.tys.len());
assert_eq!(a.structural_predicates.len(), b.structural_predicates.len());
Ok(ty::GeneratorWitnessInner {
tys: relation
.tcx()
.mk_type_list(a.tys.iter().zip(b.tys).map(|(a, b)| relation.relate(a, b)))?,
structural_predicates: relation.tcx().mk_projection_predicates(
a.structural_predicates
.iter()
.zip(b.structural_predicates)
.map(|(a, b)| relation.relate(a, b)),
)?,
})
}
}

Expand Down Expand Up @@ -434,14 +440,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
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::ProjectionPredicate<'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_projection_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
11 changes: 9 additions & 2 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ pub enum TyKind<'tcx> {
Generator(DefId, SubstsRef<'tcx>, hir::Movability),

/// A type representing the types stored inside a generator.
/// This should only appear in GeneratorInteriors.
GeneratorWitness(Binder<'tcx, &'tcx List<Ty<'tcx>>>),
/// This should only appear inside of a generator's interior types.
GeneratorWitness(Binder<'tcx, GeneratorWitnessInner<'tcx>>),

/// The never type `!`.
Never,
Expand Down Expand Up @@ -2308,3 +2308,10 @@ impl<'tcx> VarianceDiagInfo<'tcx> {
}
}
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing, but we really need a comment here. It'd be good to explain the role of this type and each of its fields.

#[derive(TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable)]
pub struct GeneratorWitnessInner<'tcx> {
nikomatsakis marked this conversation as resolved.
Show resolved Hide resolved
compiler-errors marked this conversation as resolved.
Show resolved Hide resolved
pub tys: &'tcx List<Ty<'tcx>>,
pub structural_predicates: &'tcx List<ty::ProjectionPredicate<'tcx>>,
nikomatsakis marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
| ty::FnDef(_, substs) => {
stack.extend(substs.iter().rev());
}
ty::GeneratorWitness(ts) => {
stack.extend(ts.skip_binder().iter().rev().map(|ty| ty.into()));
ty::GeneratorWitness(inner) => {
nikomatsakis marked this conversation as resolved.
Show resolved Hide resolved
stack.extend(inner.skip_binder().tys.iter().rev().map(|ty| ty.into()));
nikomatsakis marked this conversation as resolved.
Show resolved Hide resolved
}
ty::FnPtr(sig) => {
stack.push(sig.skip_binder().output().into());
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_mir_transform/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,9 +730,10 @@ fn sanitize_witness<'tcx>(

let allowed_upvars = tcx.normalize_erasing_regions(param_env, upvars);
let allowed = match witness.kind() {
&ty::GeneratorWitness(interior_tys) => {
tcx.normalize_erasing_late_bound_regions(param_env, interior_tys)
}
&ty::GeneratorWitness(interior) => tcx.normalize_erasing_late_bound_regions(
param_env,
interior.map_bound(|interior| interior.tys),
),
_ => {
tcx.sess.delay_span_bug(
body.span,
Expand Down
21 changes: 0 additions & 21 deletions compiler/rustc_trait_selection/src/traits/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use super::*;

use crate::infer::region_constraints::{Constraint, RegionConstraintData};
use crate::infer::InferCtxt;
use rustc_middle::ty::fold::TypeFolder;
use rustc_middle::ty::{Region, RegionVid, Term};

use rustc_data_structures::fx::{FxHashMap, FxHashSet};
Expand Down Expand Up @@ -867,23 +866,3 @@ impl<'tcx> AutoTraitFinder<'tcx> {
infcx.freshen(p)
}
}

// Replaces all ReVars in a type with ty::Region's, using the provided map
pub struct RegionReplacer<'a, 'tcx> {
vid_to_region: &'a FxHashMap<ty::RegionVid, ty::Region<'tcx>>,
tcx: TyCtxt<'tcx>,
}

impl<'a, 'tcx> TypeFolder<'tcx> for RegionReplacer<'a, 'tcx> {
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
self.tcx
}

fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
(match r {
ty::ReVar(vid) => self.vid_to_region.get(vid).cloned(),
_ => None,
})
.unwrap_or_else(|| r.super_fold_with(self))
}
}
Loading