@@ -70,8 +70,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
7070 _ => ( ) ,
7171 }
7272
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 ) ;
7575
7676 let kind = match & e. kind {
7777 ExprKind :: Array ( exprs) => hir:: ExprKind :: Array ( self . lower_exprs ( exprs) ) ,
@@ -175,18 +175,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
175175 ExprKind :: If ( cond, then, else_opt) => {
176176 self . lower_expr_if ( cond, then, else_opt. as_deref ( ) )
177177 }
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+ }
190197 ExprKind :: TryBlock ( body) => self . lower_expr_try_block ( body) ,
191198 ExprKind :: Match ( expr, arms, kind) => hir:: ExprKind :: Match (
192199 self . lower_expr ( expr) ,
@@ -212,7 +219,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
212219 binder,
213220 * capture_clause,
214221 e. id ,
215- hir_id ,
222+ expr_hir_id ,
216223 * coroutine_kind,
217224 fn_decl,
218225 body,
@@ -223,7 +230,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
223230 binder,
224231 * capture_clause,
225232 e. id ,
226- hir_id ,
233+ expr_hir_id ,
227234 * constness,
228235 * movability,
229236 fn_decl,
@@ -250,8 +257,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
250257 )
251258 }
252259 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+ let hir_block = self . arena . alloc ( self . lower_block_noalloc (
265+ block_hir_id,
266+ blk,
267+ opt_label. is_some ( ) ,
268+ ) ) ;
269+ hir:: ExprKind :: Block ( hir_block, opt_label)
255270 }
256271 ExprKind :: Assign ( el, er, span) => self . lower_expr_assign ( el, er, * span, e. span ) ,
257272 ExprKind :: AssignOp ( op, el, er) => hir:: ExprKind :: AssignOp (
@@ -354,7 +369,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
354369 ExprKind :: MacCall ( _) => panic ! ( "{:?} shouldn't exist here" , e. span) ,
355370 } ;
356371
357- hir:: Expr { hir_id, kind, span : self . lower_span ( e. span ) }
372+ hir:: Expr { hir_id : expr_hir_id , kind, span : self . lower_span ( e. span ) }
358373 } )
359374 }
360375
@@ -504,16 +519,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
504519 let if_expr = self . expr ( span, if_kind) ;
505520 let block = self . block_expr ( self . arena . alloc ( if_expr) ) ;
506521 let span = self . lower_span ( span. with_hi ( cond. span . hi ( ) ) ) ;
507- let opt_label = self . lower_label ( opt_label) ;
508522 hir:: ExprKind :: Loop ( block, opt_label, hir:: LoopSource :: While , span)
509523 }
510524
511525 /// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_output(<expr>) }`,
512526 /// `try { <stmts>; }` into `{ <stmts>; ::std::ops::Try::from_output(()) }`
513527 /// and save the block id to use it as a break target for desugaring of the `?` operator.
514528 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 ) ;
529+ let body_hir_id = self . lower_node_id ( body. id ) ;
530+ self . with_catch_scope ( body_hir_id, |this| {
531+ let mut block = this. lower_block_noalloc ( body_hir_id, body, true ) ;
517532
518533 // Final expression of the block (if present) or `()` with span at the end of block
519534 let ( try_span, tail_expr) = if let Some ( expr) = block. expr . take ( ) {
@@ -869,7 +884,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
869884 let x_expr = self . expr_ident ( gen_future_span, x_ident, x_pat_hid) ;
870885 let ready_field = self . single_pat_field ( gen_future_span, x_pat) ;
871886 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| {
887+ let break_x = self . with_loop_scope ( loop_hir_id , move |this| {
873888 let expr_break =
874889 hir:: ExprKind :: Break ( this. lower_loop_destination ( None ) , Some ( x_expr) ) ;
875890 this. arena . alloc ( this. expr ( gen_future_span, expr_break) )
@@ -1101,8 +1116,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11011116 hir:: CoroutineSource :: Closure ,
11021117 ) ;
11031118
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) ;
1119+ this. maybe_forward_track_caller ( body. span , closure_hir_id, expr. hir_id ) ;
11061120
11071121 ( parameters, expr)
11081122 } ) ;
@@ -1465,26 +1479,37 @@ impl<'hir> LoweringContext<'_, 'hir> {
14651479 )
14661480 }
14671481
1468- fn lower_label ( & self , opt_label : Option < Label > ) -> Option < Label > {
1482+ // Record labelled expr's HirId so that we can retrieve it in `lower_jump_destination` without
1483+ // lowering node id again.
1484+ fn lower_label (
1485+ & mut self ,
1486+ opt_label : Option < Label > ,
1487+ dest_id : NodeId ,
1488+ dest_hir_id : hir:: HirId ,
1489+ ) -> Option < Label > {
14691490 let label = opt_label?;
1491+ self . ident_and_label_to_local_id . insert ( dest_id, dest_hir_id. local_id ) ;
14701492 Some ( Label { ident : self . lower_ident ( label. ident ) } )
14711493 }
14721494
14731495 fn lower_loop_destination ( & mut self , destination : Option < ( NodeId , Label ) > ) -> hir:: Destination {
14741496 let target_id = match destination {
14751497 Some ( ( id, _) ) => {
14761498 if let Some ( loop_id) = self . resolver . get_label_res ( id) {
1477- Ok ( self . lower_node_id ( loop_id) )
1499+ let local_id = self . ident_and_label_to_local_id [ & loop_id] ;
1500+ let loop_hir_id = HirId { owner : self . current_hir_id_owner , local_id } ;
1501+ Ok ( loop_hir_id)
14781502 } else {
14791503 Err ( hir:: LoopIdError :: UnresolvedLabel )
14801504 }
14811505 }
1482- None => self
1483- . loop_scope
1484- . map ( |id| Ok ( self . lower_node_id ( id) ) )
1485- . unwrap_or ( Err ( hir:: LoopIdError :: OutsideLoopScope ) ) ,
1506+ None => {
1507+ self . loop_scope . map ( |id| Ok ( id) ) . unwrap_or ( Err ( hir:: LoopIdError :: OutsideLoopScope ) )
1508+ }
14861509 } ;
1487- let label = self . lower_label ( destination. map ( |( _, label) | label) ) ;
1510+ let label = destination
1511+ . map ( |( _, label) | label)
1512+ . map ( |label| Label { ident : self . lower_ident ( label. ident ) } ) ;
14881513 hir:: Destination { label, target_id }
14891514 }
14901515
@@ -1499,14 +1524,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
14991524 }
15001525 }
15011526
1502- fn with_catch_scope < T > ( & mut self , catch_id : NodeId , f : impl FnOnce ( & mut Self ) -> T ) -> T {
1527+ fn with_catch_scope < T > ( & mut self , catch_id : hir :: HirId , f : impl FnOnce ( & mut Self ) -> T ) -> T {
15031528 let old_scope = self . catch_scope . replace ( catch_id) ;
15041529 let result = f ( self ) ;
15051530 self . catch_scope = old_scope;
15061531 result
15071532 }
15081533
1509- fn with_loop_scope < T > ( & mut self , loop_id : NodeId , f : impl FnOnce ( & mut Self ) -> T ) -> T {
1534+ fn with_loop_scope < T > ( & mut self , loop_id : hir :: HirId , f : impl FnOnce ( & mut Self ) -> T ) -> T {
15101535 // We're no longer in the base loop's condition; we're in another loop.
15111536 let was_in_loop_condition = self . is_in_loop_condition ;
15121537 self . is_in_loop_condition = false ;
@@ -1658,17 +1683,22 @@ impl<'hir> LoweringContext<'_, 'hir> {
16581683 let head_span = self . mark_span_with_reason ( DesugaringKind :: ForLoop , head. span , None ) ;
16591684 let pat_span = self . mark_span_with_reason ( DesugaringKind :: ForLoop , pat. span , None ) ;
16601685
1686+ let loop_hir_id = self . lower_node_id ( e. id ) ;
1687+ let label = self . lower_label ( opt_label, e. id , loop_hir_id) ;
1688+
16611689 // `None => break`
16621690 let none_arm = {
1663- let break_expr = self . with_loop_scope ( e. id , |this| this. expr_break_alloc ( for_span) ) ;
1691+ let break_expr =
1692+ self . with_loop_scope ( loop_hir_id, |this| this. expr_break_alloc ( for_span) ) ;
16641693 let pat = self . pat_none ( for_span) ;
16651694 self . arm ( pat, break_expr)
16661695 } ;
16671696
16681697 // Some(<pat>) => <body>,
16691698 let some_arm = {
16701699 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 ) ) ;
1700+ let body_block =
1701+ self . with_loop_scope ( loop_hir_id, |this| this. lower_block ( body, false ) ) ;
16721702 let body_expr = self . arena . alloc ( self . expr_block ( body_block) ) ;
16731703 self . arm ( some_pat, body_expr)
16741704 } ;
@@ -1722,12 +1752,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
17221752 // `[opt_ident]: loop { ... }`
17231753 let kind = hir:: ExprKind :: Loop (
17241754 loop_block,
1725- self . lower_label ( opt_label ) ,
1755+ label ,
17261756 hir:: LoopSource :: ForLoop ,
17271757 self . lower_span ( for_span. with_hi ( head. span . hi ( ) ) ) ,
17281758 ) ;
1729- let loop_expr =
1730- self . arena . alloc ( hir:: Expr { hir_id : self . lower_node_id ( e. id ) , kind, span : for_span } ) ;
1759+ let loop_expr = self . arena . alloc ( hir:: Expr { hir_id : loop_hir_id, kind, span : for_span } ) ;
17311760
17321761 // `mut iter => { ... }`
17331762 let iter_arm = self . arm ( iter_pat, loop_expr) ;
@@ -1867,8 +1896,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
18671896 self . arena . alloc ( residual_expr) ,
18681897 unstable_span,
18691898 ) ;
1870- let ret_expr = if let Some ( catch_node ) = self . catch_scope {
1871- let target_id = Ok ( self . lower_node_id ( catch_node ) ) ;
1899+ let ret_expr = if let Some ( catch_id ) = self . catch_scope {
1900+ let target_id = Ok ( catch_id ) ;
18721901 self . arena . alloc ( self . expr (
18731902 try_span,
18741903 hir:: ExprKind :: Break (
@@ -1922,8 +1951,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
19221951 yeeted_span,
19231952 ) ;
19241953
1925- if let Some ( catch_node ) = self . catch_scope {
1926- let target_id = Ok ( self . lower_node_id ( catch_node ) ) ;
1954+ if let Some ( catch_id ) = self . catch_scope {
1955+ let target_id = Ok ( catch_id ) ;
19271956 hir:: ExprKind :: Break ( hir:: Destination { label : None , target_id } , Some ( from_yeet_expr) )
19281957 } else {
19291958 hir:: ExprKind :: Ret ( Some ( from_yeet_expr) )
0 commit comments