Skip to content

Commit 0117033

Browse files
Add a resume type param to the generator substs
...and unify it with `()` for now
1 parent 044fe0f commit 0117033

File tree

6 files changed

+42
-13
lines changed

6 files changed

+42
-13
lines changed

src/librustc/infer/opaque_types/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ where
744744

745745
substs.as_generator().return_ty(def_id, self.tcx).visit_with(self);
746746
substs.as_generator().yield_ty(def_id, self.tcx).visit_with(self);
747+
substs.as_generator().resume_ty(def_id, self.tcx).visit_with(self);
747748
}
748749
_ => {
749750
ty.super_visit_with(self);

src/librustc/traits/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ pub fn generator_trait_ref_and_outputs(
645645
) -> ty::Binder<(ty::TraitRef<'tcx>, Ty<'tcx>, Ty<'tcx>)> {
646646
let trait_ref = ty::TraitRef {
647647
def_id: fn_trait_def_id,
648-
substs: tcx.mk_substs_trait(self_ty, &[tcx.mk_unit().into()]),
648+
substs: tcx.mk_substs_trait(self_ty, &[sig.skip_binder().resume_ty.into()]),
649649
};
650650
ty::Binder::bind((trait_ref, sig.skip_binder().yield_ty, sig.skip_binder().return_ty))
651651
}

src/librustc/ty/structural_impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,8 @@ impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::AutoBorrow<'a> {
598598
impl<'a, 'tcx> Lift<'tcx> for ty::GenSig<'a> {
599599
type Lifted = ty::GenSig<'tcx>;
600600
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
601-
tcx.lift(&(self.yield_ty, self.return_ty))
602-
.map(|(yield_ty, return_ty)| ty::GenSig { yield_ty, return_ty })
601+
tcx.lift(&(self.resume_ty, self.yield_ty, self.return_ty))
602+
.map(|(resume_ty, yield_ty, return_ty)| ty::GenSig { resume_ty, yield_ty, return_ty })
603603
}
604604
}
605605

src/librustc/ty/sty.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,17 @@ static_assert_size!(TyKind<'_>, 24);
346346
/// ## Generators
347347
///
348348
/// Generators are handled similarly in `GeneratorSubsts`. The set of
349-
/// type parameters is similar, but the role of CK and CS are
350-
/// different. CK represents the "yield type" and CS represents the
351-
/// "return type" of the generator.
349+
/// type parameters is similar, but `CK` and `CS` are replaced by the
350+
/// following type parameters:
351+
///
352+
/// * `GS`: The generator's "resume type", which is the type of the
353+
/// argument passed to `resume`, and the type of `yield` expressions
354+
/// inside the generator.
355+
/// * `GY`: The "yield type", which is the type of values passed to
356+
/// `yield` inside the generator.
357+
/// * `GR`: The "return type", which is the type of value returned upon
358+
/// completion of the generator.
359+
/// * `GW`: The "generator witness".
352360
#[derive(Copy, Clone, Debug, TypeFoldable)]
353361
pub struct ClosureSubsts<'tcx> {
354362
/// Lifetime and type parameters from the enclosing function,
@@ -442,6 +450,7 @@ pub struct GeneratorSubsts<'tcx> {
442450
}
443451

444452
struct SplitGeneratorSubsts<'tcx> {
453+
resume_ty: Ty<'tcx>,
445454
yield_ty: Ty<'tcx>,
446455
return_ty: Ty<'tcx>,
447456
witness: Ty<'tcx>,
@@ -453,10 +462,11 @@ impl<'tcx> GeneratorSubsts<'tcx> {
453462
let generics = tcx.generics_of(def_id);
454463
let parent_len = generics.parent_count;
455464
SplitGeneratorSubsts {
456-
yield_ty: self.substs.type_at(parent_len),
457-
return_ty: self.substs.type_at(parent_len + 1),
458-
witness: self.substs.type_at(parent_len + 2),
459-
upvar_kinds: &self.substs[parent_len + 3..],
465+
resume_ty: self.substs.type_at(parent_len),
466+
yield_ty: self.substs.type_at(parent_len + 1),
467+
return_ty: self.substs.type_at(parent_len + 2),
468+
witness: self.substs.type_at(parent_len + 3),
469+
upvar_kinds: &self.substs[parent_len + 4..],
460470
}
461471
}
462472

