@@ -97,6 +97,10 @@ pub enum DefiningTy<'tcx> {
9797     /// `ClosureArgs::coroutine_return_ty`. 
9898     Coroutine ( DefId ,  GenericArgsRef < ' tcx > ) , 
9999
100+     /// The MIR is a special kind of closure that returns coroutines. 
101+      /// TODO: describe how to make the sig... 
102+      CoroutineClosure ( DefId ,  GenericArgsRef < ' tcx > ) , 
103+ 
100104    /// The MIR is a fn item with the given `DefId` and args. The signature 
101105     /// of the function can be bound then with the `fn_sig` query. 
102106     FnDef ( DefId ,  GenericArgsRef < ' tcx > ) , 
@@ -119,6 +123,7 @@ impl<'tcx> DefiningTy<'tcx> {
119123     pub  fn  upvar_tys ( self )  -> & ' tcx  ty:: List < Ty < ' tcx > >  { 
120124        match  self  { 
121125            DefiningTy :: Closure ( _,  args)  => args. as_closure ( ) . upvar_tys ( ) , 
126+             DefiningTy :: CoroutineClosure ( _,  args)  => args. as_coroutine_closure ( ) . upvar_tys ( ) , 
122127            DefiningTy :: Coroutine ( _,  args)  => args. as_coroutine ( ) . upvar_tys ( ) , 
123128            DefiningTy :: FnDef ( ..)  | DefiningTy :: Const ( ..)  | DefiningTy :: InlineConst ( ..)  => { 
124129                ty:: List :: empty ( ) 
@@ -131,7 +136,9 @@ impl<'tcx> DefiningTy<'tcx> {
131136     /// user's code. 
132137     pub  fn  implicit_inputs ( self )  -> usize  { 
133138        match  self  { 
134-             DefiningTy :: Closure ( ..)  | DefiningTy :: Coroutine ( ..)  => 1 , 
139+             DefiningTy :: Closure ( ..) 
140+             | DefiningTy :: CoroutineClosure ( ..) 
141+             | DefiningTy :: Coroutine ( ..)  => 1 , 
135142            DefiningTy :: FnDef ( ..)  | DefiningTy :: Const ( ..)  | DefiningTy :: InlineConst ( ..)  => 0 , 
136143        } 
137144    } 
@@ -147,6 +154,7 @@ impl<'tcx> DefiningTy<'tcx> {
147154    pub  fn  def_id ( & self )  -> DefId  { 
148155        match  * self  { 
149156            DefiningTy :: Closure ( def_id,  ..) 
157+             | DefiningTy :: CoroutineClosure ( def_id,  ..) 
150158            | DefiningTy :: Coroutine ( def_id,  ..) 
151159            | DefiningTy :: FnDef ( def_id,  ..) 
152160            | DefiningTy :: Const ( def_id,  ..) 
@@ -355,6 +363,9 @@ impl<'tcx> UniversalRegions<'tcx> {
355363                    err. note ( format ! ( "late-bound region is {:?}" ,  self . to_region_vid( r) ) ) ; 
356364                } ) ; 
357365            } 
366+             DefiningTy :: CoroutineClosure ( ..)  => { 
367+                 todo ! ( ) 
368+             } 
358369            DefiningTy :: Coroutine ( def_id,  args)  => { 
359370                let  v = with_no_trimmed_paths ! ( 
360371                    args[ tcx. generics_of( def_id) . parent_count..] 
@@ -568,6 +579,9 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
568579                match  * defining_ty. kind ( )  { 
569580                    ty:: Closure ( def_id,  args)  => DefiningTy :: Closure ( def_id,  args) , 
570581                    ty:: Coroutine ( def_id,  args)  => DefiningTy :: Coroutine ( def_id,  args) , 
582+                     ty:: CoroutineClosure ( def_id,  args)  => { 
583+                         DefiningTy :: CoroutineClosure ( def_id,  args) 
584+                     } 
571585                    ty:: FnDef ( def_id,  args)  => DefiningTy :: FnDef ( def_id,  args) , 
572586                    _ => span_bug ! ( 
573587                        tcx. def_span( self . mir_def) , 
@@ -623,6 +637,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
623637        let  identity_args = GenericArgs :: identity_for_item ( tcx,  typeck_root_def_id) ; 
624638        let  fr_args = match  defining_ty { 
625639            DefiningTy :: Closure ( _,  args) 
640+             | DefiningTy :: CoroutineClosure ( _,  args) 
626641            | DefiningTy :: Coroutine ( _,  args) 
627642            | DefiningTy :: InlineConst ( _,  args)  => { 
628643                // In the case of closures, we rely on the fact that 
@@ -702,6 +717,47 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
702717                ty:: Binder :: dummy ( inputs_and_output) 
703718            } 
704719
720+             DefiningTy :: CoroutineClosure ( def_id,  args)  => { 
721+                 assert_eq ! ( self . mir_def. to_def_id( ) ,  def_id) ; 
722+                 let  closure_sig = args. as_coroutine_closure ( ) . coroutine_closure_sig ( ) ; 
723+                 let  bound_vars = tcx. mk_bound_variable_kinds_from_iter ( 
724+                     closure_sig
725+                         . bound_vars ( ) 
726+                         . iter ( ) 
727+                         . chain ( iter:: once ( ty:: BoundVariableKind :: Region ( ty:: BrEnv ) ) ) , 
728+                 ) ; 
729+                 let  br = ty:: BoundRegion  { 
730+                     var :  ty:: BoundVar :: from_usize ( bound_vars. len ( )  - 1 ) , 
731+                     kind :  ty:: BrEnv , 
732+                 } ; 
733+                 let  env_region = ty:: Region :: new_bound ( tcx,  ty:: INNERMOST ,  br) ; 
734+                 let  closure_kind = args. as_coroutine_closure ( ) . kind ( ) ; 
735+ 
736+                 let  closure_ty = tcx. closure_env_ty ( 
737+                     Ty :: new_coroutine_closure ( tcx,  def_id,  args) , 
738+                     closure_kind, 
739+                     env_region, 
740+                 ) ; 
741+ 
742+                 let  inputs = closure_sig. skip_binder ( ) . tupled_inputs_ty . tuple_fields ( ) ; 
743+                 let  output = closure_sig. skip_binder ( ) . to_coroutine_given_kind_and_upvars ( 
744+                     tcx, 
745+                     args. as_coroutine_closure ( ) . parent_args ( ) , 
746+                     tcx. coroutine_for_closure ( def_id) , 
747+                     closure_kind, 
748+                     env_region, 
749+                     args. as_coroutine_closure ( ) . tupled_upvars_ty ( ) , 
750+                     args. as_coroutine_closure ( ) . coroutine_captures_by_ref_ty ( ) , 
751+                 ) ; 
752+ 
753+                 ty:: Binder :: bind_with_vars ( 
754+                     tcx. mk_type_list_from_iter ( 
755+                         iter:: once ( closure_ty) . chain ( inputs) . chain ( iter:: once ( output) ) , 
756+                     ) , 
757+                     bound_vars, 
758+                 ) 
759+             } 
760+ 
705761            DefiningTy :: FnDef ( def_id,  _)  => { 
706762                let  sig = tcx. fn_sig ( def_id) . instantiate_identity ( ) ; 
707763                let  sig = indices. fold_to_region_vids ( tcx,  sig) ; 
0 commit comments