Skip to content

Commit 8fb2a39

Browse files
committed
rustc: use LocalDefId instead of DefIndex in HirId.
1 parent 7463a4c commit 8fb2a39

23 files changed

+94
-104
lines changed

src/librustc/dep_graph/dep_node.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ impl<'tcx> DepNodeParams<'tcx> for HirId {
565565
local_id,
566566
} = *self;
567567

568-
let def_path_hash = tcx.def_path_hash(DefId::local(owner));
568+
let def_path_hash = tcx.def_path_hash(owner.to_def_id());
569569
let local_id = Fingerprint::from_smaller_hash(local_id.as_u32().into());
570570

571571
def_path_hash.0.combine(local_id)

src/librustc/hir/def_id.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl rustc_serialize::UseSpecializedDecodable for DefId {}
174174
/// few cases where we know that only DefIds from the local crate are expected
175175
/// and a DefId from a different crate would signify a bug somewhere. This
176176
/// is when LocalDefId comes in handy.
177-
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
177+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
178178
pub struct LocalDefId {
179179
pub index: DefIndex,
180180
}

src/librustc/hir/lowering.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ impl<'a> LoweringContext<'a> {
420420
let hir_id = self.lctx.allocate_hir_id_counter(id);
421421
self.allocate_use_tree_hir_id_counters(
422422
use_tree,
423-
hir_id.owner_local_def_id(),
423+
hir_id.owner,
424424
);
425425
}
426426
}
@@ -474,7 +474,7 @@ impl<'a> LoweringContext<'a> {
474474
ItemKind::Use(ref use_tree) => {
475475
self.allocate_use_tree_hir_id_counters(
476476
use_tree,
477-
hir_id.owner_local_def_id(),
477+
hir_id.owner,
478478
);
479479
}
480480
_ => {}
@@ -643,12 +643,12 @@ impl<'a> LoweringContext<'a> {
643643
/// properly. Calling the method twice with the same `NodeId` is fine though.
644644
fn lower_node_id(&mut self, ast_node_id: NodeId) -> hir::HirId {
645645
self.lower_node_id_generic(ast_node_id, |this| {
646-
let &mut (def_id, ref mut local_id_counter) =
646+
let &mut (owner, ref mut local_id_counter) =
647647
this.current_hir_id_owner.last_mut().unwrap();
648648
let local_id = *local_id_counter;
649649
*local_id_counter += 1;
650650
hir::HirId {
651-
owner: def_id.index,
651+
owner,
652652
local_id: hir::ItemLocalId::from_u32(local_id),
653653
}
654654
})
@@ -668,15 +668,16 @@ impl<'a> LoweringContext<'a> {
668668
debug_assert!(local_id != HIR_ID_COUNTER_LOCKED);
669669

670670
*local_id_counter += 1;
671-
let def_index = this
671+
let owner = this
672672
.resolver
673673
.definitions()
674-
.opt_def_index(owner)
674+
.opt_local_def_id(owner)
675675
.expect("you forgot to call `create_def_with_parent` or are lowering node-IDs \
676-
that do not belong to the current owner");
676+
that do not belong to the current owner")
677+
.assert_local();
677678

678679
hir::HirId {
679-
owner: def_index,
680+
owner,
680681
local_id: hir::ItemLocalId::from_u32(local_id),
681682
}
682683
})

src/librustc/hir/map/collector.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub(super) struct NodeCollector<'a, 'hir> {
3434

3535
// These fields keep track of the currently relevant DepNodes during
3636
// the visitor's traversal.
37-
current_dep_node_owner: DefIndex,
37+
current_dep_node_owner: LocalDefId,
3838
current_signature_dep_index: DepNodeIndex,
3939
current_full_dep_index: DepNodeIndex,
4040
currently_in_body: bool,
@@ -153,7 +153,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
153153
parent_node: hir::CRATE_HIR_ID,
154154
current_signature_dep_index: root_mod_sig_dep_index,
155155
current_full_dep_index: root_mod_full_dep_index,
156-
current_dep_node_owner: CRATE_DEF_INDEX,
156+
current_dep_node_owner: LocalDefId { index: CRATE_DEF_INDEX },
157157
currently_in_body: false,
158158
dep_graph,
159159
definitions,
@@ -227,7 +227,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
227227

228228
fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>) {
229229
debug!("hir_map: {:?} => {:?}", id, entry);
230-
let local_map = &mut self.map[id.owner];
230+
let local_map = &mut self.map[id.owner.index];
231231
let i = id.local_id.as_u32() as usize;
232232
let len = local_map.len();
233233
if i >= len {
@@ -274,10 +274,10 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
274274
self.source_map.span_to_string(span),
275275
node_str,
276276
self.definitions
277-
.def_path(self.current_dep_node_owner)
277+
.def_path(self.current_dep_node_owner.index)
278278
.to_string_no_crate(),
279279
self.current_dep_node_owner,
280-
self.definitions.def_path(hir_id.owner).to_string_no_crate(),
280+
self.definitions.def_path(hir_id.owner.index).to_string_no_crate(),
281281
hir_id.owner,
282282
forgot_str,
283283
)
@@ -300,15 +300,15 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
300300

301301
fn with_dep_node_owner<T: for<'b> HashStable<StableHashingContext<'b>>,
302302
F: FnOnce(&mut Self)>(&mut self,
303-
dep_node_owner: DefIndex,
303+
dep_node_owner: LocalDefId,
304304
item_like: &T,
305305
f: F) {
306306
let prev_owner = self.current_dep_node_owner;
307307
let prev_signature_dep_index = self.current_signature_dep_index;
308308
let prev_full_dep_index = self.current_full_dep_index;
309309
let prev_in_body = self.currently_in_body;
310310

311-
let def_path_hash = self.definitions.def_path_hash(dep_node_owner);
311+
let def_path_hash = self.definitions.def_path_hash(dep_node_owner.index);
312312

313313
let (signature_dep_index, full_dep_index) = alloc_hir_dep_nodes(
314314
self.dep_graph,
@@ -369,7 +369,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
369369

370370
fn visit_item(&mut self, i: &'hir Item) {
371371
debug!("visit_item: {:?}", i);
372-
debug_assert_eq!(i.hir_id.owner,
372+
debug_assert_eq!(i.hir_id.owner.index,
373373
self.definitions.opt_def_index(self.hir_to_node_id[&i.hir_id]).unwrap());
374374
self.with_dep_node_owner(i.hir_id.owner, i, |this| {
375375
this.insert(i.span, i.hir_id, Node::Item(i));
@@ -399,7 +399,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
399399
}
400400

401401
fn visit_trait_item(&mut self, ti: &'hir TraitItem) {
402-
debug_assert_eq!(ti.hir_id.owner,
402+
debug_assert_eq!(ti.hir_id.owner.index,
403403
self.definitions.opt_def_index(self.hir_to_node_id[&ti.hir_id]).unwrap());
404404
self.with_dep_node_owner(ti.hir_id.owner, ti, |this| {
405405
this.insert(ti.span, ti.hir_id, Node::TraitItem(ti));
@@ -411,7 +411,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
411411
}
412412

413413
fn visit_impl_item(&mut self, ii: &'hir ImplItem) {
414-
debug_assert_eq!(ii.hir_id.owner,
414+
debug_assert_eq!(ii.hir_id.owner.index,
415415
self.definitions.opt_def_index(self.hir_to_node_id[&ii.hir_id]).unwrap());
416416
self.with_dep_node_owner(ii.hir_id.owner, ii, |this| {
417417
this.insert(ii.span, ii.hir_id, Node::ImplItem(ii));
@@ -531,10 +531,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
531531
}
532532

533533
fn visit_macro_def(&mut self, macro_def: &'hir MacroDef) {
534-
let node_id = self.hir_to_node_id[&macro_def.hir_id];
535-
let def_index = self.definitions.opt_def_index(node_id).unwrap();
536-
537-
self.with_dep_node_owner(def_index, macro_def, |this| {
534+
self.with_dep_node_owner(macro_def.hir_id.owner, macro_def, |this| {
538535
this.insert(macro_def.span, macro_def.hir_id, Node::MacroDef(macro_def));
539536
});
540537
}

src/librustc/hir/map/hir_id_validator.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::hir::def_id::{DefIndex, CRATE_DEF_INDEX, LocalDefId};
1+
use crate::hir::def_id::{CRATE_DEF_INDEX, LocalDefId};
22
use crate::hir::{self, intravisit, HirId, ItemLocalId};
33
use crate::hir::itemlikevisit::ItemLikeVisitor;
44
use rustc_data_structures::fx::FxHashSet;
@@ -29,7 +29,7 @@ pub fn check_crate(hir_map: &hir::map::Map<'_>) {
2929

3030
struct HirIdValidator<'a, 'hir> {
3131
hir_map: &'a hir::map::Map<'hir>,
32-
owner_def_index: Option<DefIndex>,
32+
owner: Option<LocalDefId>,
3333
hir_ids_seen: FxHashSet<ItemLocalId>,
3434
errors: &'a Lock<Vec<String>>,
3535
}
@@ -45,7 +45,7 @@ impl<'a, 'hir> OuterVisitor<'a, 'hir> {
4545
-> HirIdValidator<'a, 'hir> {
4646
HirIdValidator {
4747
hir_map,
48-
owner_def_index: None,
48+
owner: None,
4949
hir_ids_seen: Default::default(),
5050
errors: self.errors,
5151
}
@@ -79,12 +79,12 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> {
7979
fn check<F: FnOnce(&mut HirIdValidator<'a, 'hir>)>(&mut self,
8080
hir_id: HirId,
8181
walk: F) {
82-
assert!(self.owner_def_index.is_none());
83-
let owner_def_index = self.hir_map.local_def_id(hir_id).index;
84-
self.owner_def_index = Some(owner_def_index);
82+
assert!(self.owner.is_none());
83+
let owner = self.hir_map.local_def_id(hir_id).assert_local();
84+
self.owner = Some(owner);
8585
walk(self);
8686

87-
if owner_def_index == CRATE_DEF_INDEX {
87+
if owner.index == CRATE_DEF_INDEX {
8888
return;
8989
}
9090

@@ -106,7 +106,7 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> {
106106

107107
for local_id in missing {
108108
let hir_id = HirId {
109-
owner: owner_def_index,
109+
owner,
110110
local_id: ItemLocalId::from_u32(local_id),
111111
};
112112

@@ -119,13 +119,13 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> {
119119
self.error(|| format!(
120120
"ItemLocalIds not assigned densely in {}. \
121121
Max ItemLocalId = {}, missing IDs = {:?}; seens IDs = {:?}",
122-
self.hir_map.def_path(LocalDefId { index: owner_def_index }).to_string_no_crate(),
122+
self.hir_map.def_path(owner).to_string_no_crate(),
123123
max,
124124
missing_items,
125125
self.hir_ids_seen
126126
.iter()
127127
.map(|&local_id| HirId {
128-
owner: owner_def_index,
128+
owner,
129129
local_id,
130130
})
131131
.map(|h| format!("({:?} {})", h, self.hir_map.node_to_string(h)))
@@ -142,7 +142,7 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
142142
}
143143

144144
fn visit_id(&mut self, hir_id: HirId) {
145-
let owner = self.owner_def_index.expect("no owner_def_index");
145+
let owner = self.owner.expect("no owner");
146146

147147
if hir_id == hir::DUMMY_HIR_ID {
148148
self.error(|| format!("HirIdValidator: HirId {:?} is invalid",
@@ -154,8 +154,8 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
154154
self.error(|| format!(
155155
"HirIdValidator: The recorded owner of {} is {} instead of {}",
156156
self.hir_map.node_to_string(hir_id),
157-
self.hir_map.def_path(hir_id.owner_local_def_id()).to_string_no_crate(),
158-
self.hir_map.def_path(LocalDefId { index: owner }).to_string_no_crate()));
157+
self.hir_map.def_path(hir_id.owner).to_string_no_crate(),
158+
self.hir_map.def_path(owner).to_string_no_crate()));
159159
}
160160

161161
self.hir_ids_seen.insert(hir_id.local_id);

src/librustc/hir/map/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl<'map> Iterator for ParentHirIterator<'map> {
222222
impl<'hir> Map<'hir> {
223223
#[inline]
224224
fn lookup(&self, id: HirId) -> Option<&Entry<'hir>> {
225-
let local_map = self.map.get(id.owner)?;
225+
let local_map = self.map.get(id.owner.index)?;
226226
local_map.get(id.local_id)?.as_ref()
227227
}
228228

@@ -466,8 +466,7 @@ impl<'hir> Map<'hir> {
466466
pub fn maybe_body_owned_by(&self, hir_id: HirId) -> Option<BodyId> {
467467
if let Some(entry) = self.find_entry(hir_id) {
468468
if self.dep_graph.is_fully_enabled() {
469-
let hir_id_owner = hir_id.owner;
470-
let def_path_hash = self.definitions.def_path_hash(hir_id_owner);
469+
let def_path_hash = self.definitions.def_path_hash(hir_id.owner.index);
471470
self.dep_graph.read(def_path_hash.to_dep_node(DepKind::HirBody));
472471
}
473472

@@ -646,8 +645,7 @@ impl<'hir> Map<'hir> {
646645
/// which can happen if the ID is not in the map itself or is just weird).
647646
pub fn get_parent_node(&self, hir_id: HirId) -> HirId {
648647
if self.dep_graph.is_fully_enabled() {
649-
let hir_id_owner = hir_id.owner;
650-
let def_path_hash = self.definitions.def_path_hash(hir_id_owner);
648+
let def_path_hash = self.definitions.def_path_hash(hir_id.owner.index);
651649
self.dep_graph.read(def_path_hash.to_dep_node(DepKind::HirBody));
652650
}
653651

@@ -1034,7 +1032,7 @@ impl<'hir> Map<'hir> {
10341032
local_map.iter_enumerated().filter_map(move |(i, entry)| entry.map(move |_| {
10351033
// Reconstruct the `HirId` based on the 3 indices we used to find it.
10361034
HirId {
1037-
owner,
1035+
owner: LocalDefId { index: owner },
10381036
local_id: i,
10391037
}
10401038
}))

src/librustc/hir/mod.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub mod ptr;
6464
pub mod upvars;
6565

6666
/// Uniquely identifies a node in the HIR of the current crate. It is
67-
/// composed of the `owner`, which is the `DefIndex` of the directly enclosing
67+
/// composed of the `owner`, which is the `LocalDefId` of the directly enclosing
6868
/// `hir::Item`, `hir::TraitItem`, or `hir::ImplItem` (i.e., the closest "item-like"),
6969
/// and the `local_id` which is unique within the given owner.
7070
///
@@ -75,38 +75,30 @@ pub mod upvars;
7575
/// the code base.
7676
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
7777
pub struct HirId {
78-
pub owner: DefIndex,
78+
pub owner: LocalDefId,
7979
pub local_id: ItemLocalId,
8080
}
8181

82-
impl HirId {
83-
pub fn owner_def_id(self) -> DefId {
84-
DefId::local(self.owner)
85-
}
86-
87-
pub fn owner_local_def_id(self) -> LocalDefId {
88-
LocalDefId {
89-
index: self.owner,
90-
}
91-
}
92-
}
93-
9482
impl rustc_serialize::UseSpecializedEncodable for HirId {
9583
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
9684
let HirId {
9785
owner,
9886
local_id,
9987
} = *self;
10088

101-
owner.encode(s)?;
89+
// HACK(eddyb) this avoids `LocalDefId` because that encodes as a `DefId`,
90+
// maybe try to fix that? (but it's unclear if anything relies on it)
91+
owner.index.encode(s)?;
10292
local_id.encode(s)?;
10393
Ok(())
10494
}
10595
}
10696

10797
impl rustc_serialize::UseSpecializedDecodable for HirId {
10898
fn default_decode<D: Decoder>(d: &mut D) -> Result<HirId, D::Error> {
109-
let owner = DefIndex::decode(d)?;
99+
// HACK(eddyb) this avoids `LocalDefId` because that encodes as a `DefId`,
100+
// maybe try to fix that? (but it's unclear if anything relies on it)
101+
let owner = LocalDefId { index: DefIndex::decode(d)? };
110102
let local_id = ItemLocalId::decode(d)?;
111103

112104
Ok(HirId {
@@ -145,12 +137,12 @@ pub use self::item_local_id_inner::ItemLocalId;
145137

146138
/// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_INDEX`.
147139
pub const CRATE_HIR_ID: HirId = HirId {
148-
owner: CRATE_DEF_INDEX,
140+
owner: LocalDefId { index: CRATE_DEF_INDEX },
149141
local_id: ItemLocalId::from_u32_const(0)
150142
};
151143

152144
pub const DUMMY_HIR_ID: HirId = HirId {
153-
owner: CRATE_DEF_INDEX,
145+
owner: LocalDefId { index: CRATE_DEF_INDEX },
154146
local_id: DUMMY_ITEM_LOCAL_ID,
155147
};
156148

src/librustc/ich/hcx.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for hir::HirId {
238238
local_id,
239239
} = *self;
240240

241-
hcx.local_def_path_hash(owner).hash_stable(hcx, hasher);
241+
hcx.local_def_path_hash(owner.index).hash_stable(hcx, hasher);
242242
local_id.hash_stable(hcx, hasher);
243243
}
244244
}
@@ -252,7 +252,7 @@ impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::HirId {
252252
fn to_stable_hash_key(&self,
253253
hcx: &StableHashingContext<'a>)
254254
-> (DefPathHash, hir::ItemLocalId) {
255-
let def_path_hash = hcx.local_def_path_hash(self.owner);
255+
let def_path_hash = hcx.local_def_path_hash(self.owner.index);
256256
(def_path_hash, self.local_id)
257257
}
258258
}

src/librustc/ich/impls_hir.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,10 @@ impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::TraitCandidate {
374374
import_ids,
375375
} = self;
376376

377-
let import_keys = import_ids.iter().map(|node_id| hcx.node_to_hir_id(*node_id))
378-
.map(|hir_id| (hcx.local_def_path_hash(hir_id.owner),
379-
hir_id.local_id)).collect();
377+
let import_keys = import_ids.iter()
378+
.map(|node_id| hcx.node_to_hir_id(*node_id))
379+
.map(|hir_id| (hcx.local_def_path_hash(hir_id.owner.index), hir_id.local_id))
380+
.collect();
380381
(hcx.def_path_hash(*def_id), import_keys)
381382
}
382383
}

0 commit comments

Comments
 (0)