@@ -485,6 +495,11 @@ impl<'tcx> GeneratorSubsts<'tcx> {
485495
})
486496
}
487497

498+
/// Returns the type representing the resume type of the generator.
499+
pub fn resume_ty(self, def_id: DefId, tcx: TyCtxt<'_>) -> Ty<'tcx> {
500+
self.split(def_id, tcx).resume_ty
501+
}
502+
488503
/// Returns the type representing the yield type of the generator.
489504
pub fn yield_ty(self, def_id: DefId, tcx: TyCtxt<'_>) -> Ty<'tcx> {
490505
self.split(def_id, tcx).yield_ty
@@ -505,10 +520,14 @@ impl<'tcx> GeneratorSubsts<'tcx> {
505520
ty::Binder::dummy(self.sig(def_id, tcx))
506521
}
507522

508-
/// Returns the "generator signature", which consists of its yield
523+
/// Returns the "generator signature", which consists of its resume, yield
509524
/// and return types.
510525
pub fn sig(self, def_id: DefId, tcx: TyCtxt<'_>) -> GenSig<'tcx> {
511-
ty::GenSig { yield_ty: self.yield_ty(def_id, tcx), return_ty: self.return_ty(def_id, tcx) }
526+
ty::GenSig {
527+
resume_ty: self.resume_ty(def_id, tcx),
528+
yield_ty: self.yield_ty(def_id, tcx),
529+
return_ty: self.return_ty(def_id, tcx),
530+
}
512531
}
513532
}
514533

@@ -1072,13 +1091,17 @@ impl<'tcx> ProjectionTy<'tcx> {
10721091

10731092
#[derive(Clone, Debug, TypeFoldable)]
10741093
pub struct GenSig<'tcx> {
1094+
pub resume_ty: Ty<'tcx>,
10751095
pub yield_ty: Ty<'tcx>,
10761096
pub return_ty: Ty<'tcx>,
10771097
}
10781098

10791099
pub type PolyGenSig<'tcx> = Binder<GenSig<'tcx>>;
10801100

10811101
impl<'tcx> PolyGenSig<'tcx> {
1102+
pub fn resume_ty(&self) -> ty::Binder<Ty<'tcx>> {
1103+
self.map_bound_ref(|sig| sig.resume_ty)
1104+
}
10821105
pub fn yield_ty(&self) -> ty::Binder<Ty<'tcx>> {
10831106
self.map_bound_ref(|sig| sig.yield_ty)
10841107
}

src/librustc_typeck/check/closure.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9494
});
9595
if let Some(GeneratorTypes { yield_ty, interior, movability }) = generator_types {
9696
let generator_substs = substs.as_generator();
97+
self.demand_eqtype(
98+
expr.span,
99+
self.tcx.mk_unit(), // WIP
100+
generator_substs.resume_ty(expr_def_id, self.tcx),
101+
);
97102
self.demand_eqtype(
98103
expr.span,
99104
yield_ty,

src/librustc_typeck/collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::Generics {
11891189
// and we don't do that for closures.
11901190
if let Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(.., gen), .. }) = node {
11911191
let dummy_args = if gen.is_some() {
1192-
&["<yield_ty>", "<return_ty>", "<witness>"][..]
1192+
&["<resume_ty>", "<yield_ty>", "<return_ty>", "<witness>"][..]
11931193
} else {
11941194
&["<closure_kind>", "<closure_signature>"][..]
11951195
};

0 commit comments

Comments
 (0)