Skip to content

Commit 0a8fd43

Browse files
authored
Rollup merge of #73291 - marmeladema:hir-id-ification-fix, r=petrochenkov
Pre-compute `LocalDefId` <-> `HirId` mappings and remove `NodeId` <-> `HirId` conversion APIs cc #50928 I don't know who is exactly the best person to review this. r? @petrochenkov
2 parents 9003087 + 13104ef commit 0a8fd43

File tree

7 files changed

+48
-65
lines changed

7 files changed

+48
-65
lines changed

src/librustc_ast_lowering/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ pub trait Resolver {
205205
fn lint_buffer(&mut self) -> &mut LintBuffer;
206206

207207
fn next_node_id(&mut self) -> NodeId;
208+
209+
fn trait_map(&self) -> &NodeMap<Vec<hir::TraitCandidate>>;
208210
}
209211

210212
type NtToTokenstream = fn(&Nonterminal, &ParseSess, Span) -> TokenStream;
@@ -557,6 +559,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
557559
let proc_macros =
558560
c.proc_macros.iter().map(|id| self.node_id_to_hir_id[*id].unwrap()).collect();
559561

562+
let trait_map = self
563+
.resolver
564+
.trait_map()
565+
.iter()
566+
.map(|(&k, v)| (self.node_id_to_hir_id[k].unwrap(), v.clone()))
567+
.collect();
568+
560569
self.resolver.definitions().init_node_id_to_hir_id_mapping(self.node_id_to_hir_id);
561570

562571
hir::Crate {
@@ -571,6 +580,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
571580
trait_impls: self.trait_impls,
572581
modules: self.modules,
573582
proc_macros,
583+
trait_map,
574584
}
575585
}
576586

src/librustc_hir/definitions.rs

