Skip to content

Commit f7f31d7

Browse files
committed
Lower AST node id only once
1 parent 3afb2bb commit f7f31d7

File tree

5 files changed

+158
-104
lines changed

5 files changed

+158
-104
lines changed

Diff for: compiler/rustc_ast_lowering/src/block.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,27 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1010
b: &Block,
1111
targeted_by_break: bool,
1212
) -> &'hir hir::Block<'hir> {
13-
self.arena.alloc(self.lower_block_noalloc(b, targeted_by_break))
13+
let hir_id = self.lower_node_id(b.id);
14+
self.arena.alloc(self.lower_block_noalloc(hir_id, b, targeted_by_break))
15+
}
16+
17+
pub(super) fn lower_block_with_hir_id(
18+
&mut self,
19+
b: &Block,
20+
hir_id: hir::HirId,
21+
targeted_by_break: bool,
22+
) -> &'hir hir::Block<'hir> {
23+
self.arena.alloc(self.lower_block_noalloc(hir_id, b, targeted_by_break))
1424
}
1525

1626
pub(super) fn lower_block_noalloc(
1727
&mut self,
28+
hir_id: hir::HirId,
1829
b: &Block,
1930
targeted_by_break: bool,
2031
) -> hir::Block<'hir> {
2132
let (stmts, expr) = self.lower_stmts(&b.stmts);
2233
let rules = self.lower_block_check_mode(&b.rules);
23-
let hir_id = self.lower_node_id(b.id);
2434
hir::Block { hir_id, stmts, expr, rules, span: self.lower_span(b.span), targeted_by_break }
2535
}
2636

Diff for: compiler/rustc_ast_lowering/src/expr.rs

+69-41
Original file line numberDiff line numberDiff line change
@@ -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,14 @@ 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+
hir::ExprKind::Block(
265+
self.lower_block_with_hir_id(blk, block_hir_id, opt_label.is_some()),
266+
opt_label,
267+
)
255268
}
256269
ExprKind::Assign(el, er, span) => self.lower_expr_assign(el, er, *span, e.span),
257270
ExprKind::AssignOp(op, el, er) => hir::ExprKind::AssignOp(
@@ -352,7 +365,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
352365
ExprKind::MacCall(_) => panic!("{:?} shouldn't exist here", e.span),
353366
};
354367

355-
hir::Expr { hir_id, kind, span: self.lower_span(e.span) }
368+
hir::Expr { hir_id: expr_hir_id, kind, span: self.lower_span(e.span) }
356369
})
357370
}
358371

@@ -502,16 +515,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
502515
let if_expr = self.expr(span, if_kind);
503516
let block = self.block_expr(self.arena.alloc(if_expr));
504517
let span = self.lower_span(span.with_hi(cond.span.hi()));
505-
let opt_label = self.lower_label(opt_label);
506518
hir::ExprKind::Loop(block, opt_label, hir::LoopSource::While, span)
507519
}
508520

