Skip to content

Commit a1a3557

Browse files
committed
Make with_hir_id_owner responsible for registering the item.
1 parent c1bac92 commit a1a3557

File tree

3 files changed

+91
-140
lines changed

3 files changed

+91
-140
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
327327
let mut generic_args = vec![];
328328
for (idx, arg) in args.into_iter().enumerate() {
329329
if legacy_args_idx.contains(&idx) {
330-
let parent_def_id = self.current_hir_id_owner.0;
330+
let parent_def_id = self.current_hir_id_owner;
331331
let node_id = self.resolver.next_node_id();
332332

333333
// Add a definition for the in-band const def.

compiler/rustc_ast_lowering/src/item.rs

+42-42
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,9 @@ 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);
4443
let hir_id = self.lctx.with_hir_id_owner(item.id, |lctx| {
45-
lctx.without_in_scope_lifetime_defs(|lctx| {
46-
let hir_item = lctx.lower_item(item);
47-
lctx.insert_item(hir_item)
48-
})
44+
let node = lctx.without_in_scope_lifetime_defs(|lctx| lctx.lower_item(item));
45+
hir::OwnerNode::Item(node)
4946
});
5047

5148
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
@@ -72,26 +69,17 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
7269
}
7370

7471
fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
75-
self.lctx.allocate_hir_id_counter(item.id);
7672
self.lctx.with_hir_id_owner(item.id, |lctx| match ctxt {
77-
AssocCtxt::Trait => {
78-
let hir_item = lctx.lower_trait_item(item);
79-
lctx.insert_trait_item(hir_item);
80-
}
81-
AssocCtxt::Impl => {
82-
let hir_item = lctx.lower_impl_item(item);
83-
lctx.insert_impl_item(hir_item);
84-
}
73+
AssocCtxt::Trait => hir::OwnerNode::TraitItem(lctx.lower_trait_item(item)),
74+
AssocCtxt::Impl => hir::OwnerNode::ImplItem(lctx.lower_impl_item(item)),
8575
});
8676

8777
visit::walk_assoc_item(self, item, ctxt);
8878
}
8979

9080
fn visit_foreign_item(&mut self, item: &'a ForeignItem) {
91-
self.lctx.allocate_hir_id_counter(item.id);
9281
self.lctx.with_hir_id_owner(item.id, |lctx| {
93-
let hir_item = lctx.lower_foreign_item(item);
94-
lctx.insert_foreign_item(hir_item);
82+
hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item))
9583
});
9684

9785
visit::walk_foreign_item(self, item);
@@ -106,12 +94,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
10694
// only used when lowering a child item of a trait or impl.
10795
fn with_parent_item_lifetime_defs<T>(
10896
&mut self,
109-
parent_hir_id: hir::ItemId,
97+
parent_hir_id: LocalDefId,
11098
f: impl FnOnce(&mut Self) -> T,
11199
) -> T {
112100
let old_len = self.in_scope_lifetimes.len();
113101

114-
let parent_generics = match self.owners[parent_hir_id.def_id].unwrap().expect_item().kind {
102+
let parent_generics = match self.owners[parent_hir_id].unwrap().expect_item().kind {
115103
hir::ItemKind::Impl(hir::Impl { ref generics, .. })
116104
| hir::ItemKind::Trait(_, _, ref generics, ..) => generics.params,
117105
_ => &[],
@@ -186,19 +174,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
186174
}
187175
}
188176

189-
pub fn lower_item(&mut self, i: &Item) -> hir::Item<'hir> {
177+
fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
190178
let mut ident = i.ident;
191179
let mut vis = self.lower_visibility(&i.vis);
192180
let hir_id = self.lower_node_id(i.id);
193181
let attrs = self.lower_attrs(hir_id, &i.attrs);
194182
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, &mut vis, &i.kind);
195-
hir::Item {
183+
let item = hir::Item {
196184
def_id: hir_id.expect_owner(),
197185
ident: self.lower_ident(ident),
198186
kind,
199187
vis,
200188
span: self.lower_span(i.span),
201-
}
189+
};
190+
self.arena.alloc(item)
202191
}
203192

204193
fn lower_item_kind(
@@ -480,10 +469,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
480469
// Essentially a single `use` which imports two names is desugared into
481470
// two imports.
482471
for new_node_id in [id1, id2] {
483-
// Associate an HirId to both ids even if there is no resolution.
484-
let new_id = self.allocate_hir_id_counter(new_node_id);
485-
486-
let res = if let Some(res) = resolutions.next() { res } else { continue };
472+
let new_id = self.resolver.local_def_id(new_node_id);
473+
let res = if let Some(res) = resolutions.next() {
474+
res
475+
} else {
476+
// Associate an HirId to both ids even if there is no resolution.
477+
self.node_id_to_hir_id.ensure_contains_elem(new_node_id, || None);
478+
debug_assert!(self.node_id_to_hir_id[new_node_id].is_none());
479+
self.node_id_to_hir_id[new_node_id] = Some(hir::HirId::make_owner(new_id));
480+
continue;
481+
};
487482
let ident = *ident;
488483
let mut path = path.clone();
489484
for seg in &mut path.segments {
@@ -500,13 +495,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
500495
this.attrs.insert(hir::HirId::make_owner(new_id), attrs);
501496
}
502497

503-
this.insert_item(hir::Item {
498+
let item = hir::Item {
504499
def_id: new_id,
505500
ident: this.lower_ident(ident),
506501
kind,
507502
vis,
508503
span: this.lower_span(span),
509-
});
504+
};
505+
hir::OwnerNode::Item(this.arena.alloc(item))
510506
});
511507
}
512508

