@@ -346,9 +346,17 @@ static_assert_size!(TyKind<'_>, 24);
346
346
/// ## Generators
347
347
///
348
348
/// 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".
352
360
#[ derive( Copy , Clone , Debug , TypeFoldable ) ]
353
361
pub struct ClosureSubsts < ' tcx > {
354
362
/// Lifetime and type parameters from the enclosing function,
@@ -442,6 +450,7 @@ pub struct GeneratorSubsts<'tcx> {
442
450
}
443
451
444
452
struct SplitGeneratorSubsts < ' tcx > {
453
+ resume_ty : Ty < ' tcx > ,
445
454
yield_ty : Ty < ' tcx > ,
446
455
return_ty : Ty < ' tcx > ,
447
456
witness : Ty < ' tcx > ,
@@ -453,10 +462,11 @@ impl<'tcx> GeneratorSubsts<'tcx> {
453
462
let generics = tcx. generics_of ( def_id) ;
454
463
let parent_len = generics. parent_count ;
455
464
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 ..] ,
460
470
}
461
471
}
462
472
@@ -485,6 +495,11 @@ impl<'tcx> GeneratorSubsts<'tcx> {
485
495
} )
486
496
}
487
497
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
+
488
503
/// Returns the type representing the yield type of the generator.
489
504
pub fn yield_ty ( self , def_id : DefId , tcx : TyCtxt < ' _ > ) -> Ty < ' tcx > {
490
505
self . split ( def_id, tcx) . yield_ty
@@ -505,10 +520,14 @@ impl<'tcx> GeneratorSubsts<'tcx> {
505
520
ty:: Binder :: dummy ( self . sig ( def_id, tcx) )
506
521
}
507
522
508
- /// Returns the "generator signature", which consists of its yield
523
+ /// Returns the "generator signature", which consists of its resume, yield
509
524
/// and return types.
510
525
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
+ }
512
531
}
513
532
}
514
533
@@ -1072,13 +1091,17 @@ impl<'tcx> ProjectionTy<'tcx> {
1072
1091
1073
1092
#[ derive( Clone , Debug , TypeFoldable ) ]
1074
1093
pub struct GenSig < ' tcx > {
1094
+ pub resume_ty : Ty < ' tcx > ,
1075
1095
pub yield_ty : Ty < ' tcx > ,
1076
1096
pub return_ty : Ty < ' tcx > ,
1077
1097
}
1078
1098
1079
1099
pub type PolyGenSig < ' tcx > = Binder < GenSig < ' tcx > > ;
1080
1100
1081
1101
impl < ' tcx > PolyGenSig < ' tcx > {
1102
+ pub fn resume_ty ( & self ) -> ty:: Binder < Ty < ' tcx > > {
1103
+ self . map_bound_ref ( |sig| sig. resume_ty )
1104
+ }
1082
1105
pub fn yield_ty ( & self ) -> ty:: Binder < Ty < ' tcx > > {
1083
1106
self . map_bound_ref ( |sig| sig. yield_ty )
1084
1107
}
0 commit comments