@@ -175,6 +175,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
175
175
interior,
176
176
) ) ;
177
177
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 `().`
178
182
let kind_ty = match kind {
179
183
hir:: CoroutineKind :: Desugared ( _, hir:: CoroutineSource :: Closure ) => self
180
184
. next_ty_var ( TypeVariableOrigin {
@@ -203,6 +207,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
203
207
)
204
208
}
205
209
hir:: ClosureKind :: CoroutineClosure ( kind) => {
210
+ // async closures always return the type ascribed after the `->` (if present),
211
+ // and yield `()`.
206
212
let ( bound_return_ty, bound_yield_ty) = match kind {
207
213
hir:: CoroutineDesugaring :: Async => {
208
214
( bound_sig. skip_binder ( ) . output ( ) , tcx. types . unit )
@@ -211,6 +217,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
211
217
todo ! ( "`gen` and `async gen` closures not supported yet" )
212
218
}
213
219
} ;
220
+ // Compute all of the variables that will be used to populate the coroutine.
214
221
let resume_ty = self . next_ty_var ( TypeVariableOrigin {
215
222
kind : TypeVariableOriginKind :: ClosureSynthetic ,
216
223
span : expr_span,
@@ -258,20 +265,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
258
265
kind : TypeVariableOriginKind :: ClosureSynthetic ,
259
266
span : expr_span,
260
267
} ) ;
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
+ ) ;
261
287
liberated_sig = tcx. mk_fn_sig (
262
288
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,
275
290
liberated_sig. c_variadic ,
276
291
liberated_sig. unsafety ,
277
292
liberated_sig. abi ,
0 commit comments