509521
/// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_output(<expr>) }`,
510522
/// `try { <stmts>; }` into `{ <stmts>; ::std::ops::Try::from_output(()) }`
511523
/// and save the block id to use it as a break target for desugaring of the `?` operator.
512524
fn lower_expr_try_block(&mut self, body: &Block) -> hir::ExprKind<'hir> {
513-
self.with_catch_scope(body.id, |this| {
514-
let mut block = this.lower_block_noalloc(body, true);
525+
let body_hir_id = self.lower_node_id(body.id);
526+
self.with_catch_scope(body_hir_id, |this| {
527+
let mut block = this.lower_block_noalloc(body_hir_id, body, true);
515528

516529
// Final expression of the block (if present) or `()` with span at the end of block
517530
let (try_span, tail_expr) = if let Some(expr) = block.expr.take() {
@@ -869,7 +882,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
869882
let x_expr = self.expr_ident(gen_future_span, x_ident, x_pat_hid);
870883
let ready_field = self.single_pat_field(gen_future_span, x_pat);
871884
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| {
873886
let expr_break =
874887
hir::ExprKind::Break(this.lower_loop_destination(None), Some(x_expr));
875888
this.arena.alloc(this.expr(gen_future_span, expr_break))
@@ -1462,26 +1475,37 @@ impl<'hir> LoweringContext<'_, 'hir> {
14621475
)
14631476
}
14641477

1465-
fn lower_label(&self, opt_label: Option<Label>) -> Option<Label> {
1478+
// Record labelled expr's HirId so that we can retrieve it in `lower_jump_destination` without
1479+
// lowering node id again.
1480+
fn lower_label(
1481+
&mut self,
1482+
opt_label: Option<Label>,
1483+
dest_id: NodeId,
1484+
dest_hir_id: hir::HirId,
1485+
) -> Option<Label> {
14661486
let label = opt_label?;
1487+
self.labelled_node_id_to_local_id.insert(dest_id, dest_hir_id.local_id);
14671488
Some(Label { ident: self.lower_ident(label.ident) })
14681489
}
14691490

14701491
fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
14711492
let target_id = match destination {
14721493
Some((id, _)) => {
14731494
if let Some(loop_id) = self.resolver.get_label_res(id) {
1474-
Ok(self.lower_node_id(loop_id))
1495+
let local_id = self.labelled_node_id_to_local_id[&loop_id];
1496+
let loop_hir_id = HirId { owner: self.current_hir_id_owner, local_id };
1497+
Ok(loop_hir_id)
14751498
} else {
14761499
Err(hir::LoopIdError::UnresolvedLabel)
14771500
}
14781501
}
1479-
None => self
1480-
.loop_scope
1481-
.map(|id| Ok(self.lower_node_id(id)))
1482-
.unwrap_or(Err(hir::LoopIdError::OutsideLoopScope)),
1502+
None => {
1503+
self.loop_scope.map(|id| Ok(id)).unwrap_or(Err(hir::LoopIdError::OutsideLoopScope))
1504+
}
14831505
};
1484-
let label = self.lower_label(destination.map(|(_, label)| label));
1506+
let label = destination
1507+
.map(|(_, label)| label)
1508+
.map(|label| Label { ident: self.lower_ident(label.ident) });
14851509
hir::Destination { label, target_id }
14861510
}
14871511

@@ -1496,14 +1520,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
14961520
}
14971521
}
14981522

1499-
fn with_catch_scope<T>(&mut self, catch_id: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
1523+
fn with_catch_scope<T>(&mut self, catch_id: hir::HirId, f: impl FnOnce(&mut Self) -> T) -> T {
15001524
let old_scope = self.catch_scope.replace(catch_id);
15011525
let result = f(self);
15021526
self.catch_scope = old_scope;
15031527
result
15041528
}
15051529

1506-
fn with_loop_scope<T>(&mut self, loop_id: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
1530+
fn with_loop_scope<T>(&mut self, loop_id: hir::HirId, f: impl FnOnce(&mut Self) -> T) -> T {
15071531
// We're no longer in the base loop's condition; we're in another loop.
15081532
let was_in_loop_condition = self.is_in_loop_condition;
15091533
self.is_in_loop_condition = false;
@@ -1655,17 +1679,22 @@ impl<'hir> LoweringContext<'_, 'hir> {
16551679
let head_span = self.mark_span_with_reason(DesugaringKind::ForLoop, head.span, None);
16561680
let pat_span = self.mark_span_with_reason(DesugaringKind::ForLoop, pat.span, None);
16571681

1682+
let loop_hir_id = self.lower_node_id(e.id);
1683+
let label = self.lower_label(opt_label, e.id, loop_hir_id);
1684+
16581685
// `None => break`
16591686
let none_arm = {
1660-
let break_expr = self.with_loop_scope(e.id, |this| this.expr_break_alloc(for_span));
1687+
let break_expr =
1688+
self.with_loop_scope(loop_hir_id, |this| this.expr_break_alloc(for_span));
16611689
let pat = self.pat_none(for_span);
16621690
self.arm(pat, break_expr)
16631691
};
16641692

16651693
// Some(<pat>) => <body>,
16661694
let some_arm = {
16671695
let some_pat = self.pat_some(pat_span, pat);
1668-
let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body, false));
1696+
let body_block =
1697+
self.with_loop_scope(loop_hir_id, |this| this.lower_block(body, false));
16691698
let body_expr = self.arena.alloc(self.expr_block(body_block));
16701699
self.arm(some_pat, body_expr)
16711700
};
@@ -1719,12 +1748,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
17191748
// `[opt_ident]: loop { ... }`
17201749
let kind = hir::ExprKind::Loop(
17211750
loop_block,
1722-
self.lower_label(opt_label),
1751+
label,
17231752
hir::LoopSource::ForLoop,
17241753
self.lower_span(for_span.with_hi(head.span.hi())),
17251754
);
1726-
let loop_expr =
1727-
self.arena.alloc(hir::Expr { hir_id: self.lower_node_id(e.id), kind, span: for_span });
1755+
let loop_expr = self.arena.alloc(hir::Expr { hir_id: loop_hir_id, kind, span: for_span });
17281756

17291757
// `mut iter => { ... }`
17301758
let iter_arm = self.arm(iter_pat, loop_expr);
@@ -1864,8 +1892,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
18641892
self.arena.alloc(residual_expr),
18651893
unstable_span,
18661894
);
1867-
let ret_expr = if let Some(catch_node) = self.catch_scope {
1868-
let target_id = Ok(self.lower_node_id(catch_node));
1895+
let ret_expr = if let Some(catch_id) = self.catch_scope {
1896+
let target_id = Ok(catch_id);
18691897
self.arena.alloc(self.expr(
18701898
try_span,
18711899
hir::ExprKind::Break(
@@ -1919,8 +1947,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
19191947
yeeted_span,
19201948
);
19211949

1922-
if let Some(catch_node) = self.catch_scope {
1923-
let target_id = Ok(self.lower_node_id(catch_node));
1950+
if let Some(catch_id) = self.catch_scope {
1951+
let target_id = Ok(catch_id);
19241952
hir::ExprKind::Break(hir::Destination { label: None, target_id }, Some(from_yeet_expr))
19251953
} else {
19261954
hir::ExprKind::Ret(Some(from_yeet_expr))

Diff for: compiler/rustc_ast_lowering/src/item.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
154154
fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
155155
let mut ident = i.ident;
156156
let vis_span = self.lower_span(i.vis.span);
157-
let hir_id = self.lower_node_id(i.id);
157+
let hir_id =
158+
hir::HirId { owner: self.current_hir_id_owner, local_id: hir::ItemLocalId::ZERO };
158159
let attrs = self.lower_attrs(hir_id, &i.attrs);
159160
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, vis_span, &i.kind);
160161
let item = hir::Item {
@@ -658,7 +659,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
658659
}
659660

660661
fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
661-
let hir_id = self.lower_node_id(i.id);
662+
let hir_id =
663+
hir::HirId { owner: self.current_hir_id_owner, local_id: hir::ItemLocalId::ZERO };
662664
let owner_id = hir_id.expect_owner();
663665
self.lower_attrs(hir_id, &i.attrs);
664666
let item = hir::ForeignItem {
@@ -786,7 +788,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
786788
i: &AssocItem,
787789
trait_constness: Const,
788790
) -> &'hir hir::TraitItem<'hir> {
789-
let hir_id = self.lower_node_id(i.id);
791+
let hir_id =
792+
hir::HirId { owner: self.current_hir_id_owner, local_id: hir::ItemLocalId::ZERO };
790793
self.lower_attrs(hir_id, &i.attrs);
791794
let trait_item_def_id = hir_id.expect_owner();
792795

@@ -926,7 +929,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
926929
// Since `default impl` is not yet implemented, this is always true in impls.
927930
let has_value = true;
928931
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
929-
let hir_id = self.lower_node_id(i.id);
932+
let hir_id =
933+
hir::HirId { owner: self.current_hir_id_owner, local_id: hir::ItemLocalId::ZERO };
930934
self.lower_attrs(hir_id, &i.attrs);
931935

932936
let (generics, kind) = match &i.kind {
@@ -1166,7 +1170,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11661170
);
11671171

11681172
// FIXME(async_fn_track_caller): Can this be moved above?
1169-
let hir_id = this.lower_node_id(coroutine_kind.closure_id());
1173+
let hir_id = expr.hir_id;
11701174
this.maybe_forward_track_caller(body.span, fn_id, hir_id);
11711175

11721176
(parameters, expr)

0 commit comments

Comments
 (0)