Skip to content

File tree

6 files changed

+36
-42
lines changed

6 files changed

+36
-42
lines changed
 

‎src/librustc/dep_graph/dep_node.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
use mir::interpret::GlobalId;
6464
use hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX};
6565
use hir::map::DefPathHash;
66-
use hir::{HirId, ItemLocalId};
66+
use hir::HirId;
6767

6868
use ich::{Fingerprint, StableHashingContext};
6969
use rustc_data_structures::stable_hasher::{StableHasher, HashStable};
@@ -790,11 +790,11 @@ impl<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> DepNodeParams<'a, 'gcx, 'tcx> for HirId {
790790
fn to_fingerprint(&self, tcx: TyCtxt<'_, '_, '_>) -> Fingerprint {
791791
let HirId {
792792
owner,
793-
local_id: ItemLocalId(local_id),
793+
local_id,
794794
} = *self;
795795

796796
let def_path_hash = tcx.def_path_hash(DefId::local(owner));
797-
let local_id = Fingerprint::from_smaller_hash(local_id as u64);
797+
let local_id = Fingerprint::from_smaller_hash(local_id.as_u32().into());
798798

799799
def_path_hash.0.combine(local_id)
800800
}

‎src/librustc/hir/lowering.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ impl<'a> LoweringContext<'a> {
588588
*local_id_counter += 1;
589589
hir::HirId {
590590
owner: def_index,
591-
local_id: hir::ItemLocalId(local_id),
591+
local_id: hir::ItemLocalId::from_u32(local_id),
592592
}
593593
})
594594
}
@@ -616,7 +616,7 @@ impl<'a> LoweringContext<'a> {
616616

617617
hir::HirId {
618618
owner: def_index,
619-
local_id: hir::ItemLocalId(local_id),
619+
local_id: hir::ItemLocalId::from_u32(local_id),
620620
}
621621
})
622622
}

