Skip to content

Commit c676e35

Browse files
committed
Use ItemId as a strongly typed index.
1 parent ac8961f commit c676e35

File tree

28 files changed

+63
-51
lines changed

28 files changed

+63
-51
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
5858
self.lctx.with_hir_id_owner(item.id, |lctx| {
5959
lctx.without_in_scope_lifetime_defs(|lctx| {
6060
if let Some(hir_item) = lctx.lower_item(item) {
61-
item_hir_id = Some(hir_item.hir_id);
62-
lctx.insert_item(hir_item);
61+
let id = lctx.insert_item(hir_item);
62+
item_hir_id = Some(id);
6363
}
6464
})
6565
});
@@ -128,7 +128,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
128128
// only used when lowering a child item of a trait or impl.
129129
fn with_parent_item_lifetime_defs<T>(
130130
&mut self,
131-
parent_hir_id: hir::HirId,
131+
parent_hir_id: hir::ItemId,
132132
f: impl FnOnce(&mut LoweringContext<'_, '_>) -> T,
133133
) -> T {
134134
let old_len = self.in_scope_lifetimes.len();

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ struct LoweringContext<'a, 'hir: 'a> {
9999
arena: &'hir Arena<'hir>,
100100

101101
/// The items being lowered are collected here.
102-
items: BTreeMap<hir::HirId, hir::Item<'hir>>,
102+
items: BTreeMap<hir::ItemId, hir::Item<'hir>>,
103103

104104
trait_items: BTreeMap<hir::TraitItemId, hir::TraitItem<'hir>>,
105105
impl_items: BTreeMap<hir::ImplItemId, hir::ImplItem<'hir>>,
@@ -605,12 +605,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
605605
}
606606
}
607607

608-
fn insert_item(&mut self, item: hir::Item<'hir>) {
608+
fn insert_item(&mut self, item: hir::Item<'hir>) -> hir::ItemId {
609609
let id = item.hir_id;
610610
// FIXME: Use `debug_asset-rt`.
611611
assert_eq!(id.local_id, hir::ItemLocalId::from_u32(0));
612+
let id = hir::ItemId { id };
612613
self.items.insert(id, item);
613614
self.modules.get_mut(&self.current_module).unwrap().items.insert(id);
615+
id
614616
}
615617

616618
fn allocate_hir_id_counter(&mut self, owner: NodeId) -> hir::HirId {

compiler/rustc_hir/src/hir.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ pub struct WhereEqPredicate<'hir> {
619619
pub struct ModuleItems {
620620
// Use BTreeSets here so items are in the same order as in the
621621
// list of all items in Crate
622-
pub items: BTreeSet<HirId>,
622+
pub items: BTreeSet<ItemId>,
623623
pub trait_items: BTreeSet<TraitItemId>,
624624
pub impl_items: BTreeSet<ImplItemId>,
625625
pub foreign_items: BTreeSet<ForeignItemId>,
@@ -652,7 +652,7 @@ pub struct Crate<'hir> {
652652
// does, because it can affect the order in which errors are
653653
// detected, which in turn can make UI tests yield
654654
// slightly different results.
655-
pub items: BTreeMap<HirId, Item<'hir>>,
655+
pub items: BTreeMap<ItemId, Item<'hir>>,
656656

657657
pub trait_items: BTreeMap<TraitItemId, TraitItem<'hir>>,
658658
pub impl_items: BTreeMap<ImplItemId, ImplItem<'hir>>,
@@ -677,7 +677,7 @@ pub struct Crate<'hir> {
677677
}
678678

679679
impl Crate<'hir> {
680-
pub fn item(&self, id: HirId) -> &Item<'hir> {
680+
pub fn item(&self, id: ItemId) -> &Item<'hir> {
681681
&self.items[&id]
682682
}
683683

@@ -2541,7 +2541,7 @@ impl VariantData<'hir> {
25412541
// The bodies for items are stored "out of line", in a separate
25422542
// hashmap in the `Crate`. Here we just record the hir-id of the item
25432543
// so it can fetched later.
2544-
#[derive(Copy, Clone, Encodable, Debug)]
2544+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)]
25452545
pub struct ItemId {
25462546
pub id: HirId,
25472547
}

compiler/rustc_hir/src/intravisit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub trait Map<'hir> {
133133
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
134134
fn find(&self, hir_id: HirId) -> Option<Node<'hir>>;
135135
fn body(&self, id: BodyId) -> &'hir Body<'hir>;
136-
fn item(&self, id: HirId) -> &'hir Item<'hir>;
136+
fn item(&self, id: ItemId) -> &'hir Item<'hir>;
137137
fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>;
138138
fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir>;
139139
fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir>;
@@ -150,7 +150,7 @@ impl<'hir> Map<'hir> for ErasedMap<'hir> {
150150
fn body(&self, id: BodyId) -> &'hir Body<'hir> {
151151
self.0.body(id)
152152
}
153-
fn item(&self, id: HirId) -> &'hir Item<'hir> {
153+
fn item(&self, id: ItemId) -> &'hir Item<'hir> {
154154
self.0.item(id)
155155
}
156156
fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> {
@@ -269,7 +269,7 @@ pub trait Visitor<'v>: Sized {
269269
/// reason to override this method is if you want a nested pattern
270270
/// but cannot supply a `Map`; see `nested_visit_map` for advice.
271271
fn visit_nested_item(&mut self, id: ItemId) {
272-
let opt_item = self.nested_visit_map().inter().map(|map| map.item(id.id));
272+
let opt_item = self.nested_visit_map().inter().map(|map| map.item(id));
273273
walk_list!(self, visit_item, opt_item);
274274
}
275275

compiler/rustc_hir/src/stable_hash_impls.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
3434
}
3535
}
3636