@@ -550,7 +546,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
550546

551547
// Add all the nested `PathListItem`s to the HIR.
552548
for &(ref use_tree, id) in trees {
553-
let new_hir_id = self.allocate_hir_id_counter(id);
549+
let new_hir_id = self.resolver.local_def_id(id);
554550

555551
let mut prefix = prefix.clone();
556552

@@ -574,13 +570,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
574570
this.attrs.insert(hir::HirId::make_owner(new_hir_id), attrs);
575571
}
576572

577-
this.insert_item(hir::Item {
573+
let item = hir::Item {
578574
def_id: new_hir_id,
579575
ident: this.lower_ident(ident),
580576
kind,
581577
vis,
582578
span: this.lower_span(use_tree.span),
583-
});
579+
};
580+
hir::OwnerNode::Item(this.arena.alloc(item))
584581
});
585582
}
586583

@@ -647,11 +644,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
647644
respan(self.lower_span(vis.span), vis_kind)
648645
}
649646

650-
fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem<'hir> {
647+
fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
651648
let hir_id = self.lower_node_id(i.id);
652649
let def_id = hir_id.expect_owner();
653650
self.lower_attrs(hir_id, &i.attrs);
654-
hir::ForeignItem {
651+
let item = hir::ForeignItem {
655652
def_id,
656653
ident: self.lower_ident(i.ident),
657654
kind: match i.kind {
@@ -681,12 +678,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
681678
},
682679
vis: self.lower_visibility(&i.vis),
683680
span: self.lower_span(i.span),
684-
}
681+
};
682+
self.arena.alloc(item)
685683
}
686684

687685
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef {
688686
hir::ForeignItemRef {
689-
id: hir::ForeignItemId { def_id: self.allocate_hir_id_counter(i.id) },
687+
id: hir::ForeignItemId { def_id: self.resolver.local_def_id(i.id) },
690688
ident: self.lower_ident(i.ident),
691689
span: self.lower_span(i.span),
692690
}
@@ -761,7 +759,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
761759
}
762760
}
763761

764-
fn lower_trait_item(&mut self, i: &AssocItem) -> hir::TraitItem<'hir> {
762+
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
765763
let hir_id = self.lower_node_id(i.id);
766764
let trait_item_def_id = hir_id.expect_owner();
767765

@@ -804,13 +802,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
804802
};
805803

806804
self.lower_attrs(hir_id, &i.attrs);
807-
hir::TraitItem {
805+
let item = hir::TraitItem {
808806
def_id: trait_item_def_id,
809807
ident: self.lower_ident(i.ident),
810808
generics,
811809
kind,
812810
span: self.lower_span(i.span),
813-
}
811+
};
812+
self.arena.alloc(item)
814813
}
815814

816815
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
@@ -840,7 +839,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
840839
self.expr(span, hir::ExprKind::Err, AttrVec::new())
841840
}
842841

843-
fn lower_impl_item(&mut self, i: &AssocItem) -> hir::ImplItem<'hir> {
842+
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
844843
let impl_item_def_id = self.resolver.local_def_id(i.id);
845844

846845
let (generics, kind) = match &i.kind {
@@ -894,23 +893,24 @@ impl<'hir> LoweringContext<'_, 'hir> {
894893
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
895894
let hir_id = self.lower_node_id(i.id);
896895
self.lower_attrs(hir_id, &i.attrs);
897-
hir::ImplItem {
896+
let item = hir::ImplItem {
898897
def_id: hir_id.expect_owner(),
899898
ident: self.lower_ident(i.ident),
900899
generics,
901900
vis: self.lower_visibility(&i.vis),
902901
defaultness,
903902
kind,
904903
span: self.lower_span(i.span),
905-
}
904+
};
905+
self.arena.alloc(item)
906906
}
907907

908908
fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef {
909909
// Since `default impl` is not yet implemented, this is always true in impls.
910910
let has_value = true;
911911
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
912912
hir::ImplItemRef {
913-
id: hir::ImplItemId { def_id: self.allocate_hir_id_counter(i.id) },
913+
id: hir::ImplItemId { def_id: self.resolver.local_def_id(i.id) },
914914
ident: self.lower_ident(i.ident),
915915
span: self.lower_span(i.span),
916916
defaultness,

0 commit comments

Comments
 (0)