@@ -31,6 +31,44 @@ impl<'hir> LoweringContext<'_, 'hir> {
31
31
32
32
pub ( super ) fn lower_expr_mut ( & mut self , e : & Expr ) -> hir:: Expr < ' hir > {
33
33
ensure_sufficient_stack ( || {
34
+ match & e. kind {
35
+ // Paranthesis expression does not have a HirId and is handled specially.
36
+ ExprKind :: Paren ( ex) => {
37
+ let mut ex = self . lower_expr_mut ( ex) ;
38
+ // Include parens in span, but only if it is a super-span.
39
+ if e. span . contains ( ex. span ) {
40
+ ex. span = self . lower_span ( e. span ) ;
41
+ }
42
+ // Merge attributes into the inner expression.
43
+ if !e. attrs . is_empty ( ) {
44
+ let old_attrs =
45
+ self . attrs . get ( & ex. hir_id . local_id ) . map ( |la| * la) . unwrap_or ( & [ ] ) ;
46
+ self . attrs . insert (
47
+ ex. hir_id . local_id ,
48
+ & * self . arena . alloc_from_iter (
49
+ e. attrs
50
+ . iter ( )
51
+ . map ( |a| self . lower_attr ( a) )
52
+ . chain ( old_attrs. iter ( ) . cloned ( ) ) ,
53
+ ) ,
54
+ ) ;
55
+ }
56
+ return ex;
57
+ }
58
+ // Desugar `ExprForLoop`
59
+ // from: `[opt_ident]: for <pat> in <head> <body>`
60
+ //
61
+ // This also needs special handling because the HirId of the returned `hir::Expr` will not
62
+ // correspond to the `e.id`, so `lower_expr_for` handles attribute lowering itself.
63
+ ExprKind :: ForLoop ( pat, head, body, opt_label) => {
64
+ return self . lower_expr_for ( e, pat, head, body, * opt_label) ;
65
+ }
66
+ _ => ( ) ,
67
+ }
68
+
69
+ let hir_id = self . lower_node_id ( e. id ) ;
70
+ self . lower_attrs ( hir_id, & e. attrs ) ;
71
+
34
72
let kind = match & e. kind {
35
73
ExprKind :: Box ( inner) => hir:: ExprKind :: Box ( self . lower_expr ( inner) ) ,
36
74
ExprKind :: Array ( exprs) => hir:: ExprKind :: Array ( self . lower_exprs ( exprs) ) ,
@@ -48,7 +86,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
48
86
if e. attrs . get ( 0 ) . map_or ( false , |a| a. has_name ( sym:: rustc_box) ) {
49
87
if let [ inner] = & args[ ..] && e. attrs . len ( ) == 1 {
50
88
let kind = hir:: ExprKind :: Box ( self . lower_expr ( & inner) ) ;
51
- let hir_id = self . lower_node_id ( e. id ) ;
52
89
return hir:: Expr { hir_id, kind, span : self . lower_span ( e. span ) } ;
53
90
} else {
54
91
self . tcx . sess . emit_err ( RustcBoxAttributeError { span : e. span } ) ;
@@ -147,7 +184,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
147
184
) ,
148
185
ExprKind :: Async ( capture_clause, closure_node_id, block) => self . make_async_expr (
149
186
* capture_clause,
150
- None ,
187
+ hir_id ,
151
188
* closure_node_id,
152
189
None ,
153
190
e. span ,
@@ -184,6 +221,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
184
221
binder,
185
222
* capture_clause,
186
223
e. id ,
224
+ hir_id,
187
225
* closure_id,
188
226
fn_decl,
189
227
body,
@@ -279,39 +317,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
279
317
ExprKind :: Yield ( opt_expr) => self . lower_expr_yield ( e. span , opt_expr. as_deref ( ) ) ,
280
318
ExprKind :: Err => hir:: ExprKind :: Err ,
281
319
ExprKind :: Try ( sub_expr) => self . lower_expr_try ( e. span , sub_expr) ,
282
- ExprKind :: Paren ( ex) => {
283
- let mut ex = self . lower_expr_mut ( ex) ;
284
- // Include parens in span, but only if it is a super-span.
285
- if e. span . contains ( ex. span ) {
286
- ex. span = self . lower_span ( e. span ) ;
287
- }
288
- // Merge attributes into the inner expression.
289
- if !e. attrs . is_empty ( ) {
290
- let old_attrs =
291
- self . attrs . get ( & ex. hir_id . local_id ) . map ( |la| * la) . unwrap_or ( & [ ] ) ;
292
- self . attrs . insert (
293
- ex. hir_id . local_id ,
294
- & * self . arena . alloc_from_iter (
295
- e. attrs
296
- . iter ( )
297
- . map ( |a| self . lower_attr ( a) )
298
- . chain ( old_attrs. iter ( ) . cloned ( ) ) ,
299
- ) ,
300
- ) ;
301
- }
302
- return ex;
303
- }
304
320
305
- // Desugar `ExprForLoop`
306
- // from: `[opt_ident]: for <pat> in <head> <body>`
307
- ExprKind :: ForLoop ( pat, head, body, opt_label) => {
308
- return self . lower_expr_for ( e, pat, head, body, * opt_label) ;
309
- }
321
+ ExprKind :: Paren ( _) | ExprKind :: ForLoop ( ..) => unreachable ! ( "already handled" ) ,
322
+
310
323
ExprKind :: MacCall ( _) => panic ! ( "{:?} shouldn't exist here" , e. span) ,
311
324
} ;
312
325
313
- let hir_id = self . lower_node_id ( e. id ) ;
314
- self . lower_attrs ( hir_id, & e. attrs ) ;
315
326
hir:: Expr { hir_id, kind, span : self . lower_span ( e. span ) }
316
327
} )
317
328
}
@@ -576,7 +587,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
576
587
pub ( super ) fn make_async_expr (
577
588
& mut self ,
578
589
capture_clause : CaptureBy ,
579
- outer_hir_id : Option < hir:: HirId > ,
590
+ outer_hir_id : hir:: HirId ,
580
591
closure_node_id : NodeId ,
581
592
ret_ty : Option < hir:: FnRetTy < ' hir > > ,
582
593
span : Span ,
@@ -669,8 +680,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
669
680
hir:: ExprKind :: Closure ( c)
670
681
} ;
671
682
672
- let track_caller = outer_hir_id
673
- . and_then ( |id| self . attrs . get ( & id. local_id ) )
683
+ let track_caller = self
684
+ . attrs
685
+ . get ( & outer_hir_id. local_id )
674
686
. map_or ( false , |attrs| attrs. into_iter ( ) . any ( |attr| attr. has_name ( sym:: track_caller) ) ) ;
675
687
676
688
let hir_id = self . lower_node_id ( closure_node_id) ;
@@ -985,6 +997,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
985
997
binder : & ClosureBinder ,
986
998
capture_clause : CaptureBy ,
987
999
closure_id : NodeId ,
1000
+ closure_hir_id : hir:: HirId ,
988
1001
inner_closure_id : NodeId ,
989
1002
decl : & FnDecl ,
990
1003
body : & Expr ,
@@ -1018,9 +1031,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1018
1031
1019
1032
let async_body = this. make_async_expr (
1020
1033
capture_clause,
1021
- // FIXME(nbdd0121): This should also use a proper HIR id so `#[track_caller]`
1022
- // can be applied on async closures as well.
1023
- None ,
1034
+ closure_hir_id,
1024
1035
inner_closure_id,
1025
1036
async_ret_ty,
1026
1037
body. span ,
0 commit comments