Skip to content

Commit a1458b5

Browse files
committed
Add queries for LocalDefId <-> HirId conversion
1 parent c52cee1 commit a1458b5

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

src/librustc/arena.rs

+4
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ macro_rules! arena_types {
125125
[few] hir_definitions: rustc_hir::definitions::Definitions,
126126
[] hir_owner: rustc::hir::Owner<$tcx>,
127127
[] hir_owner_nodes: rustc::hir::OwnerNodes<$tcx>,
128+
[] hir_owner_defs: rustc_data_structures::fx::FxHashMap<
129+
rustc_hir::ItemLocalId,
130+
rustc_span::def_id::LocalDefId
131+
>,
128132
], $tcx);
129133
)
130134
}

src/librustc/hir/map/collector.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
132132
hcx,
133133
hir_body_nodes,
134134
map: (0..definitions.def_index_count())
135-
.map(|_| HirOwnerData { signature: None, with_bodies: None })
135+
.map(|_| HirOwnerData { signature: None, with_bodies: None, defs: None })
136136
.collect(),
137137
};
138138
collector.insert_entry(
@@ -202,6 +202,17 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
202202

203203
let data = &mut self.map[id.owner];
204204

205+
if i != 0 {
206+
// Check if this HirId has a DefId, and insert it in the `defs` map if so.
207+
let node_id = self.definitions.hir_id_to_node_id(id);
208+
if let Some(def_id) = self.definitions.opt_local_def_id(node_id) {
209+
if data.defs.is_none() {
210+
data.defs = Some(arena.alloc(FxHashMap::default()));
211+
};
212+
data.defs.as_mut().unwrap().insert(id.local_id, def_id);
213+
}
214+
}
215+
205216
if data.with_bodies.is_none() {
206217
data.with_bodies = Some(arena.alloc(OwnerNodes {
207218
hash,

src/librustc/hir/map/mod.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::hir::{Owner, OwnerNodes};
44
use crate::ty::query::Providers;
55
use crate::ty::TyCtxt;
66
use rustc_ast::ast::{self, Name, NodeId};
7+
use rustc_data_structures::fx::FxHashMap;
78
use rustc_data_structures::svh::Svh;
89
use rustc_hir::def::{DefKind, Res};
910
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
@@ -89,6 +90,7 @@ fn is_body_owner<'hir>(node: Node<'hir>, hir_id: HirId) -> bool {
8990
pub(super) struct HirOwnerData<'hir> {
9091
pub(super) signature: Option<&'hir Owner<'hir>>,
9192
pub(super) with_bodies: Option<&'hir mut OwnerNodes<'hir>>,
93+
pub(super) defs: Option<&'hir mut FxHashMap<ItemLocalId, LocalDefId>>,
9294
}
9395

9496
pub struct IndexedHir<'hir> {
@@ -184,8 +186,14 @@ impl<'hir> Map<'hir> {
184186

185187
#[inline]
186188
pub fn opt_local_def_id(&self, hir_id: HirId) -> Option<DefId> {
187-
let node_id = self.hir_id_to_node_id(hir_id);
188-
self.opt_local_def_id_from_node_id(node_id)
189+
if hir_id.local_id == ItemLocalId::from_u32(0) {
190+
// Every HirId owner has a DefId, so we can just return it directly here
191+
Some(hir_id.owner.to_def_id())
192+
} else {
193+
self.tcx
194+
.hir_owner_defs(hir_id.owner)
195+
.and_then(|map| map.get(&hir_id.local_id).map(|id| id.to_def_id()))
196+
}
189197
}
190198

191199
#[inline]
@@ -200,7 +208,7 @@ impl<'hir> Map<'hir> {
200208

201209
#[inline]
202210
pub fn as_local_hir_id(&self, def_id: DefId) -> Option<HirId> {
203-
self.tcx.definitions.as_local_hir_id(def_id)
211+
def_id.as_local().and_then(|def_id| self.tcx.local_def_id_to_hir_id(def_id))
204212
}
205213

206214
#[inline]

src/librustc/hir/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,8 @@ pub fn provide(providers: &mut Providers<'_>) {
8181
providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature;
8282
providers.hir_owner_nodes =
8383
|tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_ref().map(|nodes| &**nodes);
84+
providers.local_def_id_to_hir_id = |tcx, id| tcx.definitions.as_local_hir_id(id.to_def_id());
85+
providers.hir_owner_defs =
86+
|tcx, index| tcx.index_hir(LOCAL_CRATE).map[index].defs.as_ref().map(|defs| &**defs);
8487
map::provide(providers);
8588
}

src/librustc/query/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ rustc_queries! {
9090
desc { |tcx| "HIR owner items in `{}`", tcx.def_path_str(key.to_def_id()) }
9191
}
9292

93+
// Converts a `LocalDefId` to a `HirId`.
94+
// This can be conveniently accessed by `tcx.hir().as_local_hir_id`.
95+
// Avoid calling this query directly.
96+
query local_def_id_to_hir_id(key: LocalDefId) -> Option<hir::HirId> {
97+
eval_always
98+
desc { "converting a LocalDefId to HirId" }
99+
}
100+
101+
// A map of the HIR items which also have a `DefId` in addition to their `HirId`.
102+
// This can be conveniently accessed by `tcx.hir().opt_local_def_id`.
103+
// Avoid calling this query directly.
104+
query hir_owner_defs(key: LocalDefId) -> Option<&'tcx FxHashMap<ItemLocalId, LocalDefId>> {
105+
eval_always
106+
desc { "getting a LocalDefId map" }
107+
}
108+
93109
/// Records the type of every item.
94110
query type_of(key: DefId) -> Ty<'tcx> {
95111
cache_on_disk_if { key.is_local() }

0 commit comments

Comments
 (0)