1
Fork 0

Do not preallocate item HirIds.

This commit is contained in:
Camille GILLOT 2021-07-14 18:54:56 +02:00
parent d2dfb0eb8e
commit 59f43bdd29
3 changed files with 20 additions and 47 deletions

View file

@ -55,7 +55,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
} }
} }
StmtKind::Item(ref it) => { StmtKind::Item(ref it) => {
stmts.extend(self.lower_item_id(it).into_iter().enumerate().map( stmts.extend(self.lower_item_ref(it).into_iter().enumerate().map(
|(i, item_id)| { |(i, item_id)| {
let hir_id = match i { let hir_id = match i {
0 => self.lower_node_id(s.id), 0 => self.lower_node_id(s.id),

View file

@ -40,6 +40,7 @@ impl ItemLowerer<'_, '_, '_> {
impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> { impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
fn visit_item(&mut self, item: &'a Item) { fn visit_item(&mut self, item: &'a Item) {
self.lctx.allocate_hir_id_counter(item.id);
let hir_id = self.lctx.with_hir_id_owner(item.id, |lctx| { let hir_id = self.lctx.with_hir_id_owner(item.id, |lctx| {
lctx.without_in_scope_lifetime_defs(|lctx| { lctx.without_in_scope_lifetime_defs(|lctx| {
let hir_item = lctx.lower_item(item); let hir_item = lctx.lower_item(item);
@ -77,6 +78,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
} }
fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) { fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
self.lctx.allocate_hir_id_counter(item.id);
self.lctx.with_hir_id_owner(item.id, |lctx| match ctxt { self.lctx.with_hir_id_owner(item.id, |lctx| match ctxt {
AssocCtxt::Trait => { AssocCtxt::Trait => {
let hir_item = lctx.lower_trait_item(item); let hir_item = lctx.lower_trait_item(item);
@ -154,41 +156,28 @@ impl<'hir> LoweringContext<'_, 'hir> {
pub(super) fn lower_mod(&mut self, items: &[P<Item>], inner: Span) -> hir::Mod<'hir> { pub(super) fn lower_mod(&mut self, items: &[P<Item>], inner: Span) -> hir::Mod<'hir> {
hir::Mod { hir::Mod {
inner: self.lower_span(inner), inner: self.lower_span(inner),
item_ids: self.arena.alloc_from_iter(items.iter().flat_map(|x| self.lower_item_id(x))), item_ids: self.arena.alloc_from_iter(items.iter().flat_map(|x| self.lower_item_ref(x))),
} }
} }
pub(super) fn lower_item_id(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> { pub(super) fn lower_item_ref(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
let node_ids = match i.kind { let mut node_ids = smallvec![hir::ItemId { def_id: self.resolver.local_def_id(i.id) }];
ItemKind::Use(ref use_tree) => { if let ItemKind::Use(ref use_tree) = &i.kind {
let mut vec = smallvec![i.id]; self.lower_item_id_use_tree(use_tree, i.id, &mut node_ids);
self.lower_item_id_use_tree(use_tree, i.id, &mut vec); }
vec
}
ItemKind::Fn(..) | ItemKind::Impl(box ImplKind { of_trait: None, .. }) => {
smallvec![i.id]
}
_ => smallvec![i.id],
};
node_ids node_ids
.into_iter()
.map(|node_id| hir::ItemId {
def_id: self.allocate_hir_id_counter(node_id).expect_owner(),
})
.collect()
} }
fn lower_item_id_use_tree( fn lower_item_id_use_tree(
&mut self, &mut self,
tree: &UseTree, tree: &UseTree,
base_id: NodeId, base_id: NodeId,
vec: &mut SmallVec<[NodeId; 1]>, vec: &mut SmallVec<[hir::ItemId; 1]>,
) { ) {
match tree.kind { match tree.kind {
UseTreeKind::Nested(ref nested_vec) => { UseTreeKind::Nested(ref nested_vec) => {
for &(ref nested, id) in nested_vec { for &(ref nested, id) in nested_vec {
vec.push(id); vec.push(hir::ItemId { def_id: self.resolver.local_def_id(id) });
self.lower_item_id_use_tree(nested, id, vec); self.lower_item_id_use_tree(nested, id, vec);
} }
} }
@ -197,7 +186,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
for (_, &id) in for (_, &id) in
iter::zip(self.expect_full_res_from_use(base_id).skip(1), &[id1, id2]) iter::zip(self.expect_full_res_from_use(base_id).skip(1), &[id1, id2])
{ {
vec.push(id); vec.push(hir::ItemId { def_id: self.resolver.local_def_id(id) });
} }
} }
} }
@ -700,7 +689,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef<'hir> { fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef<'hir> {
hir::ForeignItemRef { hir::ForeignItemRef {
id: hir::ForeignItemId { def_id: self.lower_node_id(i.id).expect_owner() }, id: hir::ForeignItemId { def_id: self.allocate_hir_id_counter(i.id) },
ident: self.lower_ident(i.ident), ident: self.lower_ident(i.ident),
span: self.lower_span(i.span), span: self.lower_span(i.span),
vis: self.lower_visibility(&i.vis, Some(i.id)), vis: self.lower_visibility(&i.vis, Some(i.id)),
@ -842,7 +831,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
} }
AssocItemKind::MacCall(..) => unimplemented!(), AssocItemKind::MacCall(..) => unimplemented!(),
}; };
let id = hir::TraitItemId { def_id: self.lower_node_id(i.id).expect_owner() }; let id = hir::TraitItemId { def_id: self.resolver.local_def_id(i.id) };
let defaultness = hir::Defaultness::Default { has_value: has_default }; let defaultness = hir::Defaultness::Default { has_value: has_default };
hir::TraitItemRef { hir::TraitItemRef {
id, id,
@ -928,7 +917,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let has_value = true; let has_value = true;
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value); let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
hir::ImplItemRef { hir::ImplItemRef {
id: hir::ImplItemId { def_id: self.lower_node_id(i.id).expect_owner() }, id: hir::ImplItemId { def_id: self.allocate_hir_id_counter(i.id) },
ident: self.lower_ident(i.ident), ident: self.lower_ident(i.ident),
span: self.lower_span(i.span), span: self.lower_span(i.span),
vis: self.lower_visibility(&i.vis, Some(i.id)), vis: self.lower_visibility(&i.vis, Some(i.id)),

View file

@ -38,7 +38,7 @@
use rustc_ast::node_id::NodeMap; use rustc_ast::node_id::NodeMap;
use rustc_ast::token::{self, Token}; use rustc_ast::token::{self, Token};
use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, TokenStream, TokenTree}; use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, TokenStream, TokenTree};
use rustc_ast::visit::{self, AssocCtxt, Visitor}; use rustc_ast::visit::{self, Visitor};
use rustc_ast::{self as ast, *}; use rustc_ast::{self as ast, *};
use rustc_ast_pretty::pprust; use rustc_ast_pretty::pprust;
use rustc_data_structures::captures::Captures; use rustc_data_structures::captures::Captures;
@ -448,24 +448,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
impl<'tcx> Visitor<'tcx> for MiscCollector<'tcx, '_, '_> { impl<'tcx> Visitor<'tcx> for MiscCollector<'tcx, '_, '_> {
fn visit_item(&mut self, item: &'tcx Item) { fn visit_item(&mut self, item: &'tcx Item) {
self.lctx.allocate_hir_id_counter(item.id);
if let ItemKind::Use(ref use_tree) = item.kind { if let ItemKind::Use(ref use_tree) = item.kind {
self.allocate_use_tree_hir_id_counters(use_tree); self.allocate_use_tree_hir_id_counters(use_tree);
} }
visit::walk_item(self, item); visit::walk_item(self, item);
} }
fn visit_assoc_item(&mut self, item: &'tcx AssocItem, ctxt: AssocCtxt) {
self.lctx.allocate_hir_id_counter(item.id);
visit::walk_assoc_item(self, item, ctxt);
}
fn visit_foreign_item(&mut self, item: &'tcx ForeignItem) {
self.lctx.allocate_hir_id_counter(item.id);
visit::walk_foreign_item(self, item);
}
} }
self.lower_node_id(CRATE_NODE_ID); self.lower_node_id(CRATE_NODE_ID);
@ -554,13 +542,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
id id
} }
fn allocate_hir_id_counter(&mut self, owner: NodeId) -> hir::HirId { fn allocate_hir_id_counter(&mut self, owner: NodeId) -> LocalDefId {
// Set up the counter if needed. // Set up the counter if needed.
self.item_local_id_counters.entry(owner).or_insert(0); self.item_local_id_counters.entry(owner).or_insert(0);
// Always allocate the first `HirId` for the owner itself. // Always allocate the first `HirId` for the owner itself.
let lowered = self.lower_node_id_with_owner(owner, owner); let lowered = self.lower_node_id_with_owner(owner, owner);
debug_assert_eq!(lowered.local_id.as_u32(), 0); debug_assert_eq!(lowered.local_id.as_u32(), 0);
lowered lowered.owner
} }
fn create_stable_hashing_context(&self) -> LoweringHasher<'_> { fn create_stable_hashing_context(&self) -> LoweringHasher<'_> {
@ -1503,9 +1491,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// frequently opened issues show. // frequently opened issues show.
let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None); let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None);
let opaque_ty_def_id = self.resolver.local_def_id(opaque_ty_node_id); let opaque_ty_def_id = self.allocate_hir_id_counter(opaque_ty_node_id);
self.allocate_hir_id_counter(opaque_ty_node_id);
let collected_lifetimes = self.with_hir_id_owner(opaque_ty_node_id, move |lctx| { let collected_lifetimes = self.with_hir_id_owner(opaque_ty_node_id, move |lctx| {
let hir_bounds = lower_bounds(lctx); let hir_bounds = lower_bounds(lctx);
@ -1762,9 +1748,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::Async, span, None); let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::Async, span, None);
let opaque_ty_def_id = self.resolver.local_def_id(opaque_ty_node_id); let opaque_ty_def_id = self.allocate_hir_id_counter(opaque_ty_node_id);
self.allocate_hir_id_counter(opaque_ty_node_id);
// When we create the opaque type for this async fn, it is going to have // When we create the opaque type for this async fn, it is going to have
// to capture all the lifetimes involved in the signature (including in the // to capture all the lifetimes involved in the signature (including in the