Skip to content

Commit 7e2dd67

Browse files
committed
Move local_id_to_def_id to Lowering Context
1 parent a8d2474 commit 7e2dd67

File tree

1 file changed

+29
-29
lines changed
  • compiler/rustc_ast_lowering/src

1 file changed

+29
-29
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+29-29
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
5454
use rustc_hir::def_id::{DefId, DefPathHash, LocalDefId, CRATE_DEF_ID};
5555
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
5656
use rustc_hir::intravisit;
57-
use rustc_hir::{ConstArg, GenericArg, ParamName};
57+
use rustc_hir::{ConstArg, GenericArg, ItemLocalId, ParamName};
5858
use rustc_index::vec::{Idx, IndexVec};
5959
use rustc_query_system::ich::StableHashingContext;
6060
use rustc_session::lint::LintBuffer;
@@ -155,6 +155,7 @@ struct LoweringContext<'a, 'hir: 'a> {
155155

156156
current_hir_id_owner: LocalDefId,
157157
item_local_id_counter: hir::ItemLocalId,
158+
local_id_to_def_id: SortedMap<ItemLocalId, LocalDefId>,
158159

159160
/// NodeIds that are lowered inside the current HIR owner.
160161
node_id_to_local_id: FxHashMap<NodeId, hir::ItemLocalId>,
@@ -312,6 +313,7 @@ pub fn lower_crate<'a, 'hir>(
312313
current_hir_id_owner: CRATE_DEF_ID,
313314
item_local_id_counter: hir::ItemLocalId::new(0),
314315
node_id_to_local_id: FxHashMap::default(),
316+
local_id_to_def_id: SortedMap::new(),
315317
generator_kind: None,
316318
task_context: None,
317319
current_item: None,
@@ -439,6 +441,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
439441
let current_attrs = std::mem::take(&mut self.attrs);
440442
let current_bodies = std::mem::take(&mut self.bodies);
441443
let current_node_ids = std::mem::take(&mut self.node_id_to_local_id);
444+
let current_id_to_def_id = std::mem::take(&mut self.local_id_to_def_id);
442445
let current_owner = std::mem::replace(&mut self.current_hir_id_owner, def_id);
443446
let current_local_counter =
444447
std::mem::replace(&mut self.item_local_id_counter, hir::ItemLocalId::new(1));
@@ -454,6 +457,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
454457
self.attrs = current_attrs;
455458
self.bodies = current_bodies;
456459
self.node_id_to_local_id = current_node_ids;
460+
self.local_id_to_def_id = current_id_to_def_id;
457461
self.current_hir_id_owner = current_owner;
458462
self.item_local_id_counter = current_local_counter;
459463

@@ -468,25 +472,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
468472
let mut bodies = std::mem::take(&mut self.bodies);
469473
let node_id_to_local_id = std::mem::take(&mut self.node_id_to_local_id);
470474

471-
let local_id_to_def_id = node_id_to_local_id
472-
.iter()
473-
.filter_map(|(&node_id, &local_id)| {
474-
if local_id == hir::ItemLocalId::new(0) {
475-
None
476-
} else {
477-
let def_id = self.resolver.opt_local_def_id(node_id)?;
478-
479-
self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
480-
if let o @ hir::MaybeOwner::Phantom = &mut self.owners[def_id] {
481-
// Do not override a `MaybeOwner::Owner` that may already here.
482-
let hir_id = hir::HirId { owner: self.current_hir_id_owner, local_id };
483-
*o = hir::MaybeOwner::NonOwner(hir_id);
484-
}
485-
Some((local_id, def_id))
486-
}
487-
})
488-
.collect();
489-
490475
let trait_map = node_id_to_local_id
491476
.into_iter()
492477
.filter_map(|(node_id, local_id)| {
@@ -513,7 +498,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
513498
hash_without_bodies,
514499
nodes,
515500
bodies,
516-
local_id_to_def_id,
501+
local_id_to_def_id: std::mem::take(&mut self.local_id_to_def_id),
517502
};
518503
let attrs = {
519504
let mut hcx = self.resolver.create_stable_hashing_context();
@@ -556,18 +541,33 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
556541
fn lower_node_id(&mut self, ast_node_id: NodeId) -> hir::HirId {
557542
assert_ne!(ast_node_id, DUMMY_NODE_ID);
558543

559-
let owner = self.current_hir_id_owner;
560-
let local_id = match self.node_id_to_local_id.entry(ast_node_id) {
561-
Entry::Occupied(o) => *o.get(),
544+
match self.node_id_to_local_id.entry(ast_node_id) {
545+
Entry::Occupied(o) => {
546+
hir::HirId { owner: self.current_hir_id_owner, local_id: *o.get() }
547+
}
562548
Entry::Vacant(v) => {
563549
// Generate a new `HirId`.
550+
let owner = self.current_hir_id_owner;
564551
let local_id = self.item_local_id_counter;
565-
self.item_local_id_counter.increment_by(1);
552+
let hir_id = hir::HirId { owner, local_id };
553+
566554
v.insert(local_id);
567-
local_id
555+
self.item_local_id_counter.increment_by(1);
556+
557+
if local_id != hir::ItemLocalId::new(0) {
558+
if let Some(def_id) = self.resolver.opt_local_def_id(ast_node_id) {
559+
self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
560+
if let o @ hir::MaybeOwner::Phantom = &mut self.owners[def_id] {
561+
// Do not override a `MaybeOwner::Owner` that may already here.
562+
*o = hir::MaybeOwner::NonOwner(hir_id);
563+
}
564+
self.local_id_to_def_id.insert(local_id, def_id);
565+
}
566+
}
567+
568+
hir_id
568569
}
569-
};
570-
hir::HirId { owner, local_id }
570+
}
571571
}
572572

573573
fn next_id(&mut self) -> hir::HirId {
@@ -1427,14 +1427,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14271427
let lifetime_defs =
14281428
lctx.arena.alloc_from_iter(collected_lifetimes.iter().map(|&(name, span)| {
14291429
let def_node_id = lctx.resolver.next_node_id();
1430-
let hir_id = lctx.lower_node_id(def_node_id);
14311430
lctx.resolver.create_def(
14321431
opaque_ty_def_id,
14331432
def_node_id,
14341433
DefPathData::LifetimeNs(name.ident().name),
14351434
ExpnId::root(),
14361435
span.with_parent(None),
14371436
);
1437+
let hir_id = lctx.lower_node_id(def_node_id);
14381438

14391439
let (name, kind) = match name {
14401440
hir::LifetimeName::Underscore => (

0 commit comments

Comments
 (0)