Skip to content

Commit 3c6f982

Browse files
committed
Auto merge of #66131 - eddyb:local-def-id, r=petrochenkov
rustc: use LocalDefId instead of DefIndex where possible. That is, wherever `DefIndex` always referred to a "def" in the local crate, I replaced it with `LocalDefId`. While `LocalDefId` already existed, it wasn't used a lot, but I hope I'm on the right track. Unresolved questions: * [x] ~~should `LocalDefId` implement `rustc_index::Idx`?~~ * ~~this would get rid of a couple more `DefIndex` uses~~ * [x] ~~should `LocalDefId` be encoded/decoded as just a `DefIndex`?~~ * ~~right now it's a bit messy, `LocalDefId` encodes/decodes like `DefId`~~ * [x] ~~should `DefId::assert_local` be named something else, like `expect_local`?~~ A future PR should change `tcx.hir().local_def_id(...)` to return `LocalDefId` instead of `DefId`, as changing it in this PR would be too noisy. r? @michaelwoerister cc @nikomatsakis @petrochenkov @Zoxc
2 parents 6724d58 + 16e25f0 commit 3c6f982

File tree

49 files changed

+357
-436
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+357
-436
lines changed

src/librustc/dep_graph/dep_node.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use crate::ty::subst::SubstsRef;
6363
use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};
6464

6565
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
66-
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX};
66+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
6767
use rustc_hir::HirId;
6868
use rustc_span::symbol::Symbol;
6969
use std::fmt;
@@ -413,19 +413,19 @@ impl<'tcx> DepNodeParams<'tcx> for DefId {
413413
}
414414
}
415415

416-
impl<'tcx> DepNodeParams<'tcx> for DefIndex {
416+
impl<'tcx> DepNodeParams<'tcx> for LocalDefId {
417417
const CAN_RECONSTRUCT_QUERY_KEY: bool = true;
418418

419419
fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
420-
tcx.hir().definitions().def_path_hash(*self).0
420+
self.to_def_id().to_fingerprint(tcx)
421421
}
422422

423423
fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
424-
tcx.def_path_str(DefId::local(*self))
424+
self.to_def_id().to_debug_str(tcx)
425425
}
426426

427427
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
428-
dep_node.extract_def_id(tcx).map(|id| id.index)
428+
dep_node.extract_def_id(tcx).map(|id| id.expect_local())
429429
}
430430
}
431431

