Do not preallocate UseTree HirIds.

This commit is contained in:
Camille GILLOT 2021-07-14 19:53:57 +02:00
parent 59f43bdd29
commit d60bbde7e2
3 changed files with 13 additions and 54 deletions

View File

@ -475,7 +475,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}
let mut resolutions = self.expect_full_res_from_use(id);
let mut resolutions = self.expect_full_res_from_use(id).fuse();
// We want to return *something* from this function, so hold onto the first item
// for later.
let ret_res = self.lower_res(resolutions.next().unwrap_or(Res::Err));
@ -485,7 +485,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
// won't be dealing with macros in the rest of the compiler.
// Essentially a single `use` which imports two names is desugared into
// two imports.
for (res, &new_node_id) in iter::zip(resolutions, &[id1, id2]) {
for new_node_id in [id1, id2] {
// Associate an HirId to both ids even if there is no resolution.
let new_id = self.allocate_hir_id_counter(new_node_id);
let res = if let Some(res) = resolutions.next() { res } else { continue };
let ident = *ident;
let mut path = path.clone();
for seg in &mut path.segments {
@ -494,17 +498,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
let span = path.span;
self.with_hir_id_owner(new_node_id, |this| {
let new_id = this.lower_node_id(new_node_id);
let res = this.lower_res(res);
let path = this.lower_path_extra(res, &path, ParamMode::Explicit, None);
let kind = hir::ItemKind::Use(path, hir::UseKind::Single);
let vis = this.rebuild_vis(&vis);
if let Some(attrs) = attrs {
this.attrs.insert(new_id, attrs);
this.attrs.insert(hir::HirId::make_owner(new_id), attrs);
}
this.insert_item(hir::Item {
def_id: new_id.expect_owner(),
def_id: new_id,
ident: this.lower_ident(ident),
kind,
vis,
@ -553,7 +556,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// Add all the nested `PathListItem`s to the HIR.
for &(ref use_tree, id) in trees {
let new_hir_id = self.lower_node_id(id);
let new_hir_id = self.allocate_hir_id_counter(id);
let mut prefix = prefix.clone();
@ -574,11 +577,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
let kind =
this.lower_use_tree(use_tree, &prefix, id, &mut vis, &mut ident, attrs);
if let Some(attrs) = attrs {
this.attrs.insert(new_hir_id, attrs);
this.attrs.insert(hir::HirId::make_owner(new_hir_id), attrs);
}
this.insert_item(hir::Item {
def_id: new_hir_id.expect_owner(),
def_id: new_hir_id,
ident: this.lower_ident(ident),
kind,
vis,

View File

@ -38,7 +38,7 @@
use rustc_ast::node_id::NodeMap;
use rustc_ast::token::{self, Token};
use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, TokenStream, TokenTree};
use rustc_ast::visit::{self, Visitor};
use rustc_ast::visit;
use rustc_ast::{self as ast, *};
use rustc_ast_pretty::pprust;
use rustc_data_structures::captures::Captures;
@ -418,48 +418,9 @@ enum AnonymousLifetimeMode {
impl<'a, 'hir> LoweringContext<'a, 'hir> {
fn lower_crate(mut self, c: &Crate) -> &'hir hir::Crate<'hir> {
/// Full-crate AST visitor that inserts into a fresh
/// `LoweringContext` any information that may be
/// needed from arbitrary locations in the crate,
/// e.g., the number of lifetime generic parameters
/// declared for every type and trait definition.
struct MiscCollector<'tcx, 'lowering, 'hir> {
lctx: &'tcx mut LoweringContext<'lowering, 'hir>,
}
impl MiscCollector<'_, '_, '_> {
fn allocate_use_tree_hir_id_counters(&mut self, tree: &UseTree) {
match tree.kind {
UseTreeKind::Simple(_, id1, id2) => {
for id in [id1, id2] {
self.lctx.allocate_hir_id_counter(id);
}
}
UseTreeKind::Glob => (),
UseTreeKind::Nested(ref trees) => {
for &(ref use_tree, id) in trees {
self.lctx.allocate_hir_id_counter(id);
self.allocate_use_tree_hir_id_counters(use_tree);
}
}
}
}
}
impl<'tcx> Visitor<'tcx> for MiscCollector<'tcx, '_, '_> {
fn visit_item(&mut self, item: &'tcx Item) {
if let ItemKind::Use(ref use_tree) = item.kind {
self.allocate_use_tree_hir_id_counters(use_tree);
}
visit::walk_item(self, item);
}
}
self.lower_node_id(CRATE_NODE_ID);
debug_assert!(self.node_id_to_hir_id[CRATE_NODE_ID] == Some(hir::CRATE_HIR_ID));
visit::walk_crate(&mut MiscCollector { lctx: &mut self }, c);
visit::walk_crate(&mut item::ItemLowerer { lctx: &mut self }, c);
let module = self.arena.alloc(self.lower_mod(&c.items, c.span));

View File

@ -7,7 +7,7 @@ use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::svh::Svh;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::itemlikevisit::ItemLikeVisitor;
@ -206,11 +206,6 @@ impl<'hir> Map<'hir> {
}
pub fn opt_def_kind(&self, local_def_id: LocalDefId) -> Option<DefKind> {
// FIXME(eddyb) support `find` on the crate root.
if local_def_id.to_def_id().index == CRATE_DEF_INDEX {
return Some(DefKind::Mod);
}
let hir_id = self.local_def_id_to_hir_id(local_def_id);
let def_kind = match self.find(hir_id)? {
Node::Item(item) => match item.kind {