Skip to content

Commit cebbba0

Browse files
committed
Only store a LocalDefId in hir::Item.
Items are guaranteed to be HIR owner.
1 parent bd3cd5d commit cebbba0

File tree

86 files changed

+483
-565
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+483
-565
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
197197

198198
node_ids
199199
.into_iter()
200-
.map(|node_id| hir::ItemId { id: self.allocate_hir_id_counter(node_id) })
200+
.map(|node_id| hir::ItemId {
201+
def_id: self.allocate_hir_id_counter(node_id).expect_owner(),
202+
})
201203
.collect()
202204
}
203205

@@ -250,7 +252,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
250252

251253
let kind = self.lower_item_kind(i.span, i.id, &mut ident, attrs, &mut vis, &i.kind);
252254

253-
Some(hir::Item { hir_id: self.lower_node_id(i.id), ident, attrs, kind, vis, span: i.span })
255+
Some(hir::Item {
256+
def_id: self.lower_node_id(i.id).expect_owner(),
257+
ident,
258+
attrs,
259+
kind,
260+
vis,
261+
span: i.span,
262+
})
254263
}
255264

256265
fn lower_item_kind(
@@ -557,7 +566,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
557566
let vis = this.rebuild_vis(&vis);
558567

559568
this.insert_item(hir::Item {
560-
hir_id: new_id,
569+
def_id: new_id.expect_owner(),
561570
ident,
562571
attrs,
563572
kind,
@@ -629,7 +638,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
629638
this.lower_use_tree(use_tree, &prefix, id, &mut vis, &mut ident, attrs);
630639

631640
this.insert_item(hir::Item {
632-
hir_id: new_hir_id,
641+
def_id: new_hir_id.expect_owner(),
633642
ident,
634643
attrs,
635644
kind,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
606606
}
607607

608608
fn insert_item(&mut self, item: hir::Item<'hir>) -> hir::ItemId {
609-
let id = item.hir_id;
610-
// FIXME: Use `debug_asset-rt`.
611-
assert_eq!(id.local_id, hir::ItemLocalId::from_u32(0));
612-
let id = hir::ItemId { id };
609+
let id = hir::ItemId { def_id: item.def_id };
613610
self.items.insert(id, item);
614611
self.modules.get_mut(&self.current_module).unwrap().items.insert(id);
615612
id
@@ -1549,29 +1546,27 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15491546
};
15501547

15511548
trace!("lower_opaque_impl_trait: {:#?}", opaque_ty_def_id);
1552-
let opaque_ty_id =
1553-
lctx.generate_opaque_type(opaque_ty_node_id, opaque_ty_item, span, opaque_ty_span);
1549+
lctx.generate_opaque_type(opaque_ty_def_id, opaque_ty_item, span, opaque_ty_span);
15541550

15551551
// `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
1556-
hir::TyKind::OpaqueDef(hir::ItemId { id: opaque_ty_id }, lifetimes)
1552+
hir::TyKind::OpaqueDef(hir::ItemId { def_id: opaque_ty_def_id }, lifetimes)
15571553
})
15581554
}
15591555

15601556
/// Registers a new opaque type with the proper `NodeId`s and
15611557
/// returns the lowered node-ID for the opaque type.
15621558
fn generate_opaque_type(
15631559
&mut self,
1564-
opaque_ty_node_id: NodeId,
1560+
opaque_ty_id: LocalDefId,
15651561
opaque_ty_item: hir::OpaqueTy<'hir>,
15661562
span: Span,
15671563
opaque_ty_span: Span,
1568-
) -> hir::HirId {
1564+
) {
15691565
let opaque_ty_item_kind = hir::ItemKind::OpaqueTy(opaque_ty_item);
1570-
let opaque_ty_id = self.lower_node_id(opaque_ty_node_id);
15711566
// Generate an `type Foo = impl Trait;` declaration.
15721567
trace!("registering opaque type with id {:#?}", opaque_ty_id);
15731568
let opaque_ty_item = hir::Item {
1574-
hir_id: opaque_ty_id,
1569+
def_id: opaque_ty_id,
15751570
ident: Ident::invalid(),
15761571
attrs: Default::default(),
15771572
kind: opaque_ty_item_kind,
@@ -1583,7 +1578,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15831578
// automatically for all AST items. But this opaque type item
15841579
// does not actually exist in the AST.
15851580
self.insert_item(opaque_ty_item);
1586-
opaque_ty_id
15871581
}
15881582

15891583
fn lifetimes_from_impl_trait_bounds(
@@ -2012,7 +2006,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20122006
// grow.
20132007
let input_lifetimes_count = self.in_scope_lifetimes.len() + self.lifetimes_to_define.len();
20142008

2015-
let (opaque_ty_id, lifetime_params) = self.with_hir_id_owner(opaque_ty_node_id, |this| {
2009+
let lifetime_params = self.with_hir_id_owner(opaque_ty_node_id, |this| {
20162010
// We have to be careful to get elision right here. The
20172011
// idea is that we create a lifetime parameter for each
20182012
// lifetime in the return type. So, given a return type
@@ -2063,10 +2057,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20632057
};
20642058

20652059
trace!("exist ty from async fn def id: {:#?}", opaque_ty_def_id);
2066-
let opaque_ty_id =
2067-
this.generate_opaque_type(opaque_ty_node_id, opaque_ty_item, span, opaque_ty_span);
2060+
this.generate_opaque_type(opaque_ty_def_id, opaque_ty_item, span, opaque_ty_span);
20682061

2069-
(opaque_ty_id, lifetime_params)
2062+
lifetime_params
20702063
});
20712064

20722065
// As documented above on the variable
@@ -2109,7 +2102,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21092102
// Foo = impl Trait` is, internally, created as a child of the
21102103
// async fn, so the *type parameters* are inherited. It's
21112104
// only the lifetime parameters that we must supply.
2112-
let opaque_ty_ref = hir::TyKind::OpaqueDef(hir::ItemId { id: opaque_ty_id }, generic_args);
2105+
let opaque_ty_ref =
2106+
hir::TyKind::OpaqueDef(hir::ItemId { def_id: opaque_ty_def_id }, generic_args);
21132107
let opaque_ty = self.ty(opaque_ty_span, opaque_ty_ref);
21142108
hir::FnRetTy::Return(self.arena.alloc(opaque_ty))
21152109
}
@@ -2434,7 +2428,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24342428
let mut ids: SmallVec<[hir::Stmt<'hir>; 1]> = item_ids
24352429
.into_iter()
24362430
.map(|item_id| {
2437-
let item_id = hir::ItemId { id: self.lower_node_id(item_id) };
2431+
let item_id = hir::ItemId {
2432+
// All the items that `lower_local` finds are `impl Trait` types.
2433+
def_id: self.lower_node_id(item_id).expect_owner(),
2434+
};
24382435
self.stmt(s.span, hir::StmtKind::Item(item_id))
24392436
})
24402437
.collect();

compiler/rustc_driver/src/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
237237
pprust_hir::AnnNode::Name(_) => {}
238238
pprust_hir::AnnNode::Item(item) => {
239239
s.s.space();
240-
s.synth_comment(format!("hir_id: {}", item.hir_id));
240+
s.synth_comment(format!("hir_id: {}", item.hir_id()));
241241
}
242242
pprust_hir::AnnNode::SubItem(id) => {
243243
s.s.space();

compiler/rustc_hir/src/hir.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,12 +2543,13 @@ impl VariantData<'hir> {
25432543
// so it can fetched later.
25442544
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug, Hash)]
25452545
pub struct ItemId {
2546-
pub id: HirId,
2546+
pub def_id: LocalDefId,
25472547
}
25482548

25492549
impl ItemId {
25502550
pub fn hir_id(&self) -> HirId {
2551-
self.id
2551+
// Items are always HIR owners.
2552+
HirId::make_owner(self.def_id)
25522553
}
25532554
}
25542555

@@ -2558,16 +2559,21 @@ impl ItemId {
25582559
#[derive(Debug)]
25592560
pub struct Item<'hir> {
25602561
pub ident: Ident,
2561-
pub hir_id: HirId,
2562+
pub def_id: LocalDefId,
25622563
pub attrs: &'hir [Attribute],
25632564
pub kind: ItemKind<'hir>,
25642565
pub vis: Visibility<'hir>,
25652566
pub span: Span,
25662567
}
25672568

25682569
impl Item<'_> {
2570+
pub fn hir_id(&self) -> HirId {
2571+
// Items are always HIR owners.
2572+
HirId::make_owner(self.def_id)
2573+
}
2574+
25692575
pub fn item_id(&self) -> ItemId {
2570-
ItemId { id: self.hir_id }
2576+
ItemId { def_id: self.def_id }
25712577
}
25722578
}
25732579

@@ -2879,8 +2885,8 @@ impl<'hir> Node<'hir> {
28792885

28802886
pub fn hir_id(&self) -> Option<HirId> {
28812887
match self {
2882-
Node::Item(Item { hir_id, .. })
2883-
| Node::ForeignItem(ForeignItem { hir_id, .. })
2888+
Node::Item(Item { def_id, .. }) => Some(HirId::make_owner(*def_id)),
2889+
Node::ForeignItem(ForeignItem { hir_id, .. })
28842890
| Node::TraitItem(TraitItem { hir_id, .. })
28852891
| Node::ImplItem(ImplItem { hir_id, .. })
28862892
| Node::Field(StructField { hir_id, .. })
@@ -2915,7 +2921,7 @@ mod size_asserts {
29152921
rustc_data_structures::static_assert_size!(super::QPath<'static>, 24);
29162922
rustc_data_structures::static_assert_size!(super::Ty<'static>, 72);
29172923

2918-
rustc_data_structures::static_assert_size!(super::Item<'static>, 208);
2924+
rustc_data_structures::static_assert_size!(super::Item<'static>, 200);
29192925
rustc_data_structures::static_assert_size!(super::TraitItem<'static>, 152);
29202926
rustc_data_structures::static_assert_size!(super::ImplItem<'static>, 168);
29212927
rustc_data_structures::static_assert_size!(super::ForeignItem<'static>, 160);

compiler/rustc_hir/src/hir_id.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ pub struct HirId {
1818
pub local_id: ItemLocalId,
1919
}
2020

21+
impl HirId {
22+
pub fn expect_owner(self) -> LocalDefId {
23+
assert_eq!(self.local_id.index(), 0);
24+
self.owner
25+
}
26+
27+
pub fn as_owner(self) -> Option<LocalDefId> {
28+
if self.local_id.index() == 0 { Some(self.owner) } else { None }
29+
}
30+
31+
pub fn make_owner(owner: LocalDefId) -> Self {
32+
Self { owner, local_id: ItemLocalId::from_u32(0) }
33+
}
34+
}
35+
2136
impl fmt::Display for HirId {
2237
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2338
write!(f, "{:?}", self)

compiler/rustc_hir/src/intravisit.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -565,16 +565,16 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
565565
visitor.visit_ident(item.ident);
566566
match item.kind {
567567
ItemKind::ExternCrate(orig_name) => {
568-
visitor.visit_id(item.hir_id);
568+
visitor.visit_id(item.hir_id());
569569
if let Some(orig_name) = orig_name {
570570
visitor.visit_name(item.span, orig_name);
571571
}
572572
}
573573
ItemKind::Use(ref path, _) => {
574-
visitor.visit_use(path, item.hir_id);
574+
visitor.visit_use(path, item.hir_id());
575575
}
576576
ItemKind::Static(ref typ, _, body) | ItemKind::Const(ref typ, body) => {
577-
visitor.visit_id(item.hir_id);
577+
visitor.visit_id(item.hir_id());
578578
visitor.visit_ty(typ);
579579
visitor.visit_nested_body(body);
580580
}
@@ -583,33 +583,33 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
583583
&sig.decl,
584584
body_id,
585585
item.span,
586-
item.hir_id,
586+
item.hir_id(),
587587
),
588588
ItemKind::Mod(ref module) => {
589589
// `visit_mod()` takes care of visiting the `Item`'s `HirId`.
590-
visitor.visit_mod(module, item.span, item.hir_id)
590+
visitor.visit_mod(module, item.span, item.hir_id())
591591
}
592592
ItemKind::ForeignMod { abi: _, items } => {
593-
visitor.visit_id(item.hir_id);
593+
visitor.visit_id(item.hir_id());
594594
walk_list!(visitor, visit_foreign_item_ref, items);
595595
}
596596
ItemKind::GlobalAsm(_) => {
597-
visitor.visit_id(item.hir_id);
597+
visitor.visit_id(item.hir_id());
598598
}
599599
ItemKind::TyAlias(ref ty, ref generics) => {
600-
visitor.visit_id(item.hir_id);
600+
visitor.visit_id(item.hir_id());
601601
visitor.visit_ty(ty);
602602
visitor.visit_generics(generics)
603603
}
604604
ItemKind::OpaqueTy(OpaqueTy { ref generics, bounds, .. }) => {
605-
visitor.visit_id(item.hir_id);
605+
visitor.visit_id(item.hir_id());
606606
walk_generics(visitor, generics);
607607
walk_list!(visitor, visit_param_bound, bounds);
608608
}
609609
ItemKind::Enum(ref enum_definition, ref generics) => {
610610
visitor.visit_generics(generics);
611611
// `visit_enum_def()` takes care of visiting the `Item`'s `HirId`.
612-
visitor.visit_enum_def(enum_definition, generics, item.hir_id, item.span)
612+
visitor.visit_enum_def(enum_definition, generics, item.hir_id(), item.span)
613613
}
614614
ItemKind::Impl(Impl {
615615
unsafety: _,
@@ -622,7 +622,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
622622
ref self_ty,
623623
items,
624624
}) => {
625-
visitor.visit_id(item.hir_id);
625+
visitor.visit_id(item.hir_id());
626626
visitor.visit_generics(generics);
627627
walk_list!(visitor, visit_trait_ref, of_trait);
628628
visitor.visit_ty(self_ty);
@@ -631,23 +631,23 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
631631
ItemKind::Struct(ref struct_definition, ref generics)
632632
| ItemKind::Union(ref struct_definition, ref generics) => {
633633
visitor.visit_generics(generics);
634-
visitor.visit_id(item.hir_id);
634+
visitor.visit_id(item.hir_id());
635635
visitor.visit_variant_data(
636636
struct_definition,
637637
item.ident.name,
638638
generics,
639-
item.hir_id,
639+
item.hir_id(),
640640
item.span,
641641
);
642642
}
643643
ItemKind::Trait(.., ref generics, bounds, trait_item_refs) => {
644-
visitor.visit_id(item.hir_id);
644+
visitor.visit_id(item.hir_id());
645645
visitor.visit_generics(generics);
646646
walk_list!(visitor, visit_param_bound, bounds);
647647
walk_list!(visitor, visit_trait_item_ref, trait_item_refs);
648648
}
649649
ItemKind::TraitAlias(ref generics, bounds) => {
650-
visitor.visit_id(item.hir_id);
650+
visitor.visit_id(item.hir_id());
651651
visitor.visit_generics(generics);
652652
walk_list!(visitor, visit_param_bound, bounds);
653653
}

compiler/rustc_hir/src/stable_hash_impls.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
3535
}
3636

3737
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
38-
type KeyType = (DefPathHash, ItemLocalId);
38+
type KeyType = DefPathHash;
3939

4040
#[inline]
41-
fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
42-
self.id.to_stable_hash_key(hcx)
41+
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
42+
hcx.local_def_path_hash(self.def_id)
4343
}
4444
}
4545

@@ -91,7 +91,7 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
9191

9292
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ItemId {
9393
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
94-
hcx.hash_reference_to_item(self.id, hasher)
94+
hcx.hash_reference_to_item(self.hir_id(), hasher)
9595
}
9696
}
9797

@@ -178,7 +178,7 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItem<'_> {
178178

179179
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
180180
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
181-
let Item { ident, ref attrs, hir_id: _, ref kind, ref vis, span } = *self;
181+
let Item { ident, ref attrs, def_id: _, ref kind, ref vis, span } = *self;
182182

183183
hcx.hash_hir_item_like(|hcx| {
184184
ident.name.hash_stable(hcx, hasher);

compiler/rustc_incremental/src/assert_dep_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl Visitor<'tcx> for IfThisChanged<'tcx> {
167167
}
168168

169169
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
170-
self.process_attrs(item.hir_id, &item.attrs);
170+
self.process_attrs(item.hir_id(), &item.attrs);
171171
intravisit::walk_item(self, item);
172172
}
173173

compiler/rustc_incremental/src/persist/dirty_clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ impl DirtyCleanVisitor<'tcx> {
450450

451451
impl ItemLikeVisitor<'tcx> for DirtyCleanVisitor<'tcx> {
452452
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
453-
self.check_item(item.hir_id, item.span);
453+
self.check_item(item.hir_id(), item.span);
454454
}
455455

456456
fn visit_trait_item(&mut self, item: &hir::TraitItem<'_>) {

compiler/rustc_interface/src/proc_macro_decls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct Finder<'tcx> {
2626
impl<'v> ItemLikeVisitor<'v> for Finder<'_> {
2727
fn visit_item(&mut self, item: &hir::Item<'_>) {
2828
if self.tcx.sess.contains_name(&item.attrs, sym::rustc_proc_macro_decls) {
29-
self.decls = Some(item.hir_id);
29+
self.decls = Some(item.hir_id());
3030
}
3131
}
3232

0 commit comments

Comments
 (0)