Skip to content

Commit 996dc8d

Browse files
committed
Only store a LocalDefId in hir::ForeignItem.
1 parent 786a80e commit 996dc8d

File tree

32 files changed

+133
-110
lines changed

32 files changed

+133
-110
lines changed

Diff for: compiler/rustc_ast_lowering/src/item.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
111111
self.lctx.allocate_hir_id_counter(item.id);
112112
self.lctx.with_hir_id_owner(item.id, |lctx| {
113113
let hir_item = lctx.lower_foreign_item(item);
114-
let id = hir::ForeignItemId { hir_id: hir_item.hir_id };
114+
let id = hir_item.foreign_item_id();
115115
lctx.foreign_items.insert(id, hir_item);
116116
lctx.modules.get_mut(&lctx.current_module).unwrap().foreign_items.insert(id);
117117
});
@@ -711,7 +711,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
711711
fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem<'hir> {
712712
let def_id = self.resolver.local_def_id(i.id);
713713
hir::ForeignItem {
714-
hir_id: self.lower_node_id(i.id),
714+
def_id,
715715
ident: i.ident,
716716
attrs: self.lower_attrs(&i.attrs),
717717
kind: match i.kind {
@@ -746,7 +746,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
746746

747747
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef<'hir> {
748748
hir::ForeignItemRef {
749-
id: hir::ForeignItemId { hir_id: self.lower_node_id(i.id) },
749+
id: hir::ForeignItemId { def_id: self.lower_node_id(i.id).expect_owner() },
750750
ident: i.ident,
751751
span: i.span,
752752
vis: self.lower_visibility(&i.vis, Some(i.id)),

Diff for: compiler/rustc_hir/src/hir.rs

+25-8
Original file line numberDiff line numberDiff line change
@@ -2783,7 +2783,14 @@ pub enum AssocItemKind {
27832783
// so it can fetched later.
27842784
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)]
27852785
pub struct ForeignItemId {
2786-
pub hir_id: HirId,
2786+
pub def_id: LocalDefId,
2787+
}
2788+
2789+
impl ForeignItemId {
2790+
pub fn hir_id(&self) -> HirId {
2791+
// Items are always HIR owners.
2792+
HirId::make_owner(self.def_id)
2793+
}
27872794
}
27882795

27892796
/// A reference from a foreign block to one of its items. This
@@ -2801,17 +2808,27 @@ pub struct ForeignItemRef<'hir> {
28012808
pub vis: Visibility<'hir>,
28022809
}
28032810

2804-
#[derive(Debug, HashStable_Generic)]
2811+
#[derive(Debug)]
28052812
pub struct ForeignItem<'hir> {
2806-
#[stable_hasher(project(name))]
28072813
pub ident: Ident,
28082814
pub attrs: &'hir [Attribute],
28092815
pub kind: ForeignItemKind<'hir>,
2810-
pub hir_id: HirId,
2816+
pub def_id: LocalDefId,
28112817
pub span: Span,
28122818
pub vis: Visibility<'hir>,
28132819
}
28142820

