@@ -63,20 +63,6 @@ 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- }
8066 _ => ( ) ,
8167 }
8268
@@ -187,6 +173,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
187173 self . arena . alloc_from_iter ( arms. iter ( ) . map ( |x| self . lower_arm ( x) ) ) ,
188174 hir:: MatchSource :: Normal ,
189175 ) ,
176+ ExprKind :: Async ( capture_clause, block) => self . make_async_expr (
177+ * capture_clause,
178+ e. id ,
179+ None ,
180+ e. span ,
181+ hir:: AsyncGeneratorKind :: Block ,
182+ |this| this. with_new_scopes ( |this| this. lower_block_expr ( block) ) ,
183+ ) ,
190184 ExprKind :: Await ( expr) => {
191185 let dot_await_span = if expr. span . hi ( ) < e. span . hi ( ) {
192186 let span_with_whitespace = self
@@ -320,7 +314,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
320314 ) ,
321315 ExprKind :: Try ( sub_expr) => self . lower_expr_try ( e. span , sub_expr) ,
322316
323- ExprKind :: Paren ( _) | ExprKind :: ForLoop ( ..) | ExprKind :: Async ( .. ) => {
317+ ExprKind :: Paren ( _) | ExprKind :: ForLoop ( ..) => {
324318 unreachable ! ( "already handled" )
325319 }
326320
@@ -591,13 +585,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
591585 pub ( super ) fn make_async_expr (
592586 & mut self ,
593587 capture_clause : CaptureBy ,
594- outer_hir_id : hir:: HirId ,
595588 closure_node_id : NodeId ,
596589 ret_ty : Option < hir:: FnRetTy < ' hir > > ,
597590 span : Span ,
598591 async_gen_kind : hir:: AsyncGeneratorKind ,
599592 body : impl FnOnce ( & mut Self ) -> hir:: Expr < ' hir > ,
600- ) -> hir:: Expr < ' hir > {
593+ ) -> hir:: ExprKind < ' hir > {
601594 let output = ret_ty. unwrap_or_else ( || hir:: FnRetTy :: DefaultReturn ( self . lower_span ( span) ) ) ;
602595
603596 // Resume argument type: `ResumeTy`
@@ -644,32 +637,36 @@ impl<'hir> LoweringContext<'_, 'hir> {
644637 } ) ;
645638
646639 // `static |_task_context| -> <ret_ty> { body }`:
647- let generator_kind = {
648- let c = self . arena . alloc ( hir:: Closure {
649- def_id : self . local_def_id ( closure_node_id) ,
650- binder : hir:: ClosureBinder :: Default ,
651- capture_clause,
652- bound_generic_params : & [ ] ,
653- fn_decl,
654- body,
655- fn_decl_span : self . lower_span ( span) ,
656- fn_arg_span : None ,
657- movability : Some ( hir:: Movability :: Static ) ,
658- constness : hir:: Constness :: NotConst ,
659- } ) ;
660-
661- hir:: ExprKind :: Closure ( c)
662- } ;
640+ hir:: ExprKind :: Closure ( self . arena . alloc ( hir:: Closure {
641+ def_id : self . local_def_id ( closure_node_id) ,
642+ binder : hir:: ClosureBinder :: Default ,
643+ capture_clause,
644+ bound_generic_params : & [ ] ,
645+ fn_decl,
646+ body,
647+ fn_decl_span : self . lower_span ( span) ,
648+ fn_arg_span : None ,
649+ movability : Some ( hir:: Movability :: Static ) ,
650+ constness : hir:: Constness :: NotConst ,
651+ } ) )
652+ }
663653
664- let hir_id = self . lower_node_id ( closure_node_id) ;
654+ /// Forwards a possible `#[track_caller]` annotation from `outer_hir_id` to
655+ /// `inner_hir_id` in case the `closure_track_caller` feature is enabled.
656+ pub ( super ) fn maybe_forward_track_caller (
657+ & mut self ,
658+ span : Span ,
659+ outer_hir_id : hir:: HirId ,
660+ inner_hir_id : hir:: HirId ,
661+ ) {
665662 if self . tcx . features ( ) . closure_track_caller
666663 && let Some ( attrs) = self . attrs . get ( & outer_hir_id. local_id )
667664 && attrs. into_iter ( ) . any ( |attr| attr. has_name ( sym:: track_caller) )
668665 {
669666 let unstable_span =
670667 self . mark_span_with_reason ( DesugaringKind :: Async , span, self . allow_gen_future . clone ( ) ) ;
671668 self . lower_attrs (
672- hir_id ,
669+ inner_hir_id ,
673670 & [ Attribute {
674671 kind : AttrKind :: Normal ( ptr:: P ( NormalAttr {
675672 item : AttrItem {
@@ -685,8 +682,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
685682 } ] ,
686683 ) ;
687684 }
688-
689- hir:: Expr { hir_id, kind : generator_kind, span : self . lower_span ( span) }
690685 }
691686
692687 /// Desugar `<expr>.await` into:
@@ -1001,15 +996,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
1001996 None
1002997 } ;
1003998
1004- this. make_async_expr (
999+ let async_body = this. make_async_expr (
10051000 capture_clause,
1006- closure_hir_id,
10071001 inner_closure_id,
10081002 async_ret_ty,
10091003 body. span ,
10101004 hir:: AsyncGeneratorKind :: Closure ,
10111005 |this| this. with_new_scopes ( |this| this. lower_expr_mut ( body) ) ,
1012- )
1006+ ) ;
1007+ let hir_id = this. lower_node_id ( inner_closure_id) ;
1008+ this. maybe_forward_track_caller ( body. span , closure_hir_id, hir_id) ;
1009+ hir:: Expr { hir_id, kind : async_body, span : this. lower_span ( body. span ) }
10131010 } ) ;
10141011 body_id
10151012 } ) ;
0 commit comments