@@ -477,7 +477,7 @@ impl<'tcx> DepNodeParams<'tcx> for HirId {
477477
fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
478478
let HirId { owner, local_id } = *self;
479479

480-
let def_path_hash = tcx.def_path_hash(DefId::local(owner));
480+
let def_path_hash = tcx.def_path_hash(owner.to_def_id());
481481
let local_id = Fingerprint::from_smaller_hash(local_id.as_u32().into());
482482

483483
def_path_hash.0.combine(local_id)

src/librustc/dep_graph/graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ impl DepGraph {
902902

903903
fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
904904
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
905-
def_id.index == hir_id.owner
905+
def_id.index == hir_id.owner.local_def_index
906906
}
907907

908908
/// A "work product" is an intermediate result that we save into the

src/librustc/hir/map/collector.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1010
use rustc_data_structures::svh::Svh;
1111
use rustc_hir as hir;
1212
use rustc_hir::def_id::CRATE_DEF_INDEX;
13-
use rustc_hir::def_id::{DefIndex, LOCAL_CRATE};
13+
use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
1414
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
1515
use rustc_hir::*;
1616
use rustc_index::vec::{Idx, IndexVec};
@@ -30,12 +30,12 @@ pub(super) struct NodeCollector<'a, 'hir> {
3030
/// Source map
3131
source_map: &'a SourceMap,
3232

33-
map: IndexVec<DefIndex, HirOwnerData<'hir>>,
33+
map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
3434

3535
/// The parent of this node
3636
parent_node: hir::HirId,
3737

38-
current_dep_node_owner: DefIndex,
38+
current_dep_node_owner: LocalDefId,
3939

4040
definitions: &'a definitions::Definitions,
4141

@@ -98,7 +98,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
9898
definitions: &'a definitions::Definitions,
9999
mut hcx: StableHashingContext<'a>,
100100
) -> NodeCollector<'a, 'hir> {
101-
let root_mod_def_path_hash = definitions.def_path_hash(CRATE_DEF_INDEX);
101+
let root_mod_def_path_hash =
102+
definitions.def_path_hash(LocalDefId { local_def_index: CRATE_DEF_INDEX });
102103

103104
let mut hir_body_nodes = Vec::new();
104105

@@ -126,7 +127,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
126127
krate,
127128
source_map: sess.source_map(),
128129
parent_node: hir::CRATE_HIR_ID,
129-
current_dep_node_owner: CRATE_DEF_INDEX,
130+
current_dep_node_owner: LocalDefId { local_def_index: CRATE_DEF_INDEX },
130131
definitions,
131132
hcx,
132133
hir_body_nodes,
@@ -148,7 +149,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
148149
crate_disambiguator: CrateDisambiguator,
149150
cstore: &dyn CrateStore,
150151
commandline_args_hash: u64,
151-
) -> (IndexVec<DefIndex, HirOwnerData<'hir>>, Svh) {
152+
) -> (IndexVec<LocalDefId, HirOwnerData<'hir>>, Svh) {
152153
// Insert bodies into the map
153154
for (id, body) in self.krate.bodies.iter() {
154155
let bodies = &mut self.map[id.hir_id.owner].with_bodies.as_mut().unwrap().bodies;
@@ -244,8 +245,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
244245
assert_eq!(self.definitions.node_to_hir_id(node_id), hir_id);
245246

246247
if hir_id.owner != self.current_dep_node_owner {
247-
let node_str = match self.definitions.opt_def_index(node_id) {
248-
Some(def_index) => self.definitions.def_path(def_index).to_string_no_crate(),
248+
let node_str = match self.definitions.opt_local_def_id(node_id) {
249+
Some(def_id) => self.definitions.def_path(def_id).to_string_no_crate(),
249250
None => format!("{:?}", node),
250251
};
251252

@@ -285,7 +286,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
285286
F: FnOnce(&mut Self, Fingerprint),
286287
>(
287288
&mut self,
288-
dep_node_owner: DefIndex,
289+
dep_node_owner: LocalDefId,
289290
item_like: &T,
290291
f: F,
291292
) {
@@ -341,7 +342,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
341342
debug!("visit_item: {:?}", i);
342343
debug_assert_eq!(
343344
i.hir_id.owner,
344-
self.definitions.opt_def_index(self.definitions.hir_to_node_id(i.hir_id)).unwrap()
345+
self.definitions.opt_local_def_id(self.definitions.hir_to_node_id(i.hir_id)).unwrap()
345346
);
346347
self.with_dep_node_owner(i.hir_id.owner, i, |this, hash| {
347348
this.insert_with_hash(i.span, i.hir_id, Node::Item(i), hash);
@@ -373,7 +374,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
373374
fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {
374375
debug_assert_eq!(
375376
ti.hir_id.owner,
376-
self.definitions.opt_def_index(self.definitions.hir_to_node_id(ti.hir_id)).unwrap()
377+
self.definitions.opt_local_def_id(self.definitions.hir_to_node_id(ti.hir_id)).unwrap()
377378
);
378379
self.with_dep_node_owner(ti.hir_id.owner, ti, |this, hash| {
379380
this.insert_with_hash(ti.span, ti.hir_id, Node::TraitItem(ti), hash);
@@ -387,7 +388,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
387388
fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) {
388389
debug_assert_eq!(
389390
ii.hir_id.owner,
390-
self.definitions.opt_def_index(self.definitions.hir_to_node_id(ii.hir_id)).unwrap()
391+
self.definitions.opt_local_def_id(self.definitions.hir_to_node_id(ii.hir_id)).unwrap()
391392
);
392393
self.with_dep_node_owner(ii.hir_id.owner, ii, |this, hash| {
393394
this.insert_with_hash(ii.span, ii.hir_id, Node::ImplItem(ii), hash);
@@ -506,10 +507,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
506507
}
507508

508509
fn visit_macro_def(&mut self, macro_def: &'hir MacroDef<'hir>) {
509-
let node_id = self.definitions.hir_to_node_id(macro_def.hir_id);
510-
let def_index = self.definitions.opt_def_index(node_id).unwrap();
511-
512-
self.with_dep_node_owner(def_index, macro_def, |this, hash| {
510+
self.with_dep_node_owner(macro_def.hir_id.owner, macro_def, |this, hash| {
513511
this.insert_with_hash(
514512
macro_def.span,
515513
macro_def.hir_id,

0 commit comments

Comments
 (0)