Skip to content

Commit cb08e08

Browse files
committed
Lower AST node id only once
1 parent df4ca44 commit cb08e08

File tree

7 files changed

+184
-131
lines changed

7 files changed

+184
-131
lines changed

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

compiler/rustc_ast_lowering/src/delegation.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
259259
self_param_id: pat_node_id,
260260
};
261261
self_resolver.visit_block(block);
262+
// Target expr needs to lower `self` path.
263+
this.ident_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
262264
this.lower_target_expr(&block)
263265
} else {
264-
let pat_hir_id = this.lower_node_id(pat_node_id);
265-
this.generate_arg(pat_hir_id, span)
266+
this.generate_arg(param.pat.hir_id, span)
266267
};
267268
args.push(arg);
268269
}

compiler/rustc_ast_lowering/src/expr.rs

+70-43
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(
@@ -354,7 +367,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
354367
ExprKind::MacCall(_) => panic!("{:?} shouldn't exist here", e.span),
355368
};
356369

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) }
358371
})
359372
}
360373

@@ -504,16 +517,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
504517
let if_expr = self.expr(span, if_kind);
505518
let block = self.block_expr(self.arena.alloc(if_expr));
506519
let span = self.lower_span(span.with_hi(cond.span.hi()));
507-
let opt_label = self.lower_label(opt_label);
508520
hir::ExprKind::Loop(block, opt_label, hir::LoopSource::While, span)
509521
}
510522

511523
/// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_output(<expr>) }`,
512524
/// `try { <stmts>; }` into `{ <stmts>; ::std::ops::Try::from_output(()) }`
513525
/// and save the block id to use it as a break target for desugaring of the `?` operator.
514526
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);
517530

518531
// Final expression of the block (if present) or `()` with span at the end of block
519532
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))
@@ -1101,8 +1114,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11011114
hir::CoroutineSource::Closure,
11021115
);
11031116

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);
11061118

11071119
(parameters, expr)
11081120
});
@@ -1465,26 +1477,37 @@ impl<'hir> LoweringContext<'_, 'hir> {
14651477
)
14661478
}
14671479

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> {
14691488
let label = opt_label?;
1489+
self.labelled_node_id_to_local_id.insert(dest_id, dest_hir_id.local_id);
14701490
Some(Label { ident: self.lower_ident(label.ident) })
14711491
}
14721492

14731493
fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
14741494
let target_id = match destination {
14751495
Some((id, _)) => {
14761496
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)
14781500
} else {
14791501
Err(hir::LoopIdError::UnresolvedLabel)
14801502
}
14811503
}
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+
}
14861507
};
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) });
14881511
hir::Destination { label, target_id }
14891512
}
14901513

@@ -1499,14 +1522,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
14991522
}
15001523
}
15011524

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 {
15031526
let old_scope = self.catch_scope.replace(catch_id);
15041527
let result = f(self);
15051528
self.catch_scope = old_scope;
15061529
result
15071530
}
15081531

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 {
15101533
// We're no longer in the base loop's condition; we're in another loop.
15111534
let was_in_loop_condition = self.is_in_loop_condition;
15121535
self.is_in_loop_condition = false;
@@ -1658,17 +1681,22 @@ impl<'hir> LoweringContext<'_, 'hir> {
16581681
let head_span = self.mark_span_with_reason(DesugaringKind::ForLoop, head.span, None);
16591682
let pat_span = self.mark_span_with_reason(DesugaringKind::ForLoop, pat.span, None);
16601683

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+
16611687
// `None => break`
16621688
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));
16641691
let pat = self.pat_none(for_span);
16651692
self.arm(pat, break_expr)
16661693
};
16671694

16681695
// Some(<pat>) => <body>,
16691696
let some_arm = {
16701697
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));
16721700
let body_expr = self.arena.alloc(self.expr_block(body_block));
16731701
self.arm(some_pat, body_expr)
16741702
};
@@ -1722,12 +1750,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
17221750
// `[opt_ident]: loop { ... }`
17231751
let kind = hir::ExprKind::Loop(
17241752
loop_block,
1725-
self.lower_label(opt_label),
1753+
label,
17261754
hir::LoopSource::ForLoop,
17271755
self.lower_span(for_span.with_hi(head.span.hi())),
17281756
);
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 });
17311758

17321759
// `mut iter => { ... }`
17331760
let iter_arm = self.arm(iter_pat, loop_expr);
@@ -1867,8 +1894,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
18671894
self.arena.alloc(residual_expr),
18681895
unstable_span,
18691896
);
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);
18721899
self.arena.alloc(self.expr(
18731900
try_span,
18741901
hir::ExprKind::Break(
@@ -1922,8 +1949,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
19221949
yeeted_span,
19231950
);
19241951

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);
19271954
hir::ExprKind::Break(hir::Destination { label: None, target_id }, Some(from_yeet_expr))
19281955
} else {
19291956
hir::ExprKind::Ret(Some(from_yeet_expr))

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 {
@@ -604,7 +605,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
604605
}
605606

606607
fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
607-
let hir_id = self.lower_node_id(i.id);
608+
let hir_id =
609+
hir::HirId { owner: self.current_hir_id_owner, local_id: hir::ItemLocalId::ZERO };
608610
let owner_id = hir_id.expect_owner();
609611
self.lower_attrs(hir_id, &i.attrs);
610612
let item = hir::ForeignItem {
@@ -728,7 +730,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
728730
}
729731

730732
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
731-
let hir_id = self.lower_node_id(i.id);
733+
let hir_id =
734+
hir::HirId { owner: self.current_hir_id_owner, local_id: hir::ItemLocalId::ZERO };
732735
self.lower_attrs(hir_id, &i.attrs);
733736
let trait_item_def_id = hir_id.expect_owner();
734737

@@ -858,7 +861,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
858861
// Since `default impl` is not yet implemented, this is always true in impls.
859862
let has_value = true;
860863
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
861-
let hir_id = self.lower_node_id(i.id);
864+
let hir_id =
865+
hir::HirId { owner: self.current_hir_id_owner, local_id: hir::ItemLocalId::ZERO };
862866
self.lower_attrs(hir_id, &i.attrs);
863867

864868
let (generics, kind) = match &i.kind {
@@ -1086,7 +1090,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10861090
);
10871091

10881092
// FIXME(async_fn_track_caller): Can this be moved above?
1089-
let hir_id = this.lower_node_id(coroutine_kind.closure_id());
1093+
let hir_id = expr.hir_id;
10901094
this.maybe_forward_track_caller(body.span, fn_id, hir_id);
10911095

10921096
(parameters, expr)

0 commit comments

Comments
 (0)