@@ -54,7 +54,7 @@ use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
54
54
use rustc_hir::def_id::{DefId, DefPathHash, LocalDefId, CRATE_DEF_ID};
55
55
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
56
56
use rustc_hir::intravisit;
57
- use rustc_hir::{ConstArg, GenericArg, ParamName};
57
+ use rustc_hir::{ConstArg, GenericArg, ItemLocalId, ParamName};
58
58
use rustc_index::vec::{Idx, IndexVec};
59
59
use rustc_query_system::ich::StableHashingContext;
60
60
use rustc_session::lint::LintBuffer;
@@ -155,6 +155,7 @@ struct LoweringContext<'a, 'hir: 'a> {
155
155
156
156
current_hir_id_owner: LocalDefId,
157
157
item_local_id_counter: hir::ItemLocalId,
158
+ local_id_to_def_id: SortedMap<ItemLocalId, LocalDefId>,
158
159
159
160
/// NodeIds that are lowered inside the current HIR owner.
160
161
node_id_to_local_id: FxHashMap<NodeId, hir::ItemLocalId>,
@@ -312,6 +313,7 @@ pub fn lower_crate<'a, 'hir>(
312
313
current_hir_id_owner: CRATE_DEF_ID,
313
314
item_local_id_counter: hir::ItemLocalId::new(0),
314
315
node_id_to_local_id: FxHashMap::default(),
316
+ local_id_to_def_id: SortedMap::new(),
315
317
generator_kind: None,
316
318
task_context: None,
317
319
current_item: None,
@@ -439,6 +441,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
439
441
let current_attrs = std::mem::take(&mut self.attrs);
440
442
let current_bodies = std::mem::take(&mut self.bodies);
441
443
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);
442
445
let current_owner = std::mem::replace(&mut self.current_hir_id_owner, def_id);
443
446
let current_local_counter =
444
447
std::mem::replace(&mut self.item_local_id_counter, hir::ItemLocalId::new(1));
@@ -454,6 +457,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
454
457
self.attrs = current_attrs;
455
458
self.bodies = current_bodies;
456
459
self.node_id_to_local_id = current_node_ids;
460
+ self.local_id_to_def_id = current_id_to_def_id;
457
461
self.current_hir_id_owner = current_owner;
458
462
self.item_local_id_counter = current_local_counter;
459
463
@@ -468,25 +472,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
468
472
let mut bodies = std::mem::take(&mut self.bodies);
469
473
let node_id_to_local_id = std::mem::take(&mut self.node_id_to_local_id);
470
474
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
-
490
475
let trait_map = node_id_to_local_id
491
476
.into_iter()
492
477
.filter_map(|(node_id, local_id)| {
@@ -513,7 +498,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
513
498
hash_without_bodies,
514
499
nodes,
515
500
bodies,
516
- local_id_to_def_id,
501
+ local_id_to_def_id: std::mem::take(&mut self.local_id_to_def_id) ,
517
502
};
518
503
let attrs = {
519
504
let mut hcx = self.resolver.create_stable_hashing_context();
@@ -556,18 +541,33 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
556
541
fn lower_node_id(&mut self, ast_node_id: NodeId) -> hir::HirId {
557
542
assert_ne!(ast_node_id, DUMMY_NODE_ID);
558
543
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
+ }
562
548
Entry::Vacant(v) => {
563
549
// Generate a new `HirId`.
550
+ let owner = self.current_hir_id_owner;
564
551
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
+
566
554
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
568
569
}
569
- };
570
- hir::HirId { owner, local_id }
570
+ }
571
571
}
572
572
573
573
fn next_id(&mut self) -> hir::HirId {
@@ -1427,14 +1427,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1427
1427
let lifetime_defs =
1428
1428
lctx.arena.alloc_from_iter(collected_lifetimes.iter().map(|&(name, span)| {
1429
1429
let def_node_id = lctx.resolver.next_node_id();
1430
- let hir_id = lctx.lower_node_id(def_node_id);
1431
1430
lctx.resolver.create_def(
1432
1431
opaque_ty_def_id,
1433
1432
def_node_id,
1434
1433
DefPathData::LifetimeNs(name.ident().name),
1435
1434
ExpnId::root(),
1436
1435
span.with_parent(None),
1437
1436
);
1437
+ let hir_id = lctx.lower_node_id(def_node_id);
1438
1438
1439
1439
let (name, kind) = match name {
1440
1440
hir::LifetimeName::Underscore => (
0 commit comments