Skip to content

Commit f7c66fb

Browse files
committed
Allocate HIR id counters on demand
1 parent cb4ac71 commit f7c66fb

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

Diff for: src/librustc/hir/lowering.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ use rustc_data_structures::thin_vec::ThinVec;
5151
use rustc_data_structures::sync::Lrc;
5252

5353
use std::collections::{BTreeSet, BTreeMap};
54-
use std::fmt::Debug;
5554
use std::mem;
5655
use smallvec::SmallVec;
5756
use syntax::attr;
@@ -378,13 +377,13 @@ impl<'a> LoweringContext<'a> {
378377
Mark::root(),
379378
tree.prefix.span,
380379
);
381-
self.lctx.allocate_hir_id_counter(id, &tree);
380+
self.lctx.allocate_hir_id_counter(id);
382381
}
383382
}
384383
UseTreeKind::Glob => (),
385384
UseTreeKind::Nested(ref trees) => {
386385
for &(ref use_tree, id) in trees {
387-
let hir_id = self.lctx.allocate_hir_id_counter(id, &use_tree).hir_id;
386+
let hir_id = self.lctx.allocate_hir_id_counter(id).hir_id;
388387
self.allocate_use_tree_hir_id_counters(use_tree, hir_id.owner);
389388
}
390389
}
@@ -394,7 +393,7 @@ impl<'a> LoweringContext<'a> {
394393

395394
impl<'lcx, 'interner> Visitor<'lcx> for MiscCollector<'lcx, 'interner> {
396395
fn visit_item(&mut self, item: &'lcx Item) {
397-
let hir_id = self.lctx.allocate_hir_id_counter(item.id, item).hir_id;
396+
let hir_id = self.lctx.allocate_hir_id_counter(item.id).hir_id;
398397

399398
match item.node {
400399
ItemKind::Struct(_, ref generics)
@@ -423,12 +422,12 @@ impl<'a> LoweringContext<'a> {
423422
}
424423

425424
fn visit_trait_item(&mut self, item: &'lcx TraitItem) {
426-
self.lctx.allocate_hir_id_counter(item.id, item);
425+
self.lctx.allocate_hir_id_counter(item.id);
427426
visit::walk_trait_item(self, item);
428427
}
429428

430429
fn visit_impl_item(&mut self, item: &'lcx ImplItem) {
431-
self.lctx.allocate_hir_id_counter(item.id, item);
430+
self.lctx.allocate_hir_id_counter(item.id);
432431
visit::walk_impl_item(self, item);
433432
}
434433
}
@@ -557,15 +556,13 @@ impl<'a> LoweringContext<'a> {
557556
self.modules.get_mut(&self.current_module).unwrap().items.insert(id);
558557
}
559558

560-
fn allocate_hir_id_counter<T: Debug>(&mut self, owner: NodeId, debug: &T) -> LoweredNodeId {
561-
if self.item_local_id_counters.insert(owner, 0).is_some() {
562-
bug!(
563-
"Tried to allocate item_local_id_counter for {:?} twice",
564-
debug
565-
);
566-
}
559+
fn allocate_hir_id_counter(&mut self, owner: NodeId) -> LoweredNodeId {
560+
// Setup the counter if needed
561+
self.item_local_id_counters.entry(owner).or_insert(0);
567562
// Always allocate the first `HirId` for the owner itself.
568-
self.lower_node_id_with_owner(owner, owner)
563+
let lowered = self.lower_node_id_with_owner(owner, owner);
564+
debug_assert_eq!(lowered.hir_id.local_id.as_u32(), 0);
565+
lowered
569566
}
570567

571568
fn lower_node_id_generic<F>(&mut self, ast_node_id: NodeId, alloc_hir_id: F) -> LoweredNodeId
@@ -1417,7 +1414,7 @@ impl<'a> LoweringContext<'a> {
14171414
.opt_def_index(exist_ty_node_id)
14181415
.unwrap();
14191416

1420-
self.allocate_hir_id_counter(exist_ty_node_id, &"existential impl trait");
1417+
self.allocate_hir_id_counter(exist_ty_node_id);
14211418

14221419
let hir_bounds = self.with_hir_id_owner(exist_ty_node_id, lower_bounds);
14231420

@@ -3495,9 +3492,7 @@ impl<'a> LoweringContext<'a> {
34953492
};
34963493

34973494
node_ids.into_iter().map(|node_id| hir::ItemId {
3498-
id: self.lower_node_id_generic(node_id, |_| {
3499-
panic!("expected node_id to be lowered already {:#?}", i)
3500-
}).hir_id
3495+
id: self.allocate_hir_id_counter(node_id).hir_id
35013496
}).collect()
35023497
}
35033498

0 commit comments

Comments
 (0)