Skip to content

Commit 59f43bd

Browse files
committed
Do not preallocate item HirIds.
1 parent d2dfb0e commit 59f43bd

File tree

3 files changed

+20
-47
lines changed

3 files changed

+20
-47
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
5555
}
5656
}
5757
StmtKind::Item(ref it) => {
58-
stmts.extend(self.lower_item_id(it).into_iter().enumerate().map(
58+
stmts.extend(self.lower_item_ref(it).into_iter().enumerate().map(
5959
|(i, item_id)| {
6060
let hir_id = match i {
6161
0 => self.lower_node_id(s.id),

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

+14-25
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl ItemLowerer<'_, '_, '_> {
4040

4141
impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
4242
fn visit_item(&mut self, item: &'a Item) {
43+
self.lctx.allocate_hir_id_counter(item.id);
4344
let hir_id = self.lctx.with_hir_id_owner(item.id, |lctx| {
4445
lctx.without_in_scope_lifetime_defs(|lctx| {
4546
let hir_item = lctx.lower_item(item);
@@ -77,6 +78,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
7778
}
7879

7980
fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
81+
self.lctx.allocate_hir_id_counter(item.id);
8082
self.lctx.with_hir_id_owner(item.id, |lctx| match ctxt {
8183
AssocCtxt::Trait => {
8284
let hir_item = lctx.lower_trait_item(item);
@@ -154,41 +156,28 @@ impl<'hir> LoweringContext<'_, 'hir> {
154156
pub(super) fn lower_mod(&mut self, items: &[P<Item>], inner: Span) -> hir::Mod<'hir> {
155157
hir::Mod {
156158
inner: self.lower_span(inner),
157-
item_ids: self.arena.alloc_from_iter(items.iter().flat_map(|x| self.lower_item_id(x))),
159+
item_ids: self.arena.alloc_from_iter(items.iter().flat_map(|x| self.lower_item_ref(x))),
158160
}
159161
}
160162

161-
pub(super) fn lower_item_id(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
162-
let node_ids = match i.kind {
163-
ItemKind::Use(ref use_tree) => {
164-
let mut vec = smallvec![i.id];
165-
self.lower_item_id_use_tree(use_tree, i.id, &mut vec);
166-
vec
167-
}
168-
ItemKind::Fn(..) | ItemKind::Impl(box ImplKind { of_trait: None, .. }) => {
169-
smallvec![i.id]
170-
}
171-
_ => smallvec![i.id],
172-
};
173-
163+
pub(super) fn lower_item_ref(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
164+
let mut node_ids = smallvec![hir::ItemId { def_id: self.resolver.local_def_id(i.id) }];
165+
if let ItemKind::Use(ref use_tree) = &i.kind {
166+
self.lower_item_id_use_tree(use_tree, i.id, &mut node_ids);
167+
}
174168
node_ids
175-
.into_iter()
176-
.map(|node_id| hir::ItemId {
177-
def_id: self.allocate_hir_id_counter(node_id).expect_owner(),
178-
})
179-
.collect()
180169
}
181170

182171
fn lower_item_id_use_tree(
183172
&mut self,
184173
tree: &UseTree,
185174
base_id: NodeId,
186-
vec: &mut SmallVec<[NodeId; 1]>,
175+
vec: &mut SmallVec<[hir::ItemId; 1]>,
187176
) {
188177
match tree.kind {
189178
UseTreeKind::Nested(ref nested_vec) => {
190179
for &(ref nested, id) in nested_vec {
191-
vec.push(id);
180+
vec.push(hir::ItemId { def_id: self.resolver.local_def_id(id) });
192181
self.lower_item_id_use_tree(nested, id, vec);
193182
}
194183
}
@@ -197,7 +186,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
197186
for (_, &id) in
198187
iter::zip(self.expect_full_res_from_use(base_id).skip(1), &[id1, id2])
199188
{
200-
vec.push(id);
189+
vec.push(hir::ItemId { def_id: self.resolver.local_def_id(id) });
201190
}
202191
}
203192
}
@@ -700,7 +689,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
700689

701690
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef<'hir> {
702691
hir::ForeignItemRef {
703-
id: hir::ForeignItemId { def_id: self.lower_node_id(i.id).expect_owner() },
692+
id: hir::ForeignItemId { def_id: self.allocate_hir_id_counter(i.id) },
704693
ident: self.lower_ident(i.ident),
705694
span: self.lower_span(i.span),
706695
vis: self.lower_visibility(&i.vis, Some(i.id)),
@@ -842,7 +831,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
842831
}
843832
AssocItemKind::MacCall(..) => unimplemented!(),
844833
};
845-
let id = hir::TraitItemId { def_id: self.lower_node_id(i.id).expect_owner() };
834+
let id = hir::TraitItemId { def_id: self.resolver.local_def_id(i.id) };
846835
let defaultness = hir::Defaultness::Default { has_value: has_default };
847836
hir::TraitItemRef {
848837
id,
@@ -928,7 +917,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
928917
let has_value = true;
929918
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
930919
hir::ImplItemRef {
931-
id: hir::ImplItemId { def_id: self.lower_node_id(i.id).expect_owner() },
920+
id: hir::ImplItemId { def_id: self.allocate_hir_id_counter(i.id) },
932921
ident: self.lower_ident(i.ident),
933922
span: self.lower_span(i.span),
934923
vis: self.lower_visibility(&i.vis, Some(i.id)),

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

+5-21
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
use rustc_ast::node_id::NodeMap;
3939
use rustc_ast::token::{self, Token};
4040
use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, TokenStream, TokenTree};
41-
use rustc_ast::visit::{self, AssocCtxt, Visitor};
41+
use rustc_ast::visit::{self, Visitor};
4242
use rustc_ast::{self as ast, *};
4343
use rustc_ast_pretty::pprust;
4444
use rustc_data_structures::captures::Captures;
@@ -448,24 +448,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
448448

449449
impl<'tcx> Visitor<'tcx> for MiscCollector<'tcx, '_, '_> {
450450
fn visit_item(&mut self, item: &'tcx Item) {
451-
self.lctx.allocate_hir_id_counter(item.id);
452-
453451
if let ItemKind::Use(ref use_tree) = item.kind {
454452
self.allocate_use_tree_hir_id_counters(use_tree);
455453
}
456454

457455
visit::walk_item(self, item);
458456
}
459-
460-
fn visit_assoc_item(&mut self, item: &'tcx AssocItem, ctxt: AssocCtxt) {
461-
self.lctx.allocate_hir_id_counter(item.id);
462-
visit::walk_assoc_item(self, item, ctxt);
463-
}
464-
465-
fn visit_foreign_item(&mut self, item: &'tcx ForeignItem) {
466-
self.lctx.allocate_hir_id_counter(item.id);
467-
visit::walk_foreign_item(self, item);
468-
}
469457
}
470458

471459
self.lower_node_id(CRATE_NODE_ID);
@@ -554,13 +542,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
554542
id
555543
}
556544

557-
fn allocate_hir_id_counter(&mut self, owner: NodeId) -> hir::HirId {
545+
fn allocate_hir_id_counter(&mut self, owner: NodeId) -> LocalDefId {
558546
// Set up the counter if needed.
559547
self.item_local_id_counters.entry(owner).or_insert(0);
560548
// Always allocate the first `HirId` for the owner itself.
561549
let lowered = self.lower_node_id_with_owner(owner, owner);
562550
debug_assert_eq!(lowered.local_id.as_u32(), 0);
563-
lowered
551+
lowered.owner
564552
}
565553

566554
fn create_stable_hashing_context(&self) -> LoweringHasher<'_> {
@@ -1503,9 +1491,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15031491
// frequently opened issues show.
15041492
let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None);
15051493

1506-
let opaque_ty_def_id = self.resolver.local_def_id(opaque_ty_node_id);
1507-
1508-
self.allocate_hir_id_counter(opaque_ty_node_id);
1494+
let opaque_ty_def_id = self.allocate_hir_id_counter(opaque_ty_node_id);
15091495

15101496
let collected_lifetimes = self.with_hir_id_owner(opaque_ty_node_id, move |lctx| {
15111497
let hir_bounds = lower_bounds(lctx);
@@ -1762,9 +1748,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17621748

17631749
let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::Async, span, None);
17641750

1765-
let opaque_ty_def_id = self.resolver.local_def_id(opaque_ty_node_id);
1766-
1767-
self.allocate_hir_id_counter(opaque_ty_node_id);
1751+
let opaque_ty_def_id = self.allocate_hir_id_counter(opaque_ty_node_id);
17681752

17691753
// When we create the opaque type for this async fn, it is going to have
17701754
// to capture all the lifetimes involved in the signature (including in the

0 commit comments

Comments
 (0)