Skip to content

Commit

Permalink
Add comments, adjust names
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jan 28, 2022
1 parent 8e1cabc commit 9e72ef4
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ macro_rules! impl_binder_encode_decode {
impl_binder_encode_decode! {
&'tcx ty::List<Ty<'tcx>>,
&'tcx ty::List<ty::GeneratorPredicate<'tcx>>,
ty::GeneratorWitnessInner<'tcx>,
ty::GeneratorInterior<'tcx>,
ty::FnSig<'tcx>,
ty::ExistentialPredicate<'tcx>,
ty::TraitRef<'tcx>,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::ty::TyKind::*;
use crate::ty::{
self, AdtDef, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig,
ClosureSizeProfileData, Const, ConstVid, DefIdTree, ExistentialPredicate, FloatTy, FloatVar,
FloatVid, GeneratorPredicate, GeneratorWitnessInner, GenericParamDefKind, InferConst, InferTy,
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,
Expand Down Expand Up @@ -2413,7 +2413,7 @@ impl<'tcx> TyCtxt<'tcx> {
#[inline]
pub fn mk_generator_witness(
self,
inner: ty::Binder<'tcx, GeneratorWitnessInner<'tcx>>,
inner: ty::Binder<'tcx, GeneratorInterior<'tcx>>,
) -> Ty<'tcx> {
self.mk_ty(GeneratorWitness(inner))
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub use self::sty::{
Binder, BoundRegion, BoundRegionKind, BoundTy, BoundTyKind, BoundVar, BoundVariableKind,
CanonicalPolyFnSig, ClosureSubsts, ClosureSubstsParts, ConstVid, EarlyBoundRegion,
ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, FnSig, FreeRegion, GenSig,
GeneratorSubsts, GeneratorSubstsParts, GeneratorWitnessInner, InlineConstSubsts,
GeneratorInterior, GeneratorSubsts, GeneratorSubstsParts, InlineConstSubsts,
InlineConstSubstsParts, ParamConst, ParamTy, PolyExistentialProjection,
PolyExistentialTraitRef, PolyFnSig, PolyGenSig, PolyTraitRef, ProjectionTy, Region, RegionKind,
RegionVid, TraitRef, TyKind, TypeAndMut, UpvarSubsts, VarianceDiagInfo,
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_middle/src/ty/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,15 @@ impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
}
}

impl<'tcx> Relate<'tcx> for ty::GeneratorWitnessInner<'tcx> {
impl<'tcx> Relate<'tcx> for ty::GeneratorInterior<'tcx> {
fn relate<R: TypeRelation<'tcx>>(
relation: &mut R,
a: ty::GeneratorWitnessInner<'tcx>,
b: ty::GeneratorWitnessInner<'tcx>,
) -> RelateResult<'tcx, ty::GeneratorWitnessInner<'tcx>> {
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::GeneratorWitnessInner {
Ok(ty::GeneratorInterior {
tys: relation
.tcx()
.mk_type_list(a.tys.iter().zip(b.tys).map(|(a, b)| relation.relate(a, b)))?,
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub enum TyKind<'tcx> {

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

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

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable)]
pub struct GeneratorWitnessInner<'tcx> {
pub struct GeneratorInterior<'tcx> {
/// types alive across await points in the generator
pub tys: &'tcx List<Ty<'tcx>>,
/// predicates that we know hold in the environment where the generator was created,
/// which are used when checking auto traits on the generator witness
pub predicates: &'tcx List<ty::GeneratorPredicate<'tcx>>,
}
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
let (param_env, nested) =
if let ty::GeneratorWitness(interior) = self_ty.skip_binder().kind() {
self.infcx().commit_unconditionally(|_| {
let ty::GeneratorWitnessInner { tys: nested, predicates } =
let ty::GeneratorInterior { tys: nested, predicates } =
self.infcx().replace_bound_vars_with_placeholders(*interior);
// FIXME(compiler-errors): Not sure if we should augment the param_env,
// or just make a new param_env from these predicates...
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_typeck/src/check/generator_interior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> {
.find(|yield_data| {
trace!(
"comparing counts yield: {} self: {}, source_span = {:?}",
yield_data.expr_and_pat_count, self.expr_count, source_span
yield_data.expr_and_pat_count,
self.expr_count,
source_span
);

if ENABLE_DROP_TRACKING
Expand Down Expand Up @@ -304,7 +306,7 @@ pub fn resolve_interior<'a, 'tcx>(
);

let witness = fcx.tcx.mk_generator_witness(ty::Binder::bind_with_vars(
ty::GeneratorWitnessInner { tys: anon_tys, predicates: anon_predicates },
ty::GeneratorInterior { tys: anon_tys, predicates: anon_predicates },
bound_vars.clone(),
));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ use rustc_data_structures::stable_set::FxHashSet;
use rustc_middle::ty::{self, Ty};
use rustc_span::Span;

/// This helper walks through generator-interior types, collecting projection types,
/// and creating ProjectionPredicates that we can use in the param-env when checking
/// auto traits later on.
///
/// We walk through the constituent types of a type, and when we encounter a projection,
/// normalize that projection. If that normalization was successful, then create a
/// ProjectionPredicate out of the old projection type and its normalized ty.
pub(super) struct StructuralPredicateElaborator<'a, 'tcx> {
stack: Vec<Ty<'tcx>>,
seen: FxHashSet<Ty<'tcx>>,
Expand All @@ -19,18 +26,8 @@ impl<'a, 'tcx> StructuralPredicateElaborator<'a, 'tcx> {
StructuralPredicateElaborator { seen, stack, fcx, span }
}

/// For default impls, we need to break apart a type into its
/// "constituent types" -- meaning, the types that it contains.
///
/// Here are some (simple) examples:
///
/// ```
/// (i32, u32) -> [i32, u32]
/// Foo where struct Foo { x: i32, y: u32 } -> [i32, u32]
/// Bar<i32> where struct Bar<T> { x: T, y: u32 } -> [i32, u32]
/// Zed<i32> where enum Zed { A(T), B(u32) } -> [i32, u32]
/// ```
fn constituent_types_for_auto_trait(&self, t: Ty<'tcx>) -> Vec<Ty<'tcx>> {
// Ripped from confirmation code, lol.
fn constituent_types(&self, t: Ty<'tcx>) -> Vec<Ty<'tcx>> {
match *t.kind() {
ty::Projection(..) => {
bug!("this type should be handled separately: {:?}", t)
Expand Down Expand Up @@ -100,10 +97,12 @@ impl<'tcx> Iterator for StructuralPredicateElaborator<'_, 'tcx> {
while let Some(ty) = self.stack.pop() {
if let ty::Projection(projection_ty) = *ty.kind() {
let mut normalized_ty = self.fcx.normalize_associated_types_in(self.span, ty);
// Try to resolve the projection type
if normalized_ty.is_ty_var() {
self.fcx.select_obligations_where_possible(false, |_| {});
normalized_ty = self.fcx.resolve_vars_if_possible(normalized_ty);
}
// If we have a normalized type, then stash it
if !normalized_ty.is_ty_var() && normalized_ty != ty {
if self.seen.insert(normalized_ty) {
self.stack.push(normalized_ty);
Expand All @@ -115,7 +114,7 @@ impl<'tcx> Iterator for StructuralPredicateElaborator<'_, 'tcx> {
}
} else {
let structural: Vec<_> = self
.constituent_types_for_auto_trait(ty)
.constituent_types(ty)
.into_iter()
.map(|ty| self.fcx.resolve_vars_if_possible(ty))
.filter(|ty| self.seen.insert(ty))
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ struct GeneratorTypes<'tcx> {
/// Type of value that is yielded.
yield_ty: Ty<'tcx>,

/// Types that are captured (see `GeneratorWitnessInner` for more).
/// Types that are captured (see `GeneratorInterior` for more).
interior: Ty<'tcx>,

/// Indicates if the generator is movable or static (immovable).
Expand Down

0 comments on commit 9e72ef4

Please sign in to comment.