@@ -275,7 +275,23 @@ impl<'a> InferenceContext<'a> {
275275 Some ( type_ref) => self . make_ty ( type_ref) ,
276276 None => self . table . new_type_var ( ) ,
277277 } ;
278- sig_tys. push ( ret_ty. clone ( ) ) ;
278+ if let ClosureKind :: Async = closure_kind {
279+ // Use the first type parameter as the output type of future.
280+ // existential type AsyncBlockImplTrait<InnerType>: Future<Output = InnerType>
281+ let impl_trait_id =
282+ crate :: ImplTraitId :: AsyncBlockTypeImplTrait ( self . owner , * body) ;
283+ let opaque_ty_id = self . db . intern_impl_trait_id ( impl_trait_id) . into ( ) ;
284+ sig_tys. push (
285+ TyKind :: OpaqueType (
286+ opaque_ty_id,
287+ Substitution :: from1 ( Interner , ret_ty. clone ( ) ) ,
288+ )
289+ . intern ( Interner ) ,
290+ ) ;
291+ } else {
292+ sig_tys. push ( ret_ty. clone ( ) ) ;
293+ }
294+
279295 let sig_ty = TyKind :: Function ( FnPointer {
280296 num_binders : 0 ,
281297 sig : FnSig { abi : ( ) , safety : chalk_ir:: Safety :: Safe , variadic : false } ,
@@ -286,7 +302,7 @@ impl<'a> InferenceContext<'a> {
286302 } )
287303 . intern ( Interner ) ;
288304
289- let ( closure_id , ty, resume_yield_tys) = match closure_kind {
305+ let ( ty, resume_yield_tys) = match closure_kind {
290306 ClosureKind :: Generator ( _) => {
291307 // FIXME: report error when there are more than 1 parameter.
292308 let resume_ty = match sig_tys. first ( ) {
@@ -306,17 +322,17 @@ impl<'a> InferenceContext<'a> {
306322 let generator_id = self . db . intern_generator ( ( self . owner , tgt_expr) ) . into ( ) ;
307323 let generator_ty = TyKind :: Generator ( generator_id, subst) . intern ( Interner ) ;
308324
309- ( None , generator_ty, Some ( ( resume_ty, yield_ty) ) )
325+ ( generator_ty, Some ( ( resume_ty, yield_ty) ) )
310326 }
311- ClosureKind :: Async | ClosureKind :: Closure => {
327+ ClosureKind :: Closure | ClosureKind :: Async => {
312328 let closure_id = self . db . intern_closure ( ( self . owner , tgt_expr) ) . into ( ) ;
313329 let closure_ty = TyKind :: Closure (
314330 closure_id,
315331 Substitution :: from1 ( Interner , sig_ty. clone ( ) ) ,
316332 )
317333 . intern ( Interner ) ;
318334
319- ( Some ( closure_id ) , closure_ty, None )
335+ ( closure_ty, None )
320336 }
321337 } ;
322338
@@ -338,47 +354,16 @@ impl<'a> InferenceContext<'a> {
338354 let prev_resume_yield_tys =
339355 mem:: replace ( & mut self . resume_yield_tys , resume_yield_tys) ;
340356
341- let ( breaks, ( ) ) =
342- self . with_breakable_ctx ( BreakableKind :: Border , None , None , |this| {
343- this. infer_return ( * body) ;
344- } ) ;
345-
346- let inner_ty = if matches ! ( closure_kind, ClosureKind :: Async ) {
347- // Use the first type parameter as the output type of future.
348- // existential type AsyncBlockImplTrait<InnerType>: Future<Output = InnerType>
349- let impl_trait_id =
350- crate :: ImplTraitId :: AsyncBlockTypeImplTrait ( self . owner , * body) ;
351- let opaque_ty_id = self . db . intern_impl_trait_id ( impl_trait_id) . into ( ) ;
352- TyKind :: OpaqueType ( opaque_ty_id, Substitution :: from1 ( Interner , ret_ty. clone ( ) ) )
353- . intern ( Interner )
354- } else {
355- ret_ty. clone ( )
356- } ;
357+ self . with_breakable_ctx ( BreakableKind :: Border , None , None , |this| {
358+ this. infer_return ( * body) ;
359+ } ) ;
357360
358361 self . diverges = prev_diverges;
359362 self . return_ty = prev_ret_ty;
360363 self . return_coercion = prev_ret_coercion;
361364 self . resume_yield_tys = prev_resume_yield_tys;
362365
363- sig_tys. pop ( ) ;
364- sig_tys. push ( inner_ty) ;
365-
366- let sig_ty = TyKind :: Function ( FnPointer {
367- num_binders : 0 ,
368- sig : FnSig { abi : ( ) , safety : chalk_ir:: Safety :: Safe , variadic : false } ,
369- substitution : FnSubst (
370- Substitution :: from_iter ( Interner , sig_tys. clone ( ) ) . shifted_in ( Interner ) ,
371- ) ,
372- } )
373- . intern ( Interner ) ;
374-
375- match closure_id {
376- Some ( closure_id) => {
377- TyKind :: Closure ( closure_id, Substitution :: from1 ( Interner , sig_ty. clone ( ) ) )
378- . intern ( Interner )
379- }
380- None => ty,
381- }
366+ ty
382367 }
383368 Expr :: Call { callee, args, .. } => {
384369 let callee_ty = self . infer_expr ( * callee, & Expectation :: none ( ) ) ;
0 commit comments