2821+
impl ForeignItem<'_> {
2822+
pub fn hir_id(&self) -> HirId {
2823+
// Items are always HIR owners.
2824+
HirId::make_owner(self.def_id)
2825+
}
2826+
2827+
pub fn foreign_item_id(&self) -> ForeignItemId {
2828+
ForeignItemId { def_id: self.def_id }
2829+
}
2830+
}
2831+
28152832
/// An item within an `extern` block.
28162833
#[derive(Debug, HashStable_Generic)]
28172834
pub enum ForeignItemKind<'hir> {
@@ -2923,9 +2940,9 @@ impl<'hir> Node<'hir> {
29232940
match self {
29242941
Node::Item(Item { def_id, .. })
29252942
| Node::TraitItem(TraitItem { def_id, .. })
2926-
| Node::ImplItem(ImplItem { def_id, .. }) => Some(HirId::make_owner(*def_id)),
2927-
Node::ForeignItem(ForeignItem { hir_id, .. })
2928-
| Node::Field(StructField { hir_id, .. })
2943+
| Node::ImplItem(ImplItem { def_id, .. })
2944+
| Node::ForeignItem(ForeignItem { def_id, .. }) => Some(HirId::make_owner(*def_id)),
2945+
Node::Field(StructField { hir_id, .. })
29292946
| Node::AnonConst(AnonConst { hir_id, .. })
29302947
| Node::Expr(Expr { hir_id, .. })
29312948
| Node::Stmt(Stmt { hir_id, .. })
@@ -2960,5 +2977,5 @@ mod size_asserts {
29602977
rustc_data_structures::static_assert_size!(super::Item<'static>, 200);
29612978
rustc_data_structures::static_assert_size!(super::TraitItem<'static>, 144);
29622979
rustc_data_structures::static_assert_size!(super::ImplItem<'static>, 168);
2963-
rustc_data_structures::static_assert_size!(super::ForeignItem<'static>, 160);
2980+
rustc_data_structures::static_assert_size!(super::ForeignItem<'static>, 152);
29642981
}

Diff for: compiler/rustc_hir/src/intravisit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) {
836836
}
837837

838838
pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v ForeignItem<'v>) {
839-
visitor.visit_id(foreign_item.hir_id);
839+
visitor.visit_id(foreign_item.hir_id());
840840
visitor.visit_vis(&foreign_item.vis);
841841
visitor.visit_ident(foreign_item.ident);
842842

Diff for: compiler/rustc_hir/src/stable_hash_impls.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
22

33
use crate::hir::{
4-
BodyId, Expr, ForeignItemId, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem, TraitItemId,
5-
Ty, VisibilityKind,
4+
BodyId, Expr, ForeignItem, ForeignItemId, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem,
5+
TraitItemId, Ty, VisibilityKind,
66
};
77
use crate::hir_id::{HirId, ItemLocalId};
88
use rustc_span::def_id::{DefPathHash, LocalDefId};
@@ -62,11 +62,11 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
6262
}
6363

6464
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
65-
type KeyType = (DefPathHash, ItemLocalId);
65+
type KeyType = DefPathHash;
6666

6767
#[inline]
68-
fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
69-
self.hir_id.to_stable_hash_key(hcx)
68+
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
69+
hcx.local_def_path_hash(self.def_id)
7070
}
7171
}
7272

@@ -97,7 +97,7 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ItemId {
9797

9898
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItemId {
9999
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
100-
hcx.hash_reference_to_item(self.hir_id, hasher)
100+
hcx.hash_reference_to_item(self.hir_id(), hasher)
101101
}
102102
}
103103

@@ -176,6 +176,20 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItem<'_> {
176176
}
177177
}
178178

179+
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItem<'_> {
180+
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
181+
let ForeignItem { def_id: _, ident, ref attrs, ref kind, span, ref vis } = *self;
182+
183+
hcx.hash_hir_item_like(|hcx| {
184+
ident.name.hash_stable(hcx, hasher);
185+
attrs.hash_stable(hcx, hasher);
186+
kind.hash_stable(hcx, hasher);
187+
span.hash_stable(hcx, hasher);
188+
vis.hash_stable(hcx, hasher);
189+
});
190+
}
191+
}
192+
179193
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
180194
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
181195
let Item { ident, ref attrs, def_id: _, ref kind, ref vis, span } = *self;

Diff for: compiler/rustc_incremental/src/persist/dirty_clean.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ impl ItemLikeVisitor<'tcx> for DirtyCleanVisitor<'tcx> {
462462
}
463463

464464
fn visit_foreign_item(&mut self, item: &hir::ForeignItem<'_>) {
465-
self.check_item(item.hir_id, item.span);
465+
self.check_item(item.hir_id(), item.span);
466466
}
467467
}
468468

