@@ -70,8 +70,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
70
70
_ => ( ) ,
71
71
}
72
72
73
- let hir_id = self . lower_node_id ( e. id ) ;
74
- self . lower_attrs ( hir_id , & e. attrs ) ;
73
+ let expr_hir_id = self . lower_node_id ( e. id ) ;
74
+ self . lower_attrs ( expr_hir_id , & e. attrs ) ;
75
75
76
76
let kind = match & e. kind {
77
77
ExprKind :: Array ( exprs) => hir:: ExprKind :: Array ( self . lower_exprs ( exprs) ) ,
@@ -175,18 +175,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
175
175
ExprKind :: If ( cond, then, else_opt) => {
176
176
self . lower_expr_if ( cond, then, else_opt. as_deref ( ) )
177
177
}
178
- ExprKind :: While ( cond, body, opt_label) => self . with_loop_scope ( e. id , |this| {
179
- let span = this. mark_span_with_reason ( DesugaringKind :: WhileLoop , e. span , None ) ;
180
- this. lower_expr_while_in_loop_scope ( span, cond, body, * opt_label)
181
- } ) ,
182
- ExprKind :: Loop ( body, opt_label, span) => self . with_loop_scope ( e. id , |this| {
183
- hir:: ExprKind :: Loop (
184
- this. lower_block ( body, false ) ,
185
- this. lower_label ( * opt_label) ,
186
- hir:: LoopSource :: Loop ,
187
- this. lower_span ( * span) ,
188
- )
189
- } ) ,
178
+ ExprKind :: While ( cond, body, opt_label) => {
179
+ self . with_loop_scope ( expr_hir_id, |this| {
180
+ let span =
181
+ this. mark_span_with_reason ( DesugaringKind :: WhileLoop , e. span , None ) ;
182
+ let opt_label = this. lower_label ( * opt_label, e. id , expr_hir_id) ;
183
+ this. lower_expr_while_in_loop_scope ( span, cond, body, opt_label)
184
+ } )
185
+ }
186
+ ExprKind :: Loop ( body, opt_label, span) => {
187
+ self . with_loop_scope ( expr_hir_id, |this| {
188
+ let opt_label = this. lower_label ( * opt_label, e. id , expr_hir_id) ;
189
+ hir:: ExprKind :: Loop (
190
+ this. lower_block ( body, false ) ,
191
+ opt_label,
192
+ hir:: LoopSource :: Loop ,
193
+ this. lower_span ( * span) ,
194
+ )
195
+ } )
196
+ }
190
197
ExprKind :: TryBlock ( body) => self . lower_expr_try_block ( body) ,
191
198
ExprKind :: Match ( expr, arms, kind) => hir:: ExprKind :: Match (
192
199
self . lower_expr ( expr) ,
@@ -212,7 +219,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
212
219
binder,
213
220
* capture_clause,
214
221
e. id ,
215
- hir_id ,
222
+ expr_hir_id ,
216
223
* coroutine_kind,
217
224
fn_decl,
218
225
body,
@@ -223,7 +230,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
223
230
binder,
224
231
* capture_clause,
225
232
e. id ,
226
- hir_id ,
233
+ expr_hir_id ,
227
234
* constness,
228
235
* movability,
229
236
fn_decl,
@@ -250,8 +257,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
250
257
)
251
258
}
252
259
ExprKind :: Block ( blk, opt_label) => {
253
- let opt_label = self . lower_label ( * opt_label) ;
254
- hir:: ExprKind :: Block ( self . lower_block ( blk, opt_label. is_some ( ) ) , opt_label)
260
+ // Different from loops, label of block resolves to block id rather than
261
+ // expr node id.
262
+ let block_hir_id = self . lower_node_id ( blk. id ) ;
263
+ let opt_label = self . lower_label ( * opt_label, blk. id , block_hir_id) ;
264
+ hir:: ExprKind :: Block (
265
+ self . lower_block_with_hir_id ( blk, block_hir_id, opt_label. is_some ( ) ) ,
266
+ opt_label,
267
+ )
255
268
}
256
269
ExprKind :: Assign ( el, er, span) => self . lower_expr_assign ( el, er, * span, e. span ) ,
257
270
ExprKind :: AssignOp ( op, el, er) => hir:: ExprKind :: AssignOp (
@@ -354,7 +367,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
354
367
ExprKind :: MacCall ( _) => panic ! ( "{:?} shouldn't exist here" , e. span) ,
355
368
} ;
356
369
357
- hir:: Expr { hir_id, kind, span : self . lower_span ( e. span ) }
370
+ hir:: Expr { hir_id : expr_hir_id , kind, span : self . lower_span ( e. span ) }
358
371
} )
359
372
}
360
373
@@ -504,16 +517,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
504
517
let if_expr = self . expr ( span, if_kind) ;
505
518
let block = self . block_expr ( self . arena . alloc ( if_expr) ) ;
506
519
let span = self . lower_span ( span. with_hi ( cond. span . hi ( ) ) ) ;
507
- let opt_label = self . lower_label ( opt_label) ;
508
520
hir:: ExprKind :: Loop ( block, opt_label, hir:: LoopSource :: While , span)
509
521
}
510
522
511
523
/// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_output(<expr>) }`,
512
524
/// `try { <stmts>; }` into `{ <stmts>; ::std::ops::Try::from_output(()) }`
513
525
/// and save the block id to use it as a break target for desugaring of the `?` operator.
514
526
fn lower_expr_try_block ( & mut self , body : & Block ) -> hir:: ExprKind < ' hir > {
515
- self . with_catch_scope ( body. id , |this| {
516
- let mut block = this. lower_block_noalloc ( body, true ) ;
527
+ let body_hir_id = self . lower_node_id ( body. id ) ;
528
+ self . with_catch_scope ( body_hir_id, |this| {
529
+ let mut block = this. lower_block_noalloc ( body_hir_id, body, true ) ;
517
530
518
531
// Final expression of the block (if present) or `()` with span at the end of block
519
532
let ( try_span, tail_expr) = if let Some ( expr) = block. expr . take ( ) {
@@ -869,7 +882,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
869
882
let x_expr = self . expr_ident ( gen_future_span, x_ident, x_pat_hid) ;
870
883
let ready_field = self . single_pat_field ( gen_future_span, x_pat) ;
871
884
let ready_pat = self . pat_lang_item_variant ( span, hir:: LangItem :: PollReady , ready_field) ;
872
- let break_x = self . with_loop_scope ( loop_node_id , move |this| {
885
+ let break_x = self . with_loop_scope ( loop_hir_id , move |this| {
873
886
let expr_break =
874
887
hir:: ExprKind :: Break ( this. lower_loop_destination ( None ) , Some ( x_expr) ) ;
875
888
this. arena . alloc ( this. expr ( gen_future_span, expr_break) )
@@ -1101,8 +1114,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1101
1114
hir:: CoroutineSource :: Closure ,
1102
1115
) ;
1103
1116
1104
- let hir_id = this. lower_node_id ( coroutine_kind. closure_id ( ) ) ;
1105
- this. maybe_forward_track_caller ( body. span , closure_hir_id, hir_id) ;
1117
+ this. maybe_forward_track_caller ( body. span , closure_hir_id, expr. hir_id ) ;
1106
1118
1107
1119
( parameters, expr)
1108
1120
} ) ;
@@ -1465,26 +1477,37 @@ impl<'hir> LoweringContext<'_, 'hir> {
1465
1477
)
1466
1478
}
1467
1479
1468
- fn lower_label ( & self , opt_label : Option < Label > ) -> Option < Label > {
1480
+ // Record labelled expr's HirId so that we can retrieve it in `lower_jump_destination` without
1481
+ // lowering node id again.
1482
+ fn lower_label (
1483
+ & mut self ,
1484
+ opt_label : Option < Label > ,
1485
+ dest_id : NodeId ,
1486
+ dest_hir_id : hir:: HirId ,
1487
+ ) -> Option < Label > {
1469
1488
let label = opt_label?;
1489
+ self . labelled_node_id_to_local_id . insert ( dest_id, dest_hir_id. local_id ) ;
1470
1490
Some ( Label { ident : self . lower_ident ( label. ident ) } )
1471
1491
}
1472
1492
1473
1493
fn lower_loop_destination ( & mut self , destination : Option < ( NodeId , Label ) > ) -> hir:: Destination {
1474
1494
let target_id = match destination {
1475
1495
Some ( ( id, _) ) => {
1476
1496
if let Some ( loop_id) = self . resolver . get_label_res ( id) {
1477
- Ok ( self . lower_node_id ( loop_id) )
1497
+ let local_id = self . labelled_node_id_to_local_id [ & loop_id] ;
1498
+ let loop_hir_id = HirId { owner : self . current_hir_id_owner , local_id } ;
1499
+ Ok ( loop_hir_id)
1478
1500
} else {
1479
1501
Err ( hir:: LoopIdError :: UnresolvedLabel )
1480
1502
}
1481
1503
}
1482
- None => self
1483
- . loop_scope
1484
- . map ( |id| Ok ( self . lower_node_id ( id) ) )
1485
- . unwrap_or ( Err ( hir:: LoopIdError :: OutsideLoopScope ) ) ,
1504
+ None => {
1505
+ self . loop_scope . map ( |id| Ok ( id) ) . unwrap_or ( Err ( hir:: LoopIdError :: OutsideLoopScope ) )
1506
+ }
1486
1507
} ;
1487
- let label = self . lower_label ( destination. map ( |( _, label) | label) ) ;
1508
+ let label = destination
1509
+ . map ( |( _, label) | label)
1510
+ . map ( |label| Label { ident : self . lower_ident ( label. ident ) } ) ;
1488
1511
hir:: Destination { label, target_id }
1489
1512
}
1490
1513
@@ -1499,14 +1522,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
1499
1522
}
1500
1523
}
1501
1524
1502
- fn with_catch_scope < T > ( & mut self , catch_id : NodeId , f : impl FnOnce ( & mut Self ) -> T ) -> T {
1525
+ fn with_catch_scope < T > ( & mut self , catch_id : hir :: HirId , f : impl FnOnce ( & mut Self ) -> T ) -> T {
1503
1526
let old_scope = self . catch_scope . replace ( catch_id) ;
1504
1527
let result = f ( self ) ;
1505
1528
self . catch_scope = old_scope;
1506
1529
result
1507
1530
}
1508
1531
1509
- fn with_loop_scope < T > ( & mut self , loop_id : NodeId , f : impl FnOnce ( & mut Self ) -> T ) -> T {
1532
+ fn with_loop_scope < T > ( & mut self , loop_id : hir :: HirId , f : impl FnOnce ( & mut Self ) -> T ) -> T {
1510
1533
// We're no longer in the base loop's condition; we're in another loop.
1511
1534
let was_in_loop_condition = self . is_in_loop_condition ;
1512
1535
self . is_in_loop_condition = false ;
@@ -1658,17 +1681,22 @@ impl<'hir> LoweringContext<'_, 'hir> {
1658
1681
let head_span = self . mark_span_with_reason ( DesugaringKind :: ForLoop , head. span , None ) ;
1659
1682
let pat_span = self . mark_span_with_reason ( DesugaringKind :: ForLoop , pat. span , None ) ;
1660
1683
1684
+ let loop_hir_id = self . lower_node_id ( e. id ) ;
1685
+ let label = self . lower_label ( opt_label, e. id , loop_hir_id) ;
1686
+
1661
1687
// `None => break`
1662
1688
let none_arm = {
1663
- let break_expr = self . with_loop_scope ( e. id , |this| this. expr_break_alloc ( for_span) ) ;
1689
+ let break_expr =
1690
+ self . with_loop_scope ( loop_hir_id, |this| this. expr_break_alloc ( for_span) ) ;
1664
1691
let pat = self . pat_none ( for_span) ;
1665
1692
self . arm ( pat, break_expr)
1666
1693
} ;
1667
1694
1668
1695
// Some(<pat>) => <body>,
1669
1696
let some_arm = {
1670
1697
let some_pat = self . pat_some ( pat_span, pat) ;
1671
- let body_block = self . with_loop_scope ( e. id , |this| this. lower_block ( body, false ) ) ;
1698
+ let body_block =
1699
+ self . with_loop_scope ( loop_hir_id, |this| this. lower_block ( body, false ) ) ;
1672
1700
let body_expr = self . arena . alloc ( self . expr_block ( body_block) ) ;
1673
1701
self . arm ( some_pat, body_expr)
1674
1702
} ;
@@ -1722,12 +1750,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
1722
1750
// `[opt_ident]: loop { ... }`
1723
1751
let kind = hir:: ExprKind :: Loop (
1724
1752
loop_block,
1725
- self . lower_label ( opt_label ) ,
1753
+ label ,
1726
1754
hir:: LoopSource :: ForLoop ,
1727
1755
self . lower_span ( for_span. with_hi ( head. span . hi ( ) ) ) ,
1728
1756
) ;
1729
- let loop_expr =
1730
- self . arena . alloc ( hir:: Expr { hir_id : self . lower_node_id ( e. id ) , kind, span : for_span } ) ;
1757
+ let loop_expr = self . arena . alloc ( hir:: Expr { hir_id : loop_hir_id, kind, span : for_span } ) ;
1731
1758
1732
1759
// `mut iter => { ... }`
1733
1760
let iter_arm = self . arm ( iter_pat, loop_expr) ;
@@ -1867,8 +1894,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
1867
1894
self . arena . alloc ( residual_expr) ,
1868
1895
unstable_span,
1869
1896
) ;
1870
- let ret_expr = if let Some ( catch_node ) = self . catch_scope {
1871
- let target_id = Ok ( self . lower_node_id ( catch_node ) ) ;
1897
+ let ret_expr = if let Some ( catch_id ) = self . catch_scope {
1898
+ let target_id = Ok ( catch_id ) ;
1872
1899
self . arena . alloc ( self . expr (
1873
1900
try_span,
1874
1901
hir:: ExprKind :: Break (
@@ -1922,8 +1949,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
1922
1949
yeeted_span,
1923
1950
) ;
1924
1951
1925
- if let Some ( catch_node ) = self . catch_scope {
1926
- let target_id = Ok ( self . lower_node_id ( catch_node ) ) ;
1952
+ if let Some ( catch_id ) = self . catch_scope {
1953
+ let target_id = Ok ( catch_id ) ;
1927
1954
hir:: ExprKind :: Break ( hir:: Destination { label : None , target_id } , Some ( from_yeet_expr) )
1928
1955
} else {
1929
1956
hir:: ExprKind :: Ret ( Some ( from_yeet_expr) )
0 commit comments