+24-37
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,13 @@ pub struct Definitions {
8181

8282
def_id_to_span: IndexVec<LocalDefId, Span>,
8383

84-
// FIXME(eddyb) don't go through `ast::NodeId` to convert between `HirId`
85-
// and `LocalDefId` - ideally all `LocalDefId`s would be HIR owners.
8684
node_id_to_def_id: FxHashMap<ast::NodeId, LocalDefId>,
8785
def_id_to_node_id: IndexVec<LocalDefId, ast::NodeId>,
8886

89-
pub(super) node_id_to_hir_id: IndexVec<ast::NodeId, Option<hir::HirId>>,
90-
/// The reverse mapping of `node_id_to_hir_id`.
91-
pub(super) hir_id_to_node_id: FxHashMap<hir::HirId, ast::NodeId>,
87+
// FIXME(eddyb) ideally all `LocalDefId`s would be HIR owners.
88+
pub(super) def_id_to_hir_id: IndexVec<LocalDefId, Option<hir::HirId>>,
89+
/// The reverse mapping of `def_id_to_hir_id`.
90+
pub(super) hir_id_to_def_id: FxHashMap<hir::HirId, LocalDefId>,
9291

9392
/// If `ExpnId` is an ID of some macro expansion,
9493
/// then `DefId` is the normal module (`mod`) in which the expanded macro was defined.
@@ -327,47 +326,27 @@ impl Definitions {
327326

328327
#[inline]
329328
pub fn local_def_id(&self, node: ast::NodeId) -> LocalDefId {
330-
self.opt_local_def_id(node).unwrap_or_else(|| {
331-
panic!("no entry for node id: `{:?}` / `{:?}`", node, self.opt_node_id_to_hir_id(node))
332-
})
329+
self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{:?}`", node))
333330
}
334331

335332
#[inline]
336333
pub fn as_local_hir_id(&self, def_id: LocalDefId) -> hir::HirId {
337334
self.local_def_id_to_hir_id(def_id)
338335
}
339336

340-
#[inline]
341-
pub fn hir_id_to_node_id(&self, hir_id: hir::HirId) -> ast::NodeId {
342-
self.hir_id_to_node_id[&hir_id]
343-
}
344-
345-
#[inline]
346-
pub fn node_id_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
347-
self.node_id_to_hir_id[node_id].unwrap()
348-
}
349-
350-
#[inline]
351-
pub fn opt_node_id_to_hir_id(&self, node_id: ast::NodeId) -> Option<hir::HirId> {
352-
self.node_id_to_hir_id[node_id]
353-
}
354-
355337
#[inline]
356338
pub fn local_def_id_to_hir_id(&self, id: LocalDefId) -> hir::HirId {
357-
let node_id = self.def_id_to_node_id[id];
358-
self.node_id_to_hir_id[node_id].unwrap()
339+
self.def_id_to_hir_id[id].unwrap()
359340
}
360341

361342
#[inline]
362343
pub fn opt_local_def_id_to_hir_id(&self, id: LocalDefId) -> Option<hir::HirId> {
363-
let node_id = self.def_id_to_node_id[id];
364-
self.node_id_to_hir_id[node_id]
344+
self.def_id_to_hir_id[id]
365345
}
366346

367347
#[inline]
368348
pub fn opt_hir_id_to_local_def_id(&self, hir_id: hir::HirId) -> Option<LocalDefId> {
369-
let node_id = self.hir_id_to_node_id(hir_id);
370-
self.opt_local_def_id(node_id)
349+
self.hir_id_to_def_id.get(&hir_id).copied()
371350
}
372351

373352
/// Retrieves the span of the given `DefId` if `DefId` is in the local crate.
@@ -477,16 +456,24 @@ impl Definitions {
477456
mapping: IndexVec<ast::NodeId, Option<hir::HirId>>,
478457
) {
479458
assert!(
480-
self.node_id_to_hir_id.is_empty(),
481-
"trying to initialize `NodeId` -> `HirId` mapping twice"
459+
self.def_id_to_hir_id.is_empty(),
460+
"trying to initialize `LocalDefId` <-> `HirId` mappings twice"
482461
);
483-
self.node_id_to_hir_id = mapping;
484462

485-
// Build the reverse mapping of `node_id_to_hir_id`.
486-
self.hir_id_to_node_id = self
487-
.node_id_to_hir_id
488-
.iter_enumerated()
489-
.filter_map(|(node_id, &hir_id)| hir_id.map(|hir_id| (hir_id, node_id)))
463+
self.def_id_to_hir_id = self
464+
.def_id_to_node_id
465+
.iter()
466+
.map(|&node_id| mapping.get(node_id).and_then(|&hir_id| hir_id))
467+
.collect();
468+
469+
// Build the reverse mapping of `def_id_to_hir_id`.
470+
self.hir_id_to_def_id = mapping
471+
.into_iter_enumerated()
472+
.filter_map(|(node_id, hir_id)| {
473+
hir_id.and_then(|hir_id| {
474+
self.node_id_to_def_id.get(&node_id).map(|&def_id| (hir_id, def_id))
475+
})
476+
})
490477
.collect();
491478
}
492479

src/librustc_hir/hir.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,8 @@ pub struct Crate<'hir> {
639639
/// A list of proc macro HirIds, written out in the order in which
640640
/// they are declared in the static array generated by proc_macro_harness.
641641
pub proc_macros: Vec<HirId>,
642+
643+
pub trait_map: BTreeMap<HirId, Vec<TraitCandidate>>,
642644
}
643645

644646
impl Crate<'hir> {
@@ -2651,7 +2653,7 @@ pub type CaptureModeMap = NodeMap<CaptureBy>;
26512653
// The TraitCandidate's import_ids is empty if the trait is defined in the same module, and
26522654
// has length > 0 if the trait is found through an chain of imports, starting with the
26532655
// import/use statement in the scope where the trait is used.
2654-
#[derive(Clone, Debug)]
2656+
#[derive(RustcEncodable, RustcDecodable, Clone, Debug)]
26552657
pub struct TraitCandidate {
26562658
pub def_id: DefId,
26572659
pub import_ids: SmallVec<[LocalDefId; 1]>,

src/librustc_middle/hir/map/collector.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
117117
body_ids: _,
118118
modules: _,
119119
proc_macros: _,
120+
trait_map: _,
120121
} = *krate;
121122

122123
hash_body(&mut hcx, root_mod_def_path_hash, item, &mut hir_body_nodes)
@@ -241,11 +242,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
241242
// Make sure that the DepNode of some node coincides with the HirId
242243
// owner of that node.
243244
if cfg!(debug_assertions) {
244-
let node_id = self.definitions.hir_id_to_node_id(hir_id);
245-
assert_eq!(self.definitions.node_id_to_hir_id(node_id), hir_id);
246-
247245
if hir_id.owner != self.current_dep_node_owner {
248-
let node_str = match self.definitions.opt_local_def_id(node_id) {
246+
let node_str = match self.definitions.opt_hir_id_to_local_def_id(hir_id) {
249247
Some(def_id) => self.definitions.def_path(def_id).to_string_no_crate(),
250248
None => format!("{:?}", node),
251249
};
@@ -335,9 +333,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
335333
debug!("visit_item: {:?}", i);
336334
debug_assert_eq!(
337335
i.hir_id.owner,
338-
self.definitions
339-
.opt_local_def_id(self.definitions.hir_id_to_node_id(i.hir_id))
340-
.unwrap()
336+
self.definitions.opt_hir_id_to_local_def_id(i.hir_id).unwrap()
341337
);
342338
self.with_dep_node_owner(i.hir_id.owner, i, |this, hash| {
343339
this.insert_with_hash(i.span, i.hir_id, Node::Item(i), hash);
@@ -369,9 +365,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
369365
fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {
370366
debug_assert_eq!(
371367
ti.hir_id.owner,
372-
self.definitions
373-
.opt_local_def_id(self.definitions.hir_id_to_node_id(ti.hir_id))
374-
.unwrap()
368+
self.definitions.opt_hir_id_to_local_def_id(ti.hir_id).unwrap()
375369
);
376370
self.with_dep_node_owner(ti.hir_id.owner, ti, |this, hash| {
377371
this.insert_with_hash(ti.span, ti.hir_id, Node::TraitItem(ti), hash);
@@ -385,9 +379,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
385379
fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) {
386380
debug_assert_eq!(
387381
ii.hir_id.owner,
388-
self.definitions
389-
.opt_local_def_id(self.definitions.hir_id_to_node_id(ii.hir_id))
390-
.unwrap()
382+
self.definitions.opt_hir_id_to_local_def_id(ii.hir_id).unwrap()
391383
);
392384
self.with_dep_node_owner(ii.hir_id.owner, ii, |this, hash| {
393385
this.insert_with_hash(ii.span, ii.hir_id, Node::ImplItem(ii), hash);

src/librustc_middle/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1101,9 +1101,9 @@ impl<'tcx> TyCtxt<'tcx> {
11011101
};
11021102

11031103
let mut trait_map: FxHashMap<_, FxHashMap<_, _>> = FxHashMap::default();
1104-
for (hir_id, v) in resolutions.trait_map.into_iter() {
1104+
for (hir_id, v) in krate.trait_map.iter() {
11051105
let map = trait_map.entry(hir_id.owner).or_default();
1106-
map.insert(hir_id.local_id, StableVec::new(v));
1106+
map.insert(hir_id.local_id, StableVec::new(v.to_vec()));
11071107
}
11081108

11091109
GlobalCtxt {

src/librustc_middle/ty/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ pub struct ResolverOutputs {
121121
pub definitions: rustc_hir::definitions::Definitions,
122122
pub cstore: Box<CrateStoreDyn>,
123123
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
124-
pub trait_map: FxHashMap<hir::HirId, Vec<hir::TraitCandidate>>,
125124
pub maybe_unused_trait_imports: FxHashSet<LocalDefId>,
126125
pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
127126
pub export_map: ExportMap<LocalDefId>,

src/librustc_resolve/lib.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,10 @@ impl rustc_ast_lowering::Resolver for Resolver<'_> {
11091109
fn next_node_id(&mut self) -> NodeId {
11101110
self.next_node_id()
11111111
}
1112+
1113+
fn trait_map(&self) -> &NodeMap<Vec<TraitCandidate>> {
1114+
&self.trait_map
1115+
}
11121116
}
11131117

11141118
impl<'a> Resolver<'a> {
@@ -1284,11 +1288,6 @@ impl<'a> Resolver<'a> {
12841288
let definitions = self.definitions;
12851289
let extern_crate_map = self.extern_crate_map;
12861290
let export_map = self.export_map;
1287-
let trait_map = self
1288-
.trait_map
1289-
.into_iter()
1290-
.map(|(k, v)| (definitions.node_id_to_hir_id(k), v))
1291-
.collect();
12921291
let maybe_unused_trait_imports = self.maybe_unused_trait_imports;
12931292
let maybe_unused_extern_crates = self.maybe_unused_extern_crates;
12941293
let glob_map = self.glob_map;
@@ -1297,7 +1296,6 @@ impl<'a> Resolver<'a> {
12971296
cstore: Box::new(self.crate_loader.into_cstore()),
12981297
extern_crate_map,
12991298
export_map,
1300-
trait_map,
13011299
glob_map,
13021300
maybe_unused_trait_imports,
13031301
maybe_unused_extern_crates,
@@ -1315,11 +1313,6 @@ impl<'a> Resolver<'a> {
13151313
cstore: Box::new(self.cstore().clone()),
13161314
extern_crate_map: self.extern_crate_map.clone(),
13171315
export_map: self.export_map.clone(),
1318-
trait_map: self
1319-
.trait_map
1320-
.iter()
1321-
.map(|(&k, v)| (self.definitions.node_id_to_hir_id(k), v.clone()))
1322-
.collect(),
13231316
glob_map: self.glob_map.clone(),
13241317
maybe_unused_trait_imports: self.maybe_unused_trait_imports.clone(),
13251318
maybe_unused_extern_crates: self.maybe_unused_extern_crates.clone(),

0 commit comments

Comments
 (0)