Skip to content

Commit 1d5829d

Browse files
authored
Rollup merge of rust-lang#55750 - oli-obk:node_id_x, r=michaelwoerister
Make `NodeId` and `HirLocalId` `newtype_index`
2 parents 756870a + 2203959 commit 1d5829d

File tree

14 files changed

+75
-100
lines changed

14 files changed

+75
-100
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 3 additions & 3 deletions
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/intravisit.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ use hir::map::{self, Map};
4949
use super::itemlikevisit::DeepVisitor;
5050

5151
use std::cmp;
52-
use std::u32;
5352

5453
#[derive(Copy, Clone)]
5554
pub enum FnKind<'a> {
@@ -1152,8 +1151,8 @@ pub struct IdRange {
11521151
impl IdRange {
11531152
pub fn max() -> IdRange {
11541153
IdRange {
1155-
min: NodeId::from_u32(u32::MAX),
1156-
max: NodeId::from_u32(u32::MIN),
1154+
min: NodeId::MAX,
1155+
max: NodeId::from_u32(0),
11571156
}
11581157
}
11591158

src/librustc/hir/lowering.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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);
@@ -124,7 +124,7 @@ impl<'a, 'hir: 'a> HirIdValidator<'a, 'hir> {
124124
.enumerate()
125125
.find(|&(_, &entry)| hir_id == entry)
126126
.expect("no node_to_hir_id entry");
127-
let node_id = NodeId::new(node_id);
127+
let node_id = NodeId::from_usize(node_id);
128128
missing_items.push(format!("[local_id: {}, node:{}]",
129129
local_id,
130130
self.hir_map.node_to_string(node_id)));

src/librustc/hir/mod.rs

Lines changed: 16 additions & 29 deletions
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 `ItemLocalId` 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

Lines changed: 9 additions & 2 deletions
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/session/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl Session {
393393

394394
match id.as_usize().checked_add(count) {
395395
Some(next) => {
396-
self.next_node_id.set(ast::NodeId::new(next));
396+
self.next_node_id.set(ast::NodeId::from_usize(next));
397397
}
398398
None => bug!("Input too large, ran out of node ids!"),
399399
}
@@ -1160,7 +1160,7 @@ pub fn build_session_(
11601160
recursion_limit: Once::new(),
11611161
type_length_limit: Once::new(),
11621162
const_eval_stack_frame_limit: 100,
1163-
next_node_id: OneThread::new(Cell::new(NodeId::new(1))),
1163+
next_node_id: OneThread::new(Cell::new(NodeId::from_u32(1))),
11641164
allocator_kind: Once::new(),
11651165
injected_panic_runtime: Once::new(),
11661166
imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),

src/librustc_driver/pretty.rs

Lines changed: 5 additions & 5 deletions
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
}
@@ -566,7 +566,7 @@ impl FromStr for UserIdentifiedItem {
566566
type Err = ();
567567
fn from_str(s: &str) -> Result<UserIdentifiedItem, ()> {
568568
Ok(s.parse()
569-
.map(ast::NodeId::new)
569+
.map(ast::NodeId::from_u32)
570570
.map(ItemViaNode)
571571
.unwrap_or_else(|_| ItemViaPath(s.split("::").map(|s| s.to_string()).collect())))
572572
}

src/librustc_driver/test.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,20 +232,20 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
232232
// children of 1, etc
233233

234234
let dscope = region::Scope {
235-
id: hir::ItemLocalId(1),
235+
id: hir::ItemLocalId::from_u32(1),
236236
data: region::ScopeData::Destruction,
237237
};
238238
self.region_scope_tree.record_scope_parent(dscope, None);
239239
self.create_region_hierarchy(
240240
&RH {
241-
id: hir::ItemLocalId(1),
241+
id: hir::ItemLocalId::from_u32(1),
242242
sub: &[
243243
RH {
244-
id: hir::ItemLocalId(10),
244+
id: hir::ItemLocalId::from_u32(10),
245245
sub: &[],
246246
},
247247
RH {
248-
id: hir::ItemLocalId(11),
248+
id: hir::ItemLocalId::from_u32(11),
249249
sub: &[],
250250
},
251251
],
@@ -400,7 +400,7 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
400400

401401
pub fn t_rptr_scope(&self, id: u32) -> Ty<'tcx> {
402402
let r = ty::ReScope(region::Scope {
403-
id: hir::ItemLocalId(id),
403+
id: hir::ItemLocalId::from_u32(id),
404404
data: region::ScopeData::Node,
405405
});
406406
self.infcx

src/librustc_resolve/resolve_imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
663663
let mut errors = false;
664664
let mut seen_spans = FxHashSet::default();
665665
let mut error_vec = Vec::new();
666-
let mut prev_root_id: NodeId = NodeId::new(0);
666+
let mut prev_root_id: NodeId = NodeId::from_u32(0);
667667
for i in 0 .. self.determined_imports.len() {
668668
let import = self.determined_imports[i];
669669
let error = self.finalize_import(import);

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ fn look_for_tests<'a, 'tcx: 'a, 'rcx: 'a, 'cstore: 'rcx>(
243243
if tests.found_tests == 0 {
244244
let mut diag = cx.tcx.struct_span_lint_node(
245245
lint::builtin::MISSING_DOC_CODE_EXAMPLES,
246-
NodeId::new(0),
246+
NodeId::from_u32(0),
247247
span_of_attrs(&item.attrs),
248248
"Missing code example in this documentation");
249249
diag.emit();
@@ -281,14 +281,14 @@ impl<'a, 'tcx, 'rcx, 'cstore> DocFolder for LinkCollector<'a, 'tcx, 'rcx, 'cstor
281281
let current_item = match item.inner {
282282
ModuleItem(..) => {
283283
if item.attrs.inner_docs {
284-
if item_node_id.unwrap() != NodeId::new(0) {
284+
if item_node_id.unwrap() != NodeId::from_u32(0) {
285285
item.name.clone()
286286
} else {
287287
None
288288
}
289289
} else {
290290
match parent_node.or(self.mod_ids.last().cloned()) {
291-
Some(parent) if parent != NodeId::new(0) => {
291+
Some(parent) if parent != NodeId::from_u32(0) => {
292292
//FIXME: can we pull the parent module's name from elsewhere?
293293
Some(self.cx.tcx.hir.name(parent).to_string())
294294
}
@@ -538,13 +538,13 @@ fn resolution_failure(
538538
);
539539

540540
diag = cx.tcx.struct_span_lint_node(lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE,
541-
NodeId::new(0),
541+
NodeId::from_u32(0),
542542
sp,
543543
&msg);
544544
diag.span_label(sp, "cannot be resolved, ignoring");
545545
} else {
546546
diag = cx.tcx.struct_span_lint_node(lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE,
547-
NodeId::new(0),
547+
NodeId::from_u32(0),
548548
sp,
549549
&msg);
550550

@@ -564,7 +564,7 @@ fn resolution_failure(
564564
diag
565565
} else {
566566
cx.tcx.struct_span_lint_node(lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE,
567-
NodeId::new(0),
567+
NodeId::from_u32(0),
568568
sp,
569569
&msg)
570570
};

0 commit comments

Comments
 (0)