@@ -32,7 +32,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
3232 pub ( super ) fn lower_expr_mut ( & mut self , e : & Expr ) -> hir:: Expr < ' hir > {
3333 ensure_sufficient_stack ( || {
3434 match & e. kind {
35- // Paranthesis expression does not have a HirId and is handled specially.
35+ // Parenthesis expression does not have a HirId and is handled specially.
3636 ExprKind :: Paren ( ex) => {
3737 let mut ex = self . lower_expr_mut ( ex) ;
3838 // Include parens in span, but only if it is a super-span.
@@ -63,6 +63,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
6363 ExprKind :: ForLoop ( pat, head, body, opt_label) => {
6464 return self . lower_expr_for ( e, pat, head, body, * opt_label) ;
6565 }
66+ // Similarly, async blocks do not use `e.id` but rather `closure_node_id`.
67+ ExprKind :: Async ( capture_clause, closure_node_id, block) => {
68+ let hir_id = self . lower_node_id ( * closure_node_id) ;
69+ self . lower_attrs ( hir_id, & e. attrs ) ;
70+ return self . make_async_expr (
71+ * capture_clause,
72+ hir_id,
73+ * closure_node_id,
74+ None ,
75+ e. span ,
76+ hir:: AsyncGeneratorKind :: Block ,
77+ |this| this. with_new_scopes ( |this| this. lower_block_expr ( block) ) ,
78+ ) ;
79+ }
6680 _ => ( ) ,
6781 }
6882
@@ -173,15 +187,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
173187 self . arena . alloc_from_iter ( arms. iter ( ) . map ( |x| self . lower_arm ( x) ) ) ,
174188 hir:: MatchSource :: Normal ,
175189 ) ,
176- ExprKind :: Async ( capture_clause, closure_node_id, block) => self . make_async_expr (
177- * capture_clause,
178- hir_id,
179- * closure_node_id,
180- None ,
181- e. span ,
182- hir:: AsyncGeneratorKind :: Block ,
183- |this| this. with_new_scopes ( |this| this. lower_block_expr ( block) ) ,
184- ) ,
185190 ExprKind :: Await ( expr) => {
186191 let dot_await_span = if expr. span . hi ( ) < e. span . hi ( ) {
187192 let span_with_whitespace = self
@@ -315,7 +320,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
315320 ) ,
316321 ExprKind :: Try ( sub_expr) => self . lower_expr_try ( e. span , sub_expr) ,
317322
318- ExprKind :: Paren ( _) | ExprKind :: ForLoop ( ..) => unreachable ! ( "already handled" ) ,
323+ ExprKind :: Paren ( _) | ExprKind :: ForLoop ( ..) | ExprKind :: Async ( ..) => {
324+ unreachable ! ( "already handled" )
325+ }
319326
320327 ExprKind :: MacCall ( _) => panic ! ( "{:?} shouldn't exist here" , e. span) ,
321328 } ;
@@ -577,9 +584,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
577584 /// This results in:
578585 ///
579586 /// ```text
580- /// std::future::identity_future( static move? |_task_context| -> <ret_ty> {
587+ /// static move? |_task_context| -> <ret_ty> {
581588 /// <body>
582- /// })
589+ /// }
583590 /// ```
584591 pub ( super ) fn make_async_expr (
585592 & mut self ,
@@ -590,7 +597,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
590597 span : Span ,
591598 async_gen_kind : hir:: AsyncGeneratorKind ,
592599 body : impl FnOnce ( & mut Self ) -> hir:: Expr < ' hir > ,
593- ) -> hir:: ExprKind < ' hir > {
600+ ) -> hir:: Expr < ' hir > {
594601 let output = ret_ty. unwrap_or_else ( || hir:: FnRetTy :: DefaultReturn ( self . lower_span ( span) ) ) ;
595602
596603 // Resume argument type: `ResumeTy`
@@ -655,13 +662,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
655662 } ;
656663
657664 let hir_id = self . lower_node_id ( closure_node_id) ;
658- let unstable_span =
659- self . mark_span_with_reason ( DesugaringKind :: Async , span, self . allow_gen_future . clone ( ) ) ;
660-
661665 if self . tcx . features ( ) . closure_track_caller
662666 && let Some ( attrs) = self . attrs . get ( & outer_hir_id. local_id )
663667 && attrs. into_iter ( ) . any ( |attr| attr. has_name ( sym:: track_caller) )
664668 {
669+ let unstable_span =
670+ self . mark_span_with_reason ( DesugaringKind :: Async , span, self . allow_gen_future . clone ( ) ) ;
665671 self . lower_attrs (
666672 hir_id,
667673 & [ Attribute {
@@ -680,22 +686,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
680686 ) ;
681687 }
682688
683- let generator = hir:: Expr { hir_id, kind : generator_kind, span : self . lower_span ( span) } ;
684-
685- // FIXME(swatinem):
686- // For some reason, the async block needs to flow through *any*
687- // call (like the identity function), as otherwise type and lifetime
688- // inference have a hard time figuring things out.
689- // Without this, we would get:
690- // E0720 in tests/ui/impl-trait/in-trait/default-body-with-rpit.rs
691- // E0700 in tests/ui/self/self_lifetime-async.rs
692-
693- // `future::identity_future`:
694- let identity_future =
695- self . expr_lang_item_path ( unstable_span, hir:: LangItem :: IdentityFuture , None ) ;
696-
697- // `future::identity_future(generator)`:
698- hir:: ExprKind :: Call ( self . arena . alloc ( identity_future) , arena_vec ! [ self ; generator] )
689+ hir:: Expr { hir_id, kind : generator_kind, span : self . lower_span ( span) }
699690 }
700691
701692 /// Desugar `<expr>.await` into:
@@ -1001,7 +992,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1001992 }
1002993
1003994 // Transform `async |x: u8| -> X { ... }` into
1004- // `|x: u8| identity_future( || -> X { ... }) `.
995+ // `|x: u8| || -> X { ... }`.
1005996 let body_id = this. lower_fn_body ( & outer_decl, |this| {
1006997 let async_ret_ty = if let FnRetTy :: Ty ( ty) = & decl. output {
1007998 let itctx = ImplTraitContext :: Disallowed ( ImplTraitPosition :: AsyncBlock ) ;
@@ -1010,16 +1001,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
10101001 None
10111002 } ;
10121003
1013- let async_body = this. make_async_expr (
1004+ this. make_async_expr (
10141005 capture_clause,
10151006 closure_hir_id,
10161007 inner_closure_id,
10171008 async_ret_ty,
10181009 body. span ,
10191010 hir:: AsyncGeneratorKind :: Closure ,
10201011 |this| this. with_new_scopes ( |this| this. lower_expr_mut ( body) ) ,
1021- ) ;
1022- this. expr ( fn_decl_span, async_body)
1012+ )
10231013 } ) ;
10241014 body_id
10251015 } ) ;
0 commit comments