37+
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
38+
type KeyType = (DefPathHash, ItemLocalId);
39+
40+
#[inline]
41+
fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
42+
self.id.to_stable_hash_key(hcx)
43+
}
44+
}
45+
3746
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
3847
type KeyType = (DefPathHash, ItemLocalId);
3948

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub const NO_ANN: &dyn PpAnn = &NoAnn;
5454
impl PpAnn for hir::Crate<'_> {
5555
fn nested(&self, state: &mut State<'_>, nested: Nested) {
5656
match nested {
57-
Nested::Item(id) => state.print_item(self.item(id.id)),
57+
Nested::Item(id) => state.print_item(self.item(id)),
5858
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
5959
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
6060
Nested::ForeignItem(id) => state.print_foreign_item(self.foreign_item(id)),
@@ -69,7 +69,7 @@ impl PpAnn for hir::Crate<'_> {
6969
impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> {
7070
fn nested(&self, state: &mut State<'_>, nested: Nested) {
7171
match nested {
72-
Nested::Item(id) => state.print_item(self.item(id.id)),
72+
Nested::Item(id) => state.print_item(self.item(id)),
7373
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
7474
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
7575
Nested::ForeignItem(id) => state.print_foreign_item(self.foreign_item(id)),

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
234234
}
235235
match fn_return.kind {
236236
TyKind::OpaqueDef(item_id, _) => {
237-
let item = tcx.hir().item(item_id.id);
237+
let item = tcx.hir().item(item_id);
238238
let opaque = if let ItemKind::OpaqueTy(opaque) = &item.kind {
239239
opaque
240240
} else {

compiler/rustc_lint/src/late.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
178178
}
179179

180180
fn visit_stmt(&mut self, s: &'tcx hir::Stmt<'tcx>) {
181-
let get_item = |id: hir::ItemId| self.context.tcx.hir().item(id.id);
181+
let get_item = |id: hir::ItemId| self.context.tcx.hir().item(id);
182182
let attrs = &s.kind.attrs(get_item);
183183
// See `EarlyContextAndPass::visit_stmt` for an explanation
184184
// of why we call `walk_stmt` outside of `with_lint_attrs`

compiler/rustc_middle/src/hir/map/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
309309

310310
fn visit_nested_item(&mut self, item: ItemId) {
311311
debug!("visit_nested_item: {:?}", item);
312-
self.visit_item(self.krate.item(item.id));
312+
self.visit_item(self.krate.item(item));
313313
}
314314

315315
fn visit_nested_trait_item(&mut self, item_id: TraitItemId) {

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,8 @@ impl<'hir> Map<'hir> {
300300
self.find_entry(id).unwrap()
301301
}
302302

303-
pub fn item(&self, id: HirId) -> &'hir Item<'hir> {
304-
match self.find(id).unwrap() {
303+
pub fn item(&self, id: ItemId) -> &'hir Item<'hir> {
304+
match self.find(id.id).unwrap() {
305305
Node::Item(item) => item,
306306
_ => bug!(),
307307
}
@@ -479,19 +479,19 @@ impl<'hir> Map<'hir> {
479479
let module = self.tcx.hir_module_items(module);
480480

481481
for id in &module.items {
482-
visitor.visit_item(self.expect_item(*id));
482+
visitor.visit_item(self.item(*id));
483483
}
484484

485485
for id in &module.trait_items {
486-
visitor.visit_trait_item(self.expect_trait_item(id.hir_id));
486+
visitor.visit_trait_item(self.trait_item(*id));
487487
}
488488

489489
for id in &module.impl_items {
490-
visitor.visit_impl_item(self.expect_impl_item(id.hir_id));
490+
visitor.visit_impl_item(self.impl_item(*id));
491491
}
492492

493493
for id in &module.foreign_items {
494-
visitor.visit_foreign_item(self.expect_foreign_item(id.hir_id));
494+
visitor.visit_foreign_item(self.foreign_item(*id));
495495
}
496496
}
497497

@@ -863,7 +863,7 @@ impl<'hir> Map<'hir> {
863863
Node::Variant(ref v) => &v.attrs[..],
864864
Node::Field(ref f) => &f.attrs[..],
865865
Node::Expr(ref e) => &*e.attrs,
866-
Node::Stmt(ref s) => s.kind.attrs(|id| self.item(id.id)),
866+
Node::Stmt(ref s) => s.kind.attrs(|id| self.item(id)),
867867
Node::Arm(ref a) => &*a.attrs,
868868
Node::GenericParam(param) => &param.attrs[..],
869869
// Unit/tuple structs/variants take the attributes straight from
@@ -977,7 +977,7 @@ impl<'hir> intravisit::Map<'hir> for Map<'hir> {
977977
self.body(id)
978978
}
979979

980-
fn item(&self, id: HirId) -> &'hir Item<'hir> {
980+
fn item(&self, id: ItemId) -> &'hir Item<'hir> {
981981
self.item(id)
982982
}
983983

0 commit comments

Comments
 (0)