Skip to content

Commit f3ec069

Browse files
committed
rustc: use LocalDefId instead of DefIndex in HirId.
1 parent 6130b99 commit f3ec069

File tree

32 files changed

+139
-202
lines changed

32 files changed

+139
-202
lines changed

src/librustc/dep_graph/dep_node.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -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

+15-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

@@ -126,7 +126,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
126126
krate,
127127
source_map: sess.source_map(),
128128
parent_node: hir::CRATE_HIR_ID,
129-
current_dep_node_owner: CRATE_DEF_INDEX,
129+
current_dep_node_owner: LocalDefId { local_def_index: CRATE_DEF_INDEX },
130130
definitions,
131131
hcx,
132132
hir_body_nodes,
@@ -148,7 +148,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
148148
crate_disambiguator: CrateDisambiguator,
149149
cstore: &dyn CrateStore,
150150
commandline_args_hash: u64,
151-
) -> (IndexVec<DefIndex, HirOwnerData<'hir>>, Svh) {
151+
) -> (IndexVec<LocalDefId, HirOwnerData<'hir>>, Svh) {
152152
// Insert bodies into the map
153153
for (id, body) in self.krate.bodies.iter() {
154154
let bodies = &mut self.map[id.hir_id.owner].with_bodies.as_mut().unwrap().bodies;
@@ -261,9 +261,11 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
261261
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?}){}",
262262
self.source_map.span_to_string(span),
263263
node_str,
264-
self.definitions.def_path(self.current_dep_node_owner).to_string_no_crate(),
264+
self.definitions
265+
.def_path(self.current_dep_node_owner.local_def_index)
266+
.to_string_no_crate(),
265267
self.current_dep_node_owner,
266-
self.definitions.def_path(hir_id.owner).to_string_no_crate(),
268+
self.definitions.def_path(hir_id.owner.local_def_index).to_string_no_crate(),
267269
hir_id.owner,
268270
forgot_str,
269271
)
@@ -285,13 +287,13 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
285287
F: FnOnce(&mut Self, Fingerprint),
286288
>(
287289
&mut self,
288-
dep_node_owner: DefIndex,
290+
dep_node_owner: LocalDefId,
289291
item_like: &T,
290292
f: F,
291293
) {
292294
let prev_owner = self.current_dep_node_owner;
293295

294-
let def_path_hash = self.definitions.def_path_hash(dep_node_owner);
296+
let def_path_hash = self.definitions.def_path_hash(dep_node_owner.local_def_index);
295297

296298
let hash = hash_body(&mut self.hcx, def_path_hash, item_like, &mut self.hir_body_nodes);
297299

@@ -340,7 +342,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
340342
fn visit_item(&mut self, i: &'hir Item<'hir>) {
341343
debug!("visit_item: {:?}", i);
342344
debug_assert_eq!(
343-
i.hir_id.owner,
345+
i.hir_id.owner.local_def_index,
344346
self.definitions.opt_def_index(self.definitions.hir_to_node_id(i.hir_id)).unwrap()
345347
);
346348
self.with_dep_node_owner(i.hir_id.owner, i, |this, hash| {
@@ -372,7 +374,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
372374

373375
fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {
374376
debug_assert_eq!(
375-
ti.hir_id.owner,
377+
ti.hir_id.owner.local_def_index,
376378
self.definitions.opt_def_index(self.definitions.hir_to_node_id(ti.hir_id)).unwrap()
377379
);
378380
self.with_dep_node_owner(ti.hir_id.owner, ti, |this, hash| {
@@ -386,7 +388,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
386388

387389
fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) {
388390
debug_assert_eq!(
389-
ii.hir_id.owner,
391+
ii.hir_id.owner.local_def_index,
390392
self.definitions.opt_def_index(self.definitions.hir_to_node_id(ii.hir_id)).unwrap()
391393
);
392394
self.with_dep_node_owner(ii.hir_id.owner, ii, |this, hash| {
@@ -506,10 +508,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
506508
}
507509

508510
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| {
511+
self.with_dep_node_owner(macro_def.hir_id.owner, macro_def, |this, hash| {
513512
this.insert_with_hash(
514513
macro_def.span,
515514
macro_def.hir_id,

src/librustc/hir/map/hir_id_validator.rs

+14-21
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::ty::TyCtxt;
33
use rustc_data_structures::fx::FxHashSet;
44
use rustc_data_structures::sync::{par_iter, Lock, ParallelIterator};
55
use rustc_hir as hir;
6-
use rustc_hir::def_id::{DefIndex, LocalDefId, CRATE_DEF_INDEX};
6+
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_INDEX};
77
use rustc_hir::intravisit;
88
use rustc_hir::itemlikevisit::ItemLikeVisitor;
99
use rustc_hir::{HirId, ItemLocalId};
@@ -32,7 +32,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
3232

3333
struct HirIdValidator<'a, 'hir> {
3434
hir_map: Map<'hir>,
35-
owner_def_index: Option<DefIndex>,
35+
owner: Option<LocalDefId>,
3636
hir_ids_seen: FxHashSet<ItemLocalId>,
3737
errors: &'a Lock<Vec<String>>,
3838
}
@@ -46,7 +46,7 @@ impl<'a, 'hir> OuterVisitor<'a, 'hir> {
4646
fn new_inner_visitor(&self, hir_map: Map<'hir>) -> HirIdValidator<'a, 'hir> {
4747
HirIdValidator {
4848
hir_map,
49-
owner_def_index: None,
49+
owner: None,
5050
hir_ids_seen: Default::default(),
5151
errors: self.errors,
5252
}
@@ -78,12 +78,12 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> {
7878
}
7979

8080
fn check<F: FnOnce(&mut HirIdValidator<'a, 'hir>)>(&mut self, hir_id: HirId, walk: F) {
81-
assert!(self.owner_def_index.is_none());
82-
let owner_def_index = self.hir_map.local_def_id(hir_id).index;
83-
self.owner_def_index = Some(owner_def_index);
81+
assert!(self.owner.is_none());
82+
let owner = self.hir_map.local_def_id(hir_id).expect_local();
83+
self.owner = Some(owner);
8484
walk(self);
8585

86-
if owner_def_index == CRATE_DEF_INDEX {
86+
if owner.local_def_index == CRATE_DEF_INDEX {
8787
return;
8888
}
8989

@@ -105,31 +105,26 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> {
105105
let mut missing_items = Vec::with_capacity(missing.len());
106106

107107
for local_id in missing {
108-
let hir_id =
109-
HirId { owner: owner_def_index, local_id: ItemLocalId::from_u32(local_id) };
108+
let hir_id = HirId { owner, local_id: ItemLocalId::from_u32(local_id) };
110109

111110
trace!("missing hir id {:#?}", hir_id);
112111

113112
missing_items.push(format!(
114113
"[local_id: {}, owner: {}]",
115114
local_id,
116-
self.hir_map
117-
.def_path(LocalDefId { local_def_index: owner_def_index })
118-
.to_string_no_crate()
115+
self.hir_map.def_path(owner).to_string_no_crate()
119116
));
120117
}
121118
self.error(|| {
122119
format!(
123120
"ItemLocalIds not assigned densely in {}. \
124121
Max ItemLocalId = {}, missing IDs = {:?}; seens IDs = {:?}",
125-
self.hir_map
126-
.def_path(LocalDefId { local_def_index: owner_def_index })
127-
.to_string_no_crate(),
122+
self.hir_map.def_path(owner).to_string_no_crate(),
128123
max,
129124
missing_items,
130125
self.hir_ids_seen
131126
.iter()
132-
.map(|&local_id| HirId { owner: owner_def_index, local_id })
127+
.map(|&local_id| HirId { owner, local_id })
133128
.map(|h| format!("({:?} {})", h, self.hir_map.node_to_string(h)))
134129
.collect::<Vec<_>>()
135130
)
@@ -146,7 +141,7 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
146141
}
147142

148143
fn visit_id(&mut self, hir_id: HirId) {
149-
let owner = self.owner_def_index.expect("no owner_def_index");
144+
let owner = self.owner.expect("no owner");
150145

151146
if hir_id == hir::DUMMY_HIR_ID {
152147
self.error(|| {
@@ -163,10 +158,8 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
163158
format!(
164159
"HirIdValidator: The recorded owner of {} is {} instead of {}",
165160
self.hir_map.node_to_string(hir_id),
166-
self.hir_map.def_path(hir_id.owner_local_def_id()).to_string_no_crate(),
167-
self.hir_map
168-
.def_path(LocalDefId { local_def_index: owner })
169-
.to_string_no_crate()
161+
self.hir_map.def_path(hir_id.owner).to_string_no_crate(),
162+
self.hir_map.def_path(owner).to_string_no_crate()
170163
)
171164
});
172165
}

src/librustc/hir/map/mod.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub struct IndexedHir<'hir> {
138138
/// The SVH of the local crate.
139139
pub crate_hash: Svh,
140140

141-
pub(super) map: IndexVec<DefIndex, HirOwnerData<'hir>>,
141+
pub(super) map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
142142
}
143143

144144
#[derive(Copy, Clone)]
@@ -345,10 +345,10 @@ impl<'hir> Map<'hir> {
345345

346346
fn get_entry(&self, id: HirId) -> Entry<'hir> {
347347
if id.local_id == ItemLocalId::from_u32(0) {
348-
let owner = self.tcx.hir_owner(id.owner_def_id());
348+
let owner = self.tcx.hir_owner(id.owner);
349349
Entry { parent: owner.parent, node: owner.node }
350350
} else {
351-
let owner = self.tcx.hir_owner_items(id.owner_def_id());
351+
let owner = self.tcx.hir_owner_items(id.owner);
352352
let item = owner.items[id.local_id].as_ref().unwrap();
353353
Entry { parent: HirId { owner: id.owner, local_id: item.parent }, node: item.node }
354354
}
@@ -376,11 +376,7 @@ impl<'hir> Map<'hir> {
376376
}
377377

378378
pub fn body(&self, id: BodyId) -> &'hir Body<'hir> {
379-
self.tcx
380-
.hir_owner_items(DefId::local(id.hir_id.owner))
381-
.bodies
382-
.get(&id.hir_id.local_id)
383-
.unwrap()
379+
self.tcx.hir_owner_items(id.hir_id.owner).bodies.get(&id.hir_id.local_id).unwrap()
384380
}
385381

386382
pub fn fn_decl_by_hir_id(&self, hir_id: HirId) -> Option<&'hir FnDecl<'hir>> {
@@ -494,7 +490,7 @@ impl<'hir> Map<'hir> {
494490
where
495491
V: ItemLikeVisitor<'hir>,
496492
{
497-
let module = self.tcx.hir_module_items(module);
493+
let module = self.tcx.hir_module_items(module.expect_local());
498494

499495
for id in &module.items {
500496
visitor.visit_item(self.expect_item(*id));

src/librustc/hir/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::ty::TyCtxt;
1111
use rustc_data_structures::fingerprint::Fingerprint;
1212
use rustc_data_structures::fx::FxHashMap;
1313
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
14-
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
14+
use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
1515
use rustc_hir::Body;
1616
use rustc_hir::HirId;
1717
use rustc_hir::ItemLocalId;
@@ -60,27 +60,27 @@ impl<'tcx> TyCtxt<'tcx> {
6060
map::Map { tcx: self }
6161
}
6262

63-
pub fn parent_module(self, id: HirId) -> DefId {
64-
self.parent_module_from_def_id(DefId::local(id.owner))
63+
pub fn parent_module(self, id: HirId) -> LocalDefId {
64+
self.parent_module_from_def_id(id.owner)
6565
}
6666
}
6767

6868
pub fn provide(providers: &mut Providers<'_>) {
6969
providers.parent_module_from_def_id = |tcx, id| {
7070
let hir = tcx.hir();
71-
hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id).unwrap()))
71+
hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id.to_def_id()).unwrap()))
72+
.expect_local()
7273
};
7374
providers.hir_crate = |tcx, _| tcx.untracked_crate;
7475
providers.index_hir = map::index_hir;
7576
providers.hir_module_items = |tcx, id| {
76-
assert_eq!(id.krate, LOCAL_CRATE);
7777
let hir = tcx.hir();
78-
let module = hir.as_local_hir_id(id).unwrap();
78+
let module = hir.as_local_hir_id(id.to_def_id()).unwrap();
7979
&tcx.untracked_crate.modules[&module]
8080
};
81-
providers.hir_owner = |tcx, id| tcx.index_hir(id.krate).map[id.index].signature.unwrap();
81+
providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature.unwrap();
8282
providers.hir_owner_items = |tcx, id| {
83-
tcx.index_hir(id.krate).map[id.index].with_bodies.as_ref().map(|items| &**items).unwrap()
83+
tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_ref().map(|items| &**items).unwrap()
8484
};
8585
map::provide(providers);
8686
}

src/librustc/ich/impls_hir.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {
2121
NodeIdHashingMode::HashDefPath => {
2222
let hir::HirId { owner, local_id } = hir_id;
2323

24-
hcx.local_def_path_hash(owner).hash_stable(hcx, hasher);
24+
hcx.local_def_path_hash(owner.local_def_index).hash_stable(hcx, hasher);
2525
local_id.hash_stable(hcx, hasher);
2626
}
2727
}
@@ -231,7 +231,7 @@ impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::TraitCandidate {
231231

232232
let import_keys = import_ids
233233
.iter()
234-
.map(|hir_id| (hcx.local_def_path_hash(hir_id.owner), hir_id.local_id))
234+
.map(|hir_id| (hcx.local_def_path_hash(hir_id.owner.local_def_index), hir_id.local_id))
235235
.collect();
236236
(hcx.def_path_hash(*def_id), import_keys)
237237
}

src/librustc/middle/region.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use crate::ich::{NodeIdHashingMode, StableHashingContext};
1010
use crate::ty::{self, DefIdTree, TyCtxt};
1111
use rustc_hir as hir;
12-
use rustc_hir::def_id::DefId;
1312
use rustc_hir::Node;
1413

1514
use rustc_data_structures::fx::FxHashMap;
@@ -594,7 +593,7 @@ impl<'tcx> ScopeTree {
594593
region scope tree for {:?} / {:?}",
595594
param_owner,
596595
self.root_parent.map(|id| tcx.hir().local_def_id(id)),
597-
self.root_body.map(|hir_id| DefId::local(hir_id.owner))
596+
self.root_body.map(|hir_id| hir_id.owner)
598597
),
599598
);
600599
}

src/librustc/query/mod.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,27 @@ rustc_queries! {
6666
// The items in a module.
6767
// This can be conveniently accessed by `tcx.hir().visit_item_likes_in_module`.
6868
// Avoid calling this query directly.
69-
query hir_module_items(key: DefId) -> &'tcx hir::ModuleItems {
69+
query hir_module_items(key: LocalDefId) -> &'tcx hir::ModuleItems {
7070
eval_always
71+
desc { |tcx| "HIR module items in `{}`", tcx.def_path_str(key.to_def_id()) }
7172
}
7273

73-
// An HIR item with a `DefId` that can own other HIR items which do not themselves have
74-
// a `DefId`.
74+
// An HIR item with a `LocalDefId` that can own other HIR items which do
75+
// not themselves have a `LocalDefId`.
7576
// This can be conveniently accessed by methods on `tcx.hir()`.
7677
// Avoid calling this query directly.
77-
query hir_owner(key: DefId) -> &'tcx HirOwner<'tcx> {
78+
query hir_owner(key: LocalDefId) -> &'tcx HirOwner<'tcx> {
7879
eval_always
80+
desc { |tcx| "HIR owner of `{}`", tcx.def_path_str(key.to_def_id()) }
7981
}
8082

81-
// The HIR items which do not themselves have a `DefId` and are owned by another HIR item
82-
// with a `DefId`.
83+
// The HIR items which do not themselves have a `LocalDefId` and are
84+
// owned by another HIR item with a `LocalDefId`.
8385
// This can be conveniently accessed by methods on `tcx.hir()`.
8486
// Avoid calling this query directly.
85-
query hir_owner_items(key: DefId) -> &'tcx HirOwnerItems<'tcx> {
87+
query hir_owner_items(key: LocalDefId) -> &'tcx HirOwnerItems<'tcx> {
8688
eval_always
89+
desc { |tcx| "HIR owner items in `{}`", tcx.def_path_str(key.to_def_id()) }
8790
}
8891

8992
/// Records the type of every item.
@@ -135,8 +138,9 @@ rustc_queries! {
135138
desc { "computing the lint levels for items in this crate" }
136139
}
137140

138-
query parent_module_from_def_id(_: DefId) -> DefId {
141+
query parent_module_from_def_id(key: LocalDefId) -> LocalDefId {
139142
eval_always
143+
desc { |tcx| "parent module of `{}`", tcx.def_path_str(key.to_def_id()) }
140144
}
141145
}
142146

0 commit comments

Comments
 (0)