‎src/librustc/hir/map/hir_id_validator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl<'a, 'hir: 'a> HirIdValidator<'a, 'hir> {
101101
if max != self.hir_ids_seen.len() - 1 {
102102
// Collect the missing ItemLocalIds
103103
let missing: Vec<_> = (0 .. max as u32 + 1)
104-
.filter(|&i| !self.hir_ids_seen.contains_key(&ItemLocalId(i)))
104+
.filter(|&i| !self.hir_ids_seen.contains_key(&ItemLocalId::from_u32(i)))
105105
.collect();
106106

107107
// Try to map those to something more useful
@@ -110,7 +110,7 @@ impl<'a, 'hir: 'a> HirIdValidator<'a, 'hir> {
110110
for local_id in missing {
111111
let hir_id = HirId {
112112
owner: owner_def_index,
113-
local_id: ItemLocalId(local_id as u32),
113+
local_id: ItemLocalId::from_u32(local_id),
114114
};
115115

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

‎src/librustc/hir/mod.rs

+16-29
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use syntax::util::parser::ExprPrecedence;
3737
use ty::AdtKind;
3838
use ty::query::Providers;
3939

40-
use rustc_data_structures::indexed_vec;
4140
use rustc_data_structures::sync::{ParallelIterator, par_iter, Send, Sync, scope};
4241
use rustc_data_structures::thin_vec::ThinVec;
4342

@@ -121,48 +120,36 @@ impl serialize::UseSpecializedDecodable for HirId {
121120
}
122121
}
123122

124-
125-
/// An `ItemLocalId` uniquely identifies something within a given "item-like",
126-
/// that is within a hir::Item, hir::TraitItem, or hir::ImplItem. There is no
127-
/// guarantee that the numerical value of a given `ItemLocalId` corresponds to
128-
/// the node's position within the owning item in any way, but there is a
129-
/// guarantee that the `LocalItemId`s within an owner occupy a dense range of
130-
/// integers starting at zero, so a mapping that maps all or most nodes within
131-
/// an "item-like" to something else can be implement by a `Vec` instead of a
132-
/// tree or hash map.
133-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug,
134-
RustcEncodable, RustcDecodable)]
135-
pub struct ItemLocalId(pub u32);
136-
137-
impl ItemLocalId {
138-
pub fn as_usize(&self) -> usize {
139-
self.0 as usize
123+
// hack to ensure that we don't try to access the private parts of `NodeId` in this module
124+
mod item_local_id_inner {
125+
use rustc_data_structures::indexed_vec::Idx;
126+
/// An `ItemLocalId` uniquely identifies something within a given "item-like",
127+
/// that is within a hir::Item, hir::TraitItem, or hir::ImplItem. There is no
128+
/// guarantee that the numerical value of a given `ItemLocalId` corresponds to
129+
/// the node's position within the owning item in any way, but there is a
130+
/// guarantee that the `LocalItemId`s within an owner occupy a dense range of
131+
/// integers starting at zero, so a mapping that maps all or most nodes within
132+
/// an "item-like" to something else can be implement by a `Vec` instead of a
133+
/// tree or hash map.
134+
newtype_index! {
135+
pub struct ItemLocalId { .. }
140136
}
141137
}
142138

143-
impl indexed_vec::Idx for ItemLocalId {
144-
fn new(idx: usize) -> Self {
145-
debug_assert!((idx as u32) as usize == idx);
146-
ItemLocalId(idx as u32)
147-
}
148-
149-
fn index(self) -> usize {
150-
self.0 as usize
151-
}
152-
}
139+
pub use self::item_local_id_inner::ItemLocalId;
153140

154141
/// The `HirId` corresponding to CRATE_NODE_ID and CRATE_DEF_INDEX
155142
pub const CRATE_HIR_ID: HirId = HirId {
156143
owner: CRATE_DEF_INDEX,
157-
local_id: ItemLocalId(0)
144+
local_id: ItemLocalId::from_u32_const(0)
158145
};
159146

160147
pub const DUMMY_HIR_ID: HirId = HirId {
161148
owner: CRATE_DEF_INDEX,
162149
local_id: DUMMY_ITEM_LOCAL_ID,
163150
};
164151

165-
pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId(!0);
152+
pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId::MAX;
166153

167154
#[derive(Clone, RustcEncodable, RustcDecodable, Copy)]
168155
pub struct Label {

‎src/librustc/ich/impls_hir.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,14 @@ impl<'a> ToStableHashKey<StableHashingContext<'a>> for CrateNum {
7979
}
8080
}
8181

82-
impl_stable_hash_for!(tuple_struct hir::ItemLocalId { index });
82+
impl<'a> HashStable<StableHashingContext<'a>> for hir::ItemLocalId {
83+
#[inline]
84+
fn hash_stable<W: StableHasherResult>(&self,
85+
hcx: &mut StableHashingContext<'a>,
86+
hasher: &mut StableHasher<W>) {
87+
self.as_u32().hash_stable(hcx, hasher);
88+
}
89+
}
8390

8491
impl<'a> ToStableHashKey<StableHashingContext<'a>>
8592
for hir::ItemLocalId {
@@ -800,7 +807,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for hir::Mod {
800807
.iter()
801808
.map(|id| {
802809
let (def_path_hash, local_id) = id.id.to_stable_hash_key(hcx);
803-
debug_assert_eq!(local_id, hir::ItemLocalId(0));
810+
debug_assert_eq!(local_id, hir::ItemLocalId::from_u32(0));
804811
def_path_hash.0
805812
}).fold(Fingerprint::ZERO, |a, b| {
806813
a.combine_commutative(b)

‎src/librustc_driver/pretty.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
425425
pprust_hir::AnnNode::Item(item) => {
426426
s.s.space()?;
427427
s.synth_comment(format!("node_id: {} hir local_id: {}",
428-
item.id, item.hir_id.local_id.0))
428+
item.id, item.hir_id.local_id.as_u32()))
429429
}
430430
pprust_hir::AnnNode::SubItem(id) => {
431431
s.s.space()?;
@@ -434,18 +434,18 @@ impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
434434
pprust_hir::AnnNode::Block(blk) => {
435435
s.s.space()?;
436436
s.synth_comment(format!("block node_id: {} hir local_id: {}",
437-
blk.id, blk.hir_id.local_id.0))
437+
blk.id, blk.hir_id.local_id.as_u32()))
438438
}
439439
pprust_hir::AnnNode::Expr(expr) => {
440440
s.s.space()?;
441441
s.synth_comment(format!("node_id: {} hir local_id: {}",
442-
expr.id, expr.hir_id.local_id.0))?;
442+
expr.id, expr.hir_id.local_id.as_u32()))?;
443443
s.pclose()
444444
}
445445
pprust_hir::AnnNode::Pat(pat) => {
446446
s.s.space()?;
447447
s.synth_comment(format!("pat node_id: {} hir local_id: {}",
448-
pat.id, pat.hir_id.local_id.0))
448+
pat.id, pat.hir_id.local_id.as_u32()))
449449
}
450450
}
451451
}

0 commit comments

Comments
 (0)
Please sign in to comment.