@@ -711,6 +711,7 @@ impl<'tcx> TyCtxt<'tcx> {
711
711
check_recursion : false ,
712
712
expand_coroutines : false ,
713
713
tcx : self ,
714
+ inspect_coroutine_fields : InspectCoroutineFields :: No ,
714
715
} ;
715
716
val. fold_with ( & mut visitor)
716
717
}
@@ -721,6 +722,7 @@ impl<'tcx> TyCtxt<'tcx> {
721
722
self ,
722
723
def_id : DefId ,
723
724
args : GenericArgsRef < ' tcx > ,
725
+ inspect_coroutine_fields : InspectCoroutineFields ,
724
726
) -> Result < Ty < ' tcx > , Ty < ' tcx > > {
725
727
let mut visitor = OpaqueTypeExpander {
726
728
seen_opaque_tys : FxHashSet :: default ( ) ,
@@ -731,6 +733,7 @@ impl<'tcx> TyCtxt<'tcx> {
731
733
check_recursion : true ,
732
734
expand_coroutines : true ,
733
735
tcx : self ,
736
+ inspect_coroutine_fields,
734
737
} ;
735
738
736
739
let expanded_type = visitor. expand_opaque_ty ( def_id, args) . unwrap ( ) ;
@@ -816,6 +819,13 @@ struct OpaqueTypeExpander<'tcx> {
816
819
/// recursion, and 'false' otherwise to avoid unnecessary work.
817
820
check_recursion : bool ,
818
821
tcx : TyCtxt < ' tcx > ,
822
+ inspect_coroutine_fields : InspectCoroutineFields ,
823
+ }
824
+
825
+ #[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
826
+ pub enum InspectCoroutineFields {
827
+ No ,
828
+ Yes ,
819
829
}
820
830
821
831
impl < ' tcx > OpaqueTypeExpander < ' tcx > {
@@ -854,7 +864,20 @@ impl<'tcx> OpaqueTypeExpander<'tcx> {
854
864
}
855
865
let args = args. fold_with ( self ) ;
856
866
if !self . check_recursion || self . seen_opaque_tys . insert ( def_id) {
857
- let expanded_ty = Ty :: new_coroutine_witness ( self . tcx , def_id, args) ;
867
+ let expanded_ty = match self . expanded_cache . get ( & ( def_id, args) ) {
868
+ Some ( expanded_ty) => * expanded_ty,
869
+ None => {
870
+ if matches ! ( self . inspect_coroutine_fields, InspectCoroutineFields :: Yes ) {
871
+ for bty in self . tcx . coroutine_hidden_types ( def_id) {
872
+ let hidden_ty = bty. instantiate ( self . tcx , args) ;
873
+ self . fold_ty ( hidden_ty) ;
874
+ }
875
+ }
876
+ let expanded_ty = Ty :: new_coroutine_witness ( self . tcx , def_id, args) ;
877
+ self . expanded_cache . insert ( ( def_id, args) , expanded_ty) ;
878
+ expanded_ty
879
+ }
880
+ } ;
858
881
if self . check_recursion {
859
882
self . seen_opaque_tys . remove ( & def_id) ;
860
883
}
@@ -1426,6 +1449,7 @@ pub fn reveal_opaque_types_in_bounds<'tcx>(
1426
1449
check_recursion : false ,
1427
1450
expand_coroutines : false ,
1428
1451
tcx,
1452
+ inspect_coroutine_fields : InspectCoroutineFields :: No ,
1429
1453
} ;
1430
1454
val. fold_with ( & mut visitor)
1431
1455
}
0 commit comments