Skip to content

Commit bb00048

Browse files
committed
HIR: introduce a HirId to DefId map in Definitions
1 parent fe2f7e0 commit bb00048

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

src/librustc/hir/lowering.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,18 @@ impl<'a> LoweringContext<'a> {
392392
match tree.kind {
393393
UseTreeKind::Simple(_, id1, id2) => {
394394
for &id in &[id1, id2] {
395-
self.lctx.resolver.definitions().create_def_with_parent(
395+
let def_index = self.lctx.resolver.definitions().create_def_with_parent(
396396
owner,
397397
id,
398+
None,
398399
DefPathData::Misc,
399400
ExpnId::root(),
400401
tree.prefix.span,
401402
);
402-
self.lctx.allocate_hir_id_counter(id);
403+
let hir_id = self.lctx.allocate_hir_id_counter(id);
404+
405+
self.lctx.resolver.definitions()
406+
.hir_to_def_index.insert(hir_id, def_index);
403407
}
404408
}
405409
UseTreeKind::Glob => (),
@@ -534,6 +538,10 @@ impl<'a> LoweringContext<'a> {
534538
.definitions()
535539
.init_node_id_to_hir_id_mapping(self.node_id_to_hir_id);
536540

541+
self.resolver
542+
.definitions()
543+
.finalize_hir_to_def_index_mapping();
544+
537545
hir::Crate {
538546
module,
539547
attrs,
@@ -793,17 +801,20 @@ impl<'a> LoweringContext<'a> {
793801
),
794802
};
795803

804+
let hir_id = self.lower_node_id(node_id);
805+
796806
// Add a definition for the in-band lifetime def.
797807
self.resolver.definitions().create_def_with_parent(
798808
parent_index,
799809
node_id,
810+
Some(hir_id),
800811
DefPathData::LifetimeNs(str_name),
801812
ExpnId::root(),
802813
span,
803814
);
804815

805816
hir::GenericParam {
806-
hir_id: self.lower_node_id(node_id),
817+
hir_id,
807818
name: hir_name,
808819
attrs: hir_vec![],
809820
bounds: hir_vec![],
@@ -1095,6 +1106,7 @@ impl<'a> LoweringContext<'a> {
10951106
self.resolver.definitions().create_def_with_parent(
10961107
parent_def_index,
10971108
impl_trait_node_id,
1109+
None,
10981110
DefPathData::ImplTrait,
10991111
ExpnId::root(),
11001112
constraint.span,
@@ -1576,6 +1588,7 @@ impl<'a> LoweringContext<'a> {
15761588
self.context.resolver.definitions().create_def_with_parent(
15771589
self.parent,
15781590
def_node_id,
1591+
Some(hir_id),
15791592
DefPathData::LifetimeNs(name.ident().as_interned_str()),
15801593
ExpnId::root(),
15811594
lifetime.span);

src/librustc/hir/map/def_collector.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ impl<'a> DefCollector<'a> {
2828
-> DefIndex {
2929
let parent_def = self.parent_def;
3030
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
31-
self.definitions.create_def_with_parent(parent_def, node_id, data, self.expansion, span)
31+
self.definitions.create_def_with_parent(
32+
parent_def, node_id, None, data, self.expansion, span)
3233
}
3334

3435
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: DefIndex, f: F) {

src/librustc/hir/map/definitions.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ use crate::hir;
88
use crate::hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE, CRATE_DEF_INDEX};
99
use crate::ich::Fingerprint;
1010
use crate::session::CrateDisambiguator;
11-
use crate::util::nodemap::NodeMap;
11+
use crate::util::nodemap::{HirIdMap, NodeMap};
1212

1313
use rustc_data_structures::fx::FxHashMap;
1414
use rustc_data_structures::indexed_vec::{IndexVec};
1515
use rustc_data_structures::stable_hasher::StableHasher;
1616
use std::borrow::Borrow;
1717
use std::fmt::Write;
1818
use std::hash::Hash;
19+
use std::mem;
1920
use syntax::ast;
2021
use syntax::ext::hygiene::ExpnId;
2122
use syntax::symbol::{Symbol, sym, InternedString};
@@ -92,6 +93,10 @@ impl DefPathTable {
9293
pub struct Definitions {
9394
table: DefPathTable,
9495
node_to_def_index: NodeMap<DefIndex>,
96+
pub hir_to_def_index: HirIdMap<DefIndex>,
97+
/// `DefIndex`es created by `DefCollector::create_def` before the AST lowering; used
98+
/// to complete the `hir_to_def_index` mapping afterwards.
99+
defs_awaiting_hir_id: NodeMap<DefIndex>,
95100
def_index_to_node: Vec<ast::NodeId>,
96101
pub(super) node_to_hir_id: IndexVec<ast::NodeId, hir::HirId>,
97102
/// If `ExpnId` is an ID of some macro expansion,
@@ -360,11 +365,21 @@ impl Definitions {
360365
self.node_to_def_index.get(&node).cloned()
361366
}
362367

368+
#[inline]
369+
pub fn opt_def_index_from_hir_id(&self, hir: hir::HirId) -> Option<DefIndex> {
370+
self.hir_to_def_index.get(&hir).cloned()
371+
}
372+
363373
#[inline]
364374
pub fn opt_local_def_id(&self, node: ast::NodeId) -> Option<DefId> {
365375
self.opt_def_index(node).map(DefId::local)
366376
}
367377

378+
#[inline]
379+
pub fn opt_local_def_id_from_hir_id(&self, hir: hir::HirId) -> Option<DefId> {
380+
self.opt_def_index_from_hir_id(hir).map(DefId::local)
381+
}
382+
368383
#[inline]
369384
pub fn local_def_id(&self, node: ast::NodeId) -> DefId {
370385
self.opt_local_def_id(node).unwrap()
@@ -440,6 +455,7 @@ impl Definitions {
440455
assert!(self.def_index_to_node.is_empty());
441456
self.def_index_to_node.push(ast::CRATE_NODE_ID);
442457
self.node_to_def_index.insert(ast::CRATE_NODE_ID, root_index);
458+
self.hir_to_def_index.insert(hir::CRATE_HIR_ID, root_index);
443459
self.set_invocation_parent(ExpnId::root(), root_index);
444460

445461
// Allocate some other `DefIndex`es that always must exist.
@@ -452,6 +468,7 @@ impl Definitions {
452468
pub fn create_def_with_parent(&mut self,
453469
parent: DefIndex,
454470
node_id: ast::NodeId,
471+
hir_id: Option<hir::HirId>,
455472
data: DefPathData,
456473
expn_id: ExpnId,
457474
span: Span)
@@ -499,6 +516,12 @@ impl Definitions {
499516
if node_id != ast::DUMMY_NODE_ID {
500517
debug!("create_def_with_parent: def_index_to_node[{:?} <-> {:?}", index, node_id);
501518
self.node_to_def_index.insert(node_id, index);
519+
520+
if let Some(hir_id) = hir_id {
521+
self.hir_to_def_index.insert(hir_id, index);
522+
} else {
523+
self.defs_awaiting_hir_id.insert(node_id, index);
524+
}
502525
}
503526

504527
if expn_id != ExpnId::root() {
@@ -522,6 +545,15 @@ impl Definitions {
522545
self.node_to_hir_id = mapping;
523546
}
524547

548+
/// Fill the missing bits of the `HirId` to `DefIndex` mapping after the AST lowering; see
549+
/// the related comment to the `defs_awaiting_hir_id` map in the `Definitions` struct.
550+
pub fn finalize_hir_to_def_index_mapping(&mut self) {
551+
for (node_id, def_id) in mem::replace(&mut self.defs_awaiting_hir_id, Default::default()) {
552+
let hir_id = self.node_to_hir_id[node_id];
553+
self.hir_to_def_index.insert(hir_id, def_id);
554+
}
555+
}
556+
525557
pub fn expansion_that_defined(&self, index: DefIndex) -> ExpnId {
526558
self.expansions_that_defined.get(&index).cloned().unwrap_or(ExpnId::root())
527559
}
@@ -611,6 +643,7 @@ macro_rules! define_global_metadata_kind {
611643
definitions.create_def_with_parent(
612644
CRATE_DEF_INDEX,
613645
ast::DUMMY_NODE_ID,
646+
Some(hir::DUMMY_HIR_ID),
614647
DefPathData::GlobalMetaData(instance.name().as_interned_str()),
615648
ExpnId::root(),
616649
DUMMY_SP

src/librustc/hir/map/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,7 @@ impl<'hir> Map<'hir> {
281281

282282
#[inline]
283283
pub fn opt_local_def_id(&self, hir_id: HirId) -> Option<DefId> {
284-
let node_id = self.hir_to_node_id(hir_id);
285-
self.definitions.opt_local_def_id(node_id)
284+
self.definitions.opt_local_def_id_from_hir_id(hir_id)
286285
}
287286

288287
#[inline]

0 commit comments

Comments
 (0)