@@ -175,6 +175,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
175175 interior,
176176 ) ) ;
177177
178+ // Coroutines that come from coroutine closures have not yet determined
179+ // their kind ty, so make a fresh infer var which will be constrained
180+ // later during upvar analysis. Regular coroutines always have the kind
181+ // ty of `().`
178182 let kind_ty = match kind {
179183 hir:: CoroutineKind :: Desugared ( _, hir:: CoroutineSource :: Closure ) => self
180184 . next_ty_var ( TypeVariableOrigin {
@@ -203,6 +207,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
203207 )
204208 }
205209 hir:: ClosureKind :: CoroutineClosure ( kind) => {
210+ // async closures always return the type ascribed after the `->` (if present),
211+ // and yield `()`.
206212 let ( bound_return_ty, bound_yield_ty) = match kind {
207213 hir:: CoroutineDesugaring :: Async => {
208214 ( bound_sig. skip_binder ( ) . output ( ) , tcx. types . unit )
@@ -211,6 +217,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
211217 todo ! ( "`gen` and `async gen` closures not supported yet" )
212218 }
213219 } ;
220+ // Compute all of the variables that will be used to populate the coroutine.
214221 let resume_ty = self . next_ty_var ( TypeVariableOrigin {
215222 kind : TypeVariableOriginKind :: ClosureSynthetic ,
216223 span : expr_span,
@@ -258,20 +265,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
258265 kind : TypeVariableOriginKind :: ClosureSynthetic ,
259266 span : expr_span,
260267 } ) ;
268+
269+ // We need to turn the liberated signature that we got from HIR, which
270+ // looks something like `|Args...| -> T`, into a signature that is suitable
271+ // for type checking the inner body of the closure, which always returns a
272+ // coroutine. To do so, we use the `CoroutineClosureSignature` to compute
273+ // the coroutine type, filling in the tupled_upvars_ty and kind_ty with infer
274+ // vars which will get constrained during upvar analysis.
275+ let coroutine_output_ty = tcx. liberate_late_bound_regions (
276+ expr_def_id. to_def_id ( ) ,
277+ closure_args. coroutine_closure_sig ( ) . map_bound ( |sig| {
278+ sig. to_coroutine (
279+ tcx,
280+ parent_args,
281+ closure_kind_ty,
282+ tcx. coroutine_for_closure ( expr_def_id) ,
283+ coroutine_upvars_ty,
284+ )
285+ } ) ,
286+ ) ;
261287 liberated_sig = tcx. mk_fn_sig (
262288 liberated_sig. inputs ( ) . iter ( ) . copied ( ) ,
263- tcx. liberate_late_bound_regions (
264- expr_def_id. to_def_id ( ) ,
265- closure_args. coroutine_closure_sig ( ) . map_bound ( |sig| {
266- sig. to_coroutine (
267- tcx,
268- parent_args,
269- closure_kind_ty,
270- tcx. coroutine_for_closure ( expr_def_id) ,
271- coroutine_upvars_ty,
272- )
273- } ) ,
274- ) ,
289+ coroutine_output_ty,
275290 liberated_sig. c_variadic ,
276291 liberated_sig. unsafety ,
277292 liberated_sig. abi ,
0 commit comments