Skip to content

Commit 697118d

Browse files
committed
Auto merge of #88627 - cjgillot:noallocuse, r=petrochenkov
Do not preallocate HirIds Part of rust-lang/rust#87234 r? `@petrochenkov`
2 parents 08a0307 + d60bbde commit 697118d

File tree

4 files changed

+32
-100
lines changed

4 files changed

+32
-100
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

+25-33
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
}
@@ -486,7 +475,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
486475
}
487476
}
488477

489-
let mut resolutions = self.expect_full_res_from_use(id);
478+
let mut resolutions = self.expect_full_res_from_use(id).fuse();
490479
// We want to return *something* from this function, so hold onto the first item
491480
// for later.
492481
let ret_res = self.lower_res(resolutions.next().unwrap_or(Res::Err));
@@ -496,7 +485,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
496485
// won't be dealing with macros in the rest of the compiler.
497486
// Essentially a single `use` which imports two names is desugared into
498487
// two imports.
499-
for (res, &new_node_id) in iter::zip(resolutions, &[id1, id2]) {
488+
for new_node_id in [id1, id2] {
489+
// Associate an HirId to both ids even if there is no resolution.
490+
let new_id = self.allocate_hir_id_counter(new_node_id);
491+
492+
let res = if let Some(res) = resolutions.next() { res } else { continue };
500493
let ident = *ident;
501494
let mut path = path.clone();
502495
for seg in &mut path.segments {
@@ -505,17 +498,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
505498
let span = path.span;
506499

507500
self.with_hir_id_owner(new_node_id, |this| {
508-
let new_id = this.lower_node_id(new_node_id);
509501
let res = this.lower_res(res);
510502
let path = this.lower_path_extra(res, &path, ParamMode::Explicit, None);
511503
let kind = hir::ItemKind::Use(path, hir::UseKind::Single);
512504
let vis = this.rebuild_vis(&vis);
513505
if let Some(attrs) = attrs {
514-
this.attrs.insert(new_id, attrs);
506+
this.attrs.insert(hir::HirId::make_owner(new_id), attrs);
515507
}
516508

517509
this.insert_item(hir::Item {
518-
def_id: new_id.expect_owner(),
510+
def_id: new_id,
519511
ident: this.lower_ident(ident),
520512
kind,
521513
vis,
@@ -564,7 +556,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
564556

565557
// Add all the nested `PathListItem`s to the HIR.
566558
for &(ref use_tree, id) in trees {
567-
let new_hir_id = self.lower_node_id(id);
559+
let new_hir_id = self.allocate_hir_id_counter(id);
568560

569561
let mut prefix = prefix.clone();
570562

@@ -585,11 +577,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
585577
let kind =
586578
this.lower_use_tree(use_tree, &prefix, id, &mut vis, &mut ident, attrs);
587579
if let Some(attrs) = attrs {
588-
this.attrs.insert(new_hir_id, attrs);
580+
this.attrs.insert(hir::HirId::make_owner(new_hir_id), attrs);
589581
}
590582

591583
this.insert_item(hir::Item {
592-
def_id: new_hir_id.expect_owner(),
584+
def_id: new_hir_id,
593585
ident: this.lower_ident(ident),
594586
kind,
595587
vis,
@@ -700,7 +692,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
700692

701693
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef<'hir> {
702694
hir::ForeignItemRef {
703-
id: hir::ForeignItemId { def_id: self.lower_node_id(i.id).expect_owner() },
695+
id: hir::ForeignItemId { def_id: self.allocate_hir_id_counter(i.id) },
704696
ident: self.lower_ident(i.ident),
705697
span: self.lower_span(i.span),
706698
vis: self.lower_visibility(&i.vis, Some(i.id)),
@@ -839,7 +831,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
839831
}
840832
AssocItemKind::MacCall(..) => unimplemented!(),
841833
};
842-
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) };
843835
let defaultness = hir::Defaultness::Default { has_value: has_default };
844836
hir::TraitItemRef {
845837
id,
@@ -925,7 +917,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
925917
let has_value = true;
926918
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
927919
hir::ImplItemRef {
928-
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) },
929921
ident: self.lower_ident(i.ident),
930922
span: self.lower_span(i.span),
931923
vis: self.lower_visibility(&i.vis, Some(i.id)),

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

+5-60
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;
4242
use rustc_ast::{self as ast, *};
4343
use rustc_ast_pretty::pprust;
4444
use rustc_data_structures::captures::Captures;
@@ -418,60 +418,9 @@ enum AnonymousLifetimeMode {
418418

419419
impl<'a, 'hir> LoweringContext<'a, 'hir> {
420420
fn lower_crate(mut self, c: &Crate) -> &'hir hir::Crate<'hir> {
421-
/// Full-crate AST visitor that inserts into a fresh
422-
/// `LoweringContext` any information that may be
423-
/// needed from arbitrary locations in the crate,
424-
/// e.g., the number of lifetime generic parameters
425-
/// declared for every type and trait definition.
426-
struct MiscCollector<'tcx, 'lowering, 'hir> {
427-
lctx: &'tcx mut LoweringContext<'lowering, 'hir>,
428-
}
429-
430-
impl MiscCollector<'_, '_, '_> {
431-
fn allocate_use_tree_hir_id_counters(&mut self, tree: &UseTree) {
432-
match tree.kind {
433-
UseTreeKind::Simple(_, id1, id2) => {
434-
for id in [id1, id2] {
435-
self.lctx.allocate_hir_id_counter(id);
436-
}
437-
}
438-
UseTreeKind::Glob => (),
439-
UseTreeKind::Nested(ref trees) => {
440-
for &(ref use_tree, id) in trees {
441-
self.lctx.allocate_hir_id_counter(id);
442-
self.allocate_use_tree_hir_id_counters(use_tree);
443-
}
444-
}
445-
}
446-
}
447-
}
448-
449-
impl<'tcx> Visitor<'tcx> for MiscCollector<'tcx, '_, '_> {
450-
fn visit_item(&mut self, item: &'tcx Item) {
451-
self.lctx.allocate_hir_id_counter(item.id);
452-
453-
if let ItemKind::Use(ref use_tree) = item.kind {
454-
self.allocate_use_tree_hir_id_counters(use_tree);
455-
}
456-
457-
visit::walk_item(self, item);
458-
}
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-
}
469-
}
470-
471421
self.lower_node_id(CRATE_NODE_ID);
472422
debug_assert!(self.node_id_to_hir_id[CRATE_NODE_ID] == Some(hir::CRATE_HIR_ID));
473423

474-
visit::walk_crate(&mut MiscCollector { lctx: &mut self }, c);
475424
visit::walk_crate(&mut item::ItemLowerer { lctx: &mut self }, c);
476425

477426
let module = self.arena.alloc(self.lower_mod(&c.items, c.span));
@@ -554,13 +503,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
554503
id
555504
}
556505

557-
fn allocate_hir_id_counter(&mut self, owner: NodeId) -> hir::HirId {
506+
fn allocate_hir_id_counter(&mut self, owner: NodeId) -> LocalDefId {
558507
// Set up the counter if needed.
559508
self.item_local_id_counters.entry(owner).or_insert(0);
560509
// Always allocate the first `HirId` for the owner itself.
561510
let lowered = self.lower_node_id_with_owner(owner, owner);
562511
debug_assert_eq!(lowered.local_id.as_u32(), 0);
563-
lowered
512+
lowered.owner
564513
}
565514

566515
fn create_stable_hashing_context(&self) -> LoweringHasher<'_> {
@@ -1494,9 +1443,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14941443
// frequently opened issues show.
14951444
let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None);
14961445

1497-
let opaque_ty_def_id = self.resolver.local_def_id(opaque_ty_node_id);
1498-
1499-
self.allocate_hir_id_counter(opaque_ty_node_id);
1446+
let opaque_ty_def_id = self.allocate_hir_id_counter(opaque_ty_node_id);
15001447

15011448
let collected_lifetimes = self.with_hir_id_owner(opaque_ty_node_id, move |lctx| {
15021449
let hir_bounds = lower_bounds(lctx);
@@ -1753,9 +1700,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17531700

17541701
let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::Async, span, None);
17551702

1756-
let opaque_ty_def_id = self.resolver.local_def_id(opaque_ty_node_id);
1757-
1758-
self.allocate_hir_id_counter(opaque_ty_node_id);
1703+
let opaque_ty_def_id = self.allocate_hir_id_counter(opaque_ty_node_id);
17591704

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

Diff for: compiler/rustc_middle/src/hir/map/mod.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_data_structures::fingerprint::Fingerprint;
77
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
88
use rustc_data_structures::svh::Svh;
99
use rustc_hir::def::{DefKind, Res};
10-
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE};
10+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
1111
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
1212
use rustc_hir::intravisit::{self, Visitor};
1313
use rustc_hir::itemlikevisit::ItemLikeVisitor;
@@ -206,11 +206,6 @@ impl<'hir> Map<'hir> {
206206
}
207207

208208
pub fn opt_def_kind(&self, local_def_id: LocalDefId) -> Option<DefKind> {
209-
// FIXME(eddyb) support `find` on the crate root.
210-
if local_def_id.to_def_id().index == CRATE_DEF_INDEX {
211-
return Some(DefKind::Mod);
212-
}
213-
214209
let hir_id = self.local_def_id_to_hir_id(local_def_id);
215210
let def_kind = match self.find(hir_id)? {
216211
Node::Item(item) => match item.kind {

0 commit comments

Comments
 (0)