Skip to content

Commit 69cea9c

Browse files
committed
Query-ify LocalDefId <-> HirId conversion
Based on PR rust-lang#70039
1 parent 32c654a commit 69cea9c

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

src/librustc_middle/arena.rs

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ macro_rules! arena_types {
9494
[few] hir_definitions: rustc_hir::definitions::Definitions,
9595
[] hir_owner: rustc_middle::hir::Owner<$tcx>,
9696
[] hir_owner_nodes: rustc_middle::hir::OwnerNodes<$tcx>,
97+
[] hir_owner_defs: rustc_data_structures::fx::FxHashMap<rustc_hir::ItemLocalId, rustc_span::def_id::LocalDefId>,
9798

9899
// Note that this deliberately duplicates items in the `rustc_hir::arena`,
99100
// since we need to allocate this type on both the `rustc_hir` arena

src/librustc_middle/hir/map/collector.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
133133
hcx,
134134
hir_body_nodes,
135135
map: (0..definitions.def_index_count())
136-
.map(|_| HirOwnerData { signature: None, with_bodies: None })
136+
.map(|_| HirOwnerData { signature: None, with_bodies: None, defs: None })
137137
.collect(),
138138
};
139139
collector.insert_entry(
@@ -229,6 +229,14 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
229229
id.local_id,
230230
ParentedNode { parent: entry.parent.local_id, node: entry.node },
231231
);
232+
233+
// Check if this HirId has a DefId, and insert it in the `defs` map if so.
234+
if let Some(def_id) = self.definitions.opt_hir_id_to_local_def_id(id) {
235+
if data.defs.is_none() {
236+
data.defs = Some(arena.alloc(FxHashMap::default()));
237+
};
238+
data.defs.as_mut().unwrap().insert(id.local_id, def_id);
239+
}
232240
}
233241
}
234242

src/librustc_middle/hir/map/mod.rs

+10-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 as ast;
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, CRATE_DEF_INDEX, 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> {
@@ -170,17 +172,22 @@ impl<'hir> Map<'hir> {
170172

171173
#[inline]
172174
pub fn opt_local_def_id(&self, hir_id: HirId) -> Option<LocalDefId> {
173-
self.tcx.definitions.opt_hir_id_to_local_def_id(hir_id)
175+
if hir_id.local_id == ItemLocalId::from_u32(0) {
176+
// Every HirId owner has a DefId, so we can just return it directly here
177+
Some(hir_id.owner)
178+
} else {
179+
self.tcx.hir_owner_defs(hir_id.owner).and_then(|map| map.get(&hir_id.local_id).copied())
180+
}
174181
}
175182

176183
#[inline]
177184
pub fn local_def_id_to_hir_id(&self, def_id: LocalDefId) -> HirId {
178-
self.tcx.definitions.local_def_id_to_hir_id(def_id)
185+
self.tcx.local_def_id_to_hir_id(def_id).unwrap()
179186
}
180187

181188
#[inline]
182189
pub fn opt_local_def_id_to_hir_id(&self, def_id: LocalDefId) -> Option<HirId> {
183-
self.tcx.definitions.opt_local_def_id_to_hir_id(def_id)
190+
self.tcx.local_def_id_to_hir_id(def_id)
184191
}
185192

186193
pub fn def_kind(&self, local_def_id: LocalDefId) -> DefKind {

src/librustc_middle/hir/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,8 @@ pub fn provide(providers: &mut Providers) {
9292
span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", id);
9393
}
9494
};
95+
providers.local_def_id_to_hir_id = |tcx, id| tcx.definitions.opt_local_def_id_to_hir_id(id);
96+
providers.hir_owner_defs =
97+
|tcx, index| tcx.index_hir(LOCAL_CRATE).map[index].defs.as_ref().map(|defs| &**defs);
9598
map::provide(providers);
9699
}

src/librustc_middle/query/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,22 @@ rustc_queries! {
112112
desc { |tcx| "computing the optional const parameter of `{}`", tcx.def_path_str(key.to_def_id()) }
113113
}
114114

115+
// Converts a `LocalDefId` to a `HirId`.
116+
// This can be conveniently accessed by `tcx.hir().as_local_hir_id`.
117+
// Avoid calling this query directly.
118+
query local_def_id_to_hir_id(key: LocalDefId) -> Option<hir::HirId> {
119+
eval_always
120+
desc { "converting a LocalDefId to HirId" }
121+
}
122+
123+
// A map of the HIR items which also have a `DefId` in addition to their `HirId`.
124+
// This can be conveniently accessed by `tcx.hir().opt_local_def_id`.
125+
// Avoid calling this query directly.
126+
query hir_owner_defs(key: LocalDefId) -> Option<&'tcx FxHashMap<ItemLocalId, LocalDefId>> {
127+
eval_always
128+
desc { "getting a LocalDefId map" }
129+
}
130+
115131
/// Records the type of every item.
116132
query type_of(key: DefId) -> Ty<'tcx> {
117133
desc { |tcx| "computing type of `{}`", tcx.def_path_str(key) }

0 commit comments

Comments
 (0)