Skip to content

Commit d60bbde

Browse files
committed
Do not preallocate UseTree HirIds.
1 parent 59f43bd commit d60bbde

File tree

3 files changed

+13
-54
lines changed

3 files changed

+13
-54
lines changed

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
475475
}
476476
}
477477

478-
let mut resolutions = self.expect_full_res_from_use(id);
478+
let mut resolutions = self.expect_full_res_from_use(id).fuse();
479479
// We want to return *something* from this function, so hold onto the first item
480480
// for later.
481481
let ret_res = self.lower_res(resolutions.next().unwrap_or(Res::Err));
@@ -485,7 +485,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
485485
// won't be dealing with macros in the rest of the compiler.
486486
// Essentially a single `use` which imports two names is desugared into
487487
// two imports.
488-
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 };
489493
let ident = *ident;
490494
let mut path = path.clone();
491495
for seg in &mut path.segments {
@@ -494,17 +498,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
494498
let span = path.span;
495499

496500
self.with_hir_id_owner(new_node_id, |this| {
497-
let new_id = this.lower_node_id(new_node_id);
498501
let res = this.lower_res(res);
499502
let path = this.lower_path_extra(res, &path, ParamMode::Explicit, None);
500503
let kind = hir::ItemKind::Use(path, hir::UseKind::Single);
501504
let vis = this.rebuild_vis(&vis);
502505
if let Some(attrs) = attrs {
503-
this.attrs.insert(new_id, attrs);
506+
this.attrs.insert(hir::HirId::make_owner(new_id), attrs);
504507
}
505508

506509
this.insert_item(hir::Item {
507-
def_id: new_id.expect_owner(),
510+
def_id: new_id,
508511
ident: this.lower_ident(ident),
509512
kind,
510513
vis,
@@ -553,7 +556,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
553556

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

558561
let mut prefix = prefix.clone();
559562

@@ -574,11 +577,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
574577
let kind =
575578
this.lower_use_tree(use_tree, &prefix, id, &mut vis, &mut ident, attrs);
576579
if let Some(attrs) = attrs {
577-
this.attrs.insert(new_hir_id, attrs);
580+
this.attrs.insert(hir::HirId::make_owner(new_hir_id), attrs);
578581
}
579582

580583
this.insert_item(hir::Item {
581-
def_id: new_hir_id.expect_owner(),
584+
def_id: new_hir_id,
582585
ident: this.lower_ident(ident),
583586
kind,
584587
vis,

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

+1-40
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, 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,48 +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-
if let ItemKind::Use(ref use_tree) = item.kind {
452-
self.allocate_use_tree_hir_id_counters(use_tree);
453-
}
454-
455-
visit::walk_item(self, item);
456-
}
457-
}
458-
459421
self.lower_node_id(CRATE_NODE_ID);
460422
debug_assert!(self.node_id_to_hir_id[CRATE_NODE_ID] == Some(hir::CRATE_HIR_ID));
461423

462-
visit::walk_crate(&mut MiscCollector { lctx: &mut self }, c);
463424
visit::walk_crate(&mut item::ItemLowerer { lctx: &mut self }, c);
464425

465426
let module = self.arena.alloc(self.lower_mod(&c.items, c.span));

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)