Diff for: compiler/rustc_lint/src/builtin.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -660,11 +660,10 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
660660
}
661661

662662
fn check_foreign_item(&mut self, cx: &LateContext<'_>, foreign_item: &hir::ForeignItem<'_>) {
663-
let def_id = cx.tcx.hir().local_def_id(foreign_item.hir_id);
664-
let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());
663+
let (article, desc) = cx.tcx.article_and_description(foreign_item.def_id.to_def_id());
665664
self.check_missing_docs_attrs(
666665
cx,
667-
Some(foreign_item.hir_id),
666+
Some(foreign_item.hir_id()),
668667
&foreign_item.attrs,
669668
foreign_item.span,
670669
article,
@@ -1365,7 +1364,7 @@ impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
13651364
self.perform_lint(
13661365
cx,
13671366
"item",
1368-
foreign_item.hir_id,
1367+
foreign_item.hir_id(),
13691368
&foreign_item.vis,
13701369
foreign_item.span,
13711370
true,
@@ -2675,10 +2674,7 @@ impl ClashingExternDeclarations {
26752674
/// Insert a new foreign item into the seen set. If a symbol with the same name already exists
26762675
/// for the item, return its HirId without updating the set.
26772676
fn insert(&mut self, tcx: TyCtxt<'_>, fi: &hir::ForeignItem<'_>) -> Option<HirId> {
2678-
let hid = fi.hir_id;
2679-
2680-
let local_did = tcx.hir().local_def_id(fi.hir_id);
2681-
let did = local_did.to_def_id();
2677+
let did = fi.def_id.to_def_id();
26822678
let instance = Instance::new(did, ty::List::identity_for_item(tcx, did));
26832679
let name = Symbol::intern(tcx.symbol_name(instance).name);
26842680
if let Some(&hir_id) = self.seen_decls.get(&name) {
@@ -2687,24 +2683,23 @@ impl ClashingExternDeclarations {
26872683
// This lets us avoid emitting "knock-on" diagnostics.
26882684
Some(hir_id)
26892685
} else {
2690-
self.seen_decls.insert(name, hid)
2686+
self.seen_decls.insert(name, fi.hir_id())
26912687
}
26922688
}
26932689

26942690
/// Get the name of the symbol that's linked against for a given extern declaration. That is,
26952691
/// the name specified in a #[link_name = ...] attribute if one was specified, else, just the
26962692
/// symbol's name.
26972693
fn name_of_extern_decl(tcx: TyCtxt<'_>, fi: &hir::ForeignItem<'_>) -> SymbolName {
2698-
let did = tcx.hir().local_def_id(fi.hir_id);
26992694
if let Some((overridden_link_name, overridden_link_name_span)) =
2700-
tcx.codegen_fn_attrs(did).link_name.map(|overridden_link_name| {
2695+
tcx.codegen_fn_attrs(fi.def_id).link_name.map(|overridden_link_name| {
27012696
// FIXME: Instead of searching through the attributes again to get span
27022697
// information, we could have codegen_fn_attrs also give span information back for
27032698
// where the attribute was defined. However, until this is found to be a
27042699
// bottleneck, this does just fine.
27052700
(
27062701
overridden_link_name,
2707-
tcx.get_attrs(did.to_def_id())
2702+
tcx.get_attrs(fi.def_id.to_def_id())
27082703
.iter()
27092704
.find(|at| tcx.sess.check_name(at, sym::link_name))
27102705
.unwrap()
@@ -2932,10 +2927,10 @@ impl<'tcx> LateLintPass<'tcx> for ClashingExternDeclarations {
29322927
let tcx = cx.tcx;
29332928
if let Some(existing_hid) = self.insert(tcx, this_fi) {
29342929
let existing_decl_ty = tcx.type_of(tcx.hir().local_def_id(existing_hid));
2935-
let this_decl_ty = tcx.type_of(tcx.hir().local_def_id(this_fi.hir_id));
2930+
let this_decl_ty = tcx.type_of(this_fi.def_id);
29362931
debug!(
29372932
"ClashingExternDeclarations: Comparing existing {:?}: {:?} to this {:?}: {:?}",
2938-
existing_hid, existing_decl_ty, this_fi.hir_id, this_decl_ty
2933+
existing_hid, existing_decl_ty, this_fi.def_id, this_decl_ty
29392934
);
29402935
// Check that the declarations match.
29412936
if !Self::structurally_same_type(
@@ -2957,7 +2952,7 @@ impl<'tcx> LateLintPass<'tcx> for ClashingExternDeclarations {
29572952
// Finally, emit the diagnostic.
29582953
tcx.struct_span_lint_hir(
29592954
CLASHING_EXTERN_DECLARATIONS,
2960-
this_fi.hir_id,
2955+
this_fi.hir_id(),
29612956
get_relevant_span(this_fi),
29622957
|lint| {
29632958
let mut expected_str = DiagnosticStyledString::new();

Diff for: compiler/rustc_lint/src/late.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
155155
}
156156

157157
fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) {
158-
self.with_lint_attrs(it.hir_id, &it.attrs, |cx| {
159-
cx.with_param_env(it.hir_id, |cx| {
158+
self.with_lint_attrs(it.hir_id(), &it.attrs, |cx| {
159+
cx.with_param_env(it.hir_id(), |cx| {
160160
lint_callback!(cx, check_foreign_item, it);
161161
hir_visit::walk_foreign_item(cx, it);
162162
lint_callback!(cx, check_foreign_item_post, it);

Diff for: compiler/rustc_lint/src/levels.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ impl<'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'_, 'tcx> {
583583
}
584584

585585
fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) {
586-
self.with_lint_attrs(it.hir_id, &it.attrs, |builder| {
586+
self.with_lint_attrs(it.hir_id(), &it.attrs, |builder| {
587587
intravisit::walk_foreign_item(builder, it);
588588
})
589589
}

Diff for: compiler/rustc_lint/src/types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1262,15 +1262,15 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12621262
impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDeclarations {
12631263
fn check_foreign_item(&mut self, cx: &LateContext<'_>, it: &hir::ForeignItem<'_>) {
12641264
let mut vis = ImproperCTypesVisitor { cx, mode: CItemKind::Declaration };
1265-
let abi = cx.tcx.hir().get_foreign_abi(it.hir_id);
1265+
let abi = cx.tcx.hir().get_foreign_abi(it.hir_id());
12661266

12671267
if !vis.is_internal_abi(abi) {
12681268
match it.kind {
12691269
hir::ForeignItemKind::Fn(ref decl, _, _) => {
1270-
vis.check_foreign_fn(it.hir_id, decl);
1270+
vis.check_foreign_fn(it.hir_id(), decl);
12711271
}
12721272
hir::ForeignItemKind::Static(ref ty, _) => {
1273-
vis.check_foreign_static(it.hir_id, ty.span);
1273+
vis.check_foreign_static(it.hir_id(), ty.span);
12741274
}
12751275
hir::ForeignItemKind::Type => (),
12761276
}

Diff for: compiler/rustc_metadata/src/foreign_modules.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,23 @@ use rustc_middle::middle::cstore::ForeignModule;
44
use rustc_middle::ty::TyCtxt;
55

66
crate fn collect(tcx: TyCtxt<'_>) -> Vec<ForeignModule> {
7-
let mut collector = Collector { tcx, modules: Vec::new() };
7+
let mut collector = Collector { modules: Vec::new() };
88
tcx.hir().krate().visit_all_item_likes(&mut collector);
99
collector.modules
1010
}
1111

12-
struct Collector<'tcx> {
13-
tcx: TyCtxt<'tcx>,
12+
struct Collector {
1413
modules: Vec<ForeignModule>,
1514
}
1615

17-
impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
16+
impl ItemLikeVisitor<'tcx> for Collector {
1817
fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
1918
let items = match it.kind {
2019
hir::ItemKind::ForeignMod { items, .. } => items,
2120
_ => return,
2221
};
2322

24-
let foreign_items =
25-
items.iter().map(|it| self.tcx.hir().local_def_id(it.id.hir_id).to_def_id()).collect();
23+
let foreign_items = items.iter().map(|it| it.id.def_id.to_def_id()).collect();
2624
self.modules.push(ForeignModule { foreign_items, def_id: it.def_id.to_def_id() });
2725
}
2826

Diff for: compiler/rustc_metadata/src/rmeta/encoder.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1411,8 +1411,7 @@ impl EncodeContext<'a, 'tcx> {
14111411
hir::ItemKind::ForeignMod { items, .. } => record!(self.tables.children[def_id] <-
14121412
items
14131413
.iter()
1414-
.map(|foreign_item| tcx.hir().local_def_id(
1415-
foreign_item.id.hir_id).local_def_index)
1414+
.map(|foreign_item| foreign_item.id.def_id.local_def_index)
14161415
),
14171416
hir::ItemKind::Enum(..) => record!(self.tables.children[def_id] <-
14181417
self.tcx.adt_def(def_id).variants.iter().map(|v| {
@@ -1859,8 +1858,7 @@ impl Visitor<'tcx> for EncodeContext<'a, 'tcx> {
18591858
}
18601859
fn visit_foreign_item(&mut self, ni: &'tcx hir::ForeignItem<'tcx>) {
18611860
intravisit::walk_foreign_item(self, ni);
1862-
let def_id = self.tcx.hir().local_def_id(ni.hir_id);
1863-
self.encode_info_for_foreign_item(def_id.to_def_id(), ni);
1861+
self.encode_info_for_foreign_item(ni.def_id.to_def_id(), ni);
18641862
}
18651863
fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
18661864
intravisit::walk_generics(self, generics);

Diff for: compiler/rustc_middle/src/hir/map/collector.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,10 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
354354
}
355355

356356
fn visit_foreign_item(&mut self, fi: &'hir ForeignItem<'hir>) {
357-
debug_assert_eq!(
358-
fi.hir_id.owner,
359-
self.definitions.opt_hir_id_to_local_def_id(fi.hir_id).unwrap()
360-
);
361-
self.with_dep_node_owner(fi.hir_id.owner, fi, |this, hash| {
362-
this.insert_with_hash(fi.span, fi.hir_id, Node::ForeignItem(fi), hash);
357+
self.with_dep_node_owner(fi.def_id, fi, |this, hash| {
358+
this.insert_with_hash(fi.span, fi.hir_id(), Node::ForeignItem(fi), hash);
363359

364-
this.with_parent(fi.hir_id, |this| {
360+
this.with_parent(fi.hir_id(), |this| {
365361
intravisit::walk_foreign_item(this, fi);
366362
});
367363
});

Diff for: compiler/rustc_middle/src/hir/map/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl<'hir> Map<'hir> {
322322
}
323323

324324
pub fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir> {
325-
match self.find(id.hir_id).unwrap() {
325+
match self.find(id.hir_id()).unwrap() {
326326
Node::ForeignItem(item) => item,
327327
_ => bug!(),
328328
}

Diff for: compiler/rustc_passes/src/check_attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
11101110
fn visit_foreign_item(&mut self, f_item: &'tcx ForeignItem<'tcx>) {
11111111
let target = Target::from_foreign_item(f_item);
11121112
self.check_attributes(
1113-
f_item.hir_id,
1113+
f_item.hir_id(),
11141114
&f_item.attrs,
11151115
&f_item.span,
11161116
target,

0 commit comments

Comments
 (0)