@@ -63,20 +63,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
63
63
ExprKind :: ForLoop ( pat, head, body, opt_label) => {
64
64
return self . lower_expr_for ( e, pat, head, body, * opt_label) ;
65
65
}
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
- }
80
66
_ => ( ) ,
81
67
}
82
68
@@ -187,6 +173,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
187
173
self . arena . alloc_from_iter ( arms. iter ( ) . map ( |x| self . lower_arm ( x) ) ) ,
188
174
hir:: MatchSource :: Normal ,
189
175
) ,
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
+ ) ,
190
184
ExprKind :: Await ( expr) => {
191
185
let dot_await_span = if expr. span . hi ( ) < e. span . hi ( ) {
192
186
let span_with_whitespace = self
@@ -320,7 +314,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
320
314
) ,
321
315
ExprKind :: Try ( sub_expr) => self . lower_expr_try ( e. span , sub_expr) ,
322
316
323
- ExprKind :: Paren ( _) | ExprKind :: ForLoop ( ..) | ExprKind :: Async ( .. ) => {
317
+ ExprKind :: Paren ( _) | ExprKind :: ForLoop ( ..) => {
324
318
unreachable ! ( "already handled" )
325
319
}
326
320
@@ -591,13 +585,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
591
585
pub ( super ) fn make_async_expr (
592
586
& mut self ,
593
587
capture_clause : CaptureBy ,
594
- outer_hir_id : hir:: HirId ,
595
588
closure_node_id : NodeId ,
596
589
ret_ty : Option < hir:: FnRetTy < ' hir > > ,
597
590
span : Span ,
598
591
async_gen_kind : hir:: AsyncGeneratorKind ,
599
592
body : impl FnOnce ( & mut Self ) -> hir:: Expr < ' hir > ,
600
- ) -> hir:: Expr < ' hir > {
593
+ ) -> hir:: ExprKind < ' hir > {
601
594
let output = ret_ty. unwrap_or_else ( || hir:: FnRetTy :: DefaultReturn ( self . lower_span ( span) ) ) ;
602
595
603
596
// Resume argument type: `ResumeTy`
@@ -644,32 +637,36 @@ impl<'hir> LoweringContext<'_, 'hir> {
644
637
} ) ;
645
638
646
639
// `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
+ }
663
653
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
+ ) {
665
662
if self . tcx . features ( ) . closure_track_caller
666
663
&& let Some ( attrs) = self . attrs . get ( & outer_hir_id. local_id )
667
664
&& attrs. into_iter ( ) . any ( |attr| attr. has_name ( sym:: track_caller) )
668
665
{
669
666
let unstable_span =
670
667
self . mark_span_with_reason ( DesugaringKind :: Async , span, self . allow_gen_future . clone ( ) ) ;
671
668
self . lower_attrs (
672
- hir_id ,
669
+ inner_hir_id ,
673
670
& [ Attribute {
674
671
kind : AttrKind :: Normal ( ptr:: P ( NormalAttr {
675
672
item : AttrItem {
@@ -685,8 +682,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
685
682
} ] ,
686
683
) ;
687
684
}
688
-
689
- hir:: Expr { hir_id, kind : generator_kind, span : self . lower_span ( span) }
690
685
}
691
686
692
687
/// Desugar `<expr>.await` into:
@@ -1001,15 +996,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
1001
996
None
1002
997
} ;
1003
998
1004
- this. make_async_expr (
999
+ let async_body = this. make_async_expr (
1005
1000
capture_clause,
1006
- closure_hir_id,
1007
1001
inner_closure_id,
1008
1002
async_ret_ty,
1009
1003
body. span ,
1010
1004
hir:: AsyncGeneratorKind :: Closure ,
1011
1005
|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 ) }
1013
1010
} ) ;
1014
1011
body_id
1015
1012
} ) ;
0 commit comments