Skip to content

Commit 786a80e

Browse files
committed
Only store a LocalDefId in hir::ImplItem.
1 parent a871a0f commit 786a80e

File tree

56 files changed

+163
-165
lines changed

Some content is hidden

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

56 files changed

+163
-165
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
9898
}
9999
AssocCtxt::Impl => {
100100
let hir_item = lctx.lower_impl_item(item);
101-
let id = hir::ImplItemId { hir_id: hir_item.hir_id };
101+
let id = hir_item.impl_item_id();
102102
lctx.impl_items.insert(id, hir_item);
103103
lctx.modules.get_mut(&lctx.current_module).unwrap().impl_items.insert(id);
104104
}
@@ -931,7 +931,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
931931
let has_value = true;
932932
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
933933
hir::ImplItem {
934-
hir_id: self.lower_node_id(i.id),
934+
def_id: self.lower_node_id(i.id).expect_owner(),
935935
ident: i.ident,
936936
attrs: self.lower_attrs(&i.attrs),
937937
generics,
@@ -947,7 +947,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
947947
let has_value = true;
948948
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
949949
hir::ImplItemRef {
950-
id: hir::ImplItemId { hir_id: self.lower_node_id(i.id) },
950+
id: hir::ImplItemId { def_id: self.lower_node_id(i.id).expect_owner() },
951951
ident: i.ident,
952952
span: i.span,
953953
vis: self.lower_visibility(&i.vis, Some(i.id)),

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

+23-6
Original file line numberDiff line numberDiff line change
@@ -1969,14 +1969,21 @@ pub enum TraitItemKind<'hir> {
19691969
// so it can fetched later.
19701970
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)]
19711971
pub struct ImplItemId {
1972-
pub hir_id: HirId,
1972+
pub def_id: LocalDefId,
1973+
}
1974+
1975+
impl ImplItemId {
1976+
pub fn hir_id(&self) -> HirId {
1977+
// Items are always HIR owners.
1978+
HirId::make_owner(self.def_id)
1979+
}
19731980
}
19741981

19751982
/// Represents anything within an `impl` block.
19761983
#[derive(Debug)]
19771984
pub struct ImplItem<'hir> {
19781985
pub ident: Ident,
1979-
pub hir_id: HirId,
1986+
pub def_id: LocalDefId,
19801987
pub vis: Visibility<'hir>,
19811988
pub defaultness: Defaultness,
19821989
pub attrs: &'hir [Attribute],
@@ -1985,6 +1992,17 @@ pub struct ImplItem<'hir> {
19851992
pub span: Span,
19861993
}
19871994

1995+
impl ImplItem<'_> {
1996+
pub fn hir_id(&self) -> HirId {
1997+
// Items are always HIR owners.
1998+
HirId::make_owner(self.def_id)
1999+
}
2000+
2001+
pub fn impl_item_id(&self) -> ImplItemId {
2002+
ImplItemId { def_id: self.def_id }
2003+
}
2004+
}
2005+
19882006
/// Represents various kinds of content within an `impl`.
19892007
#[derive(Debug, HashStable_Generic)]
19902008
pub enum ImplItemKind<'hir> {
@@ -2903,11 +2921,10 @@ impl<'hir> Node<'hir> {
29032921

29042922
pub fn hir_id(&self) -> Option<HirId> {
29052923
match self {
2906-
Node::Item(Item { def_id, .. }) | Node::TraitItem(TraitItem { def_id, .. }) => {
2907-
Some(HirId::make_owner(*def_id))
2908-
}
2924+
Node::Item(Item { def_id, .. })
2925+
| Node::TraitItem(TraitItem { def_id, .. })
2926+
| Node::ImplItem(ImplItem { def_id, .. }) => Some(HirId::make_owner(*def_id)),
29092927
Node::ForeignItem(ForeignItem { hir_id, .. })
2910-
| Node::ImplItem(ImplItem { hir_id, .. })
29112928
| Node::Field(StructField { hir_id, .. })
29122929
| Node::AnonConst(AnonConst { hir_id, .. })
29132930
| Node::Expr(Expr { hir_id, .. })

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref:
10041004
pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem<'v>) {
10051005
// N.B., deliberately force a compilation error if/when new fields are added.
10061006
let ImplItem {
1007-
hir_id: _,
1007+
def_id: _,
10081008
ident,
10091009
ref vis,
10101010
ref defaultness,
@@ -1021,7 +1021,7 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
10211021
visitor.visit_generics(generics);
10221022
match *kind {
10231023
ImplItemKind::Const(ref ty, body) => {
1024-
visitor.visit_id(impl_item.hir_id);
1024+
visitor.visit_id(impl_item.hir_id());
10251025
visitor.visit_ty(ty);
10261026
visitor.visit_nested_body(body);
10271027
}
@@ -1031,11 +1031,11 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
10311031
&sig.decl,
10321032
body_id,
10331033
impl_item.span,
1034-
impl_item.hir_id,
1034+
impl_item.hir_id(),
10351035
);
10361036
}
10371037
ImplItemKind::TyAlias(ref ty) => {
1038-
visitor.visit_id(impl_item.hir_id);
1038+
visitor.visit_id(impl_item.hir_id());
10391039
visitor.visit_ty(ty);
10401040
}
10411041
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
5353
}
5454

5555
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
56-
type KeyType = (DefPathHash, ItemLocalId);
56+
type KeyType = DefPathHash;
5757

5858
#[inline]
59-
fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
60-
self.hir_id.to_stable_hash_key(hcx)
59+
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
60+
hcx.local_def_path_hash(self.def_id)
6161
}
6262
}
6363

@@ -103,7 +103,7 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItemId {
103103

104104
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItemId {
105105
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
106-
hcx.hash_reference_to_item(self.hir_id, hasher)
106+
hcx.hash_reference_to_item(self.hir_id(), hasher)
107107
}
108108
}
109109

@@ -154,7 +154,7 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItem<'_> {
154154
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItem<'_> {
155155
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
156156
let ImplItem {
157-
hir_id: _,
157+
def_id: _,
158158
ident,
159159
ref vis,
160160
defaultness,

Diff for: compiler/rustc_hir_pretty/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ impl<'a> State<'a> {
973973
}
974974

975975
pub fn print_impl_item(&mut self, ii: &hir::ImplItem<'_>) {
976-
self.ann.pre(self, AnnNode::SubItem(ii.hir_id));
976+
self.ann.pre(self, AnnNode::SubItem(ii.hir_id()));
977977
self.hardbreak_if_not_bol();
978978
self.maybe_print_comment(ii.span.lo());
979979
self.print_outer_attributes(&ii.attrs);
@@ -995,7 +995,7 @@ impl<'a> State<'a> {
995995
self.print_associated_type(ii.ident, &ii.generics, None, Some(ty));
996996
}
997997
}
998-
self.ann.post(self, AnnNode::SubItem(ii.hir_id))
998+
self.ann.post(self, AnnNode::SubItem(ii.hir_id()))
999999
}
10001000

10011001
pub fn print_local(&mut self, init: Option<&hir::Expr<'_>>, decl: impl Fn(&mut Self)) {

Diff for: compiler/rustc_incremental/src/assert_dep_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl Visitor<'tcx> for IfThisChanged<'tcx> {
177177
}
178178

179179
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
180-
self.process_attrs(impl_item.hir_id, &impl_item.attrs);
180+
self.process_attrs(impl_item.hir_id(), &impl_item.attrs);
181181
intravisit::walk_impl_item(self, impl_item);
182182
}
183183

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ impl ItemLikeVisitor<'tcx> for DirtyCleanVisitor<'tcx> {
458458
}
459459

460460
fn visit_impl_item(&mut self, item: &hir::ImplItem<'_>) {
461-
self.check_item(item.hir_id, item.span);
461+
self.check_item(item.hir_id(), item.span);
462462
}
463463

464464
fn visit_foreign_item(&mut self, item: &hir::ForeignItem<'_>) {

Diff for: compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ use crate::traits::{ObligationCauseCode, UnifyReceiverContext};
77
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorReported};
88
use rustc_hir::def_id::DefId;
99
use rustc_hir::intravisit::{walk_ty, ErasedMap, NestedVisitorMap, Visitor};
10-
use rustc_hir::{
11-
self as hir, GenericBound, ImplItem, Item, ItemKind, Lifetime, LifetimeName, Node, TyKind,
12-
};
10+
use rustc_hir::{self as hir, GenericBound, Item, ItemKind, Lifetime, LifetimeName, Node, TyKind};
1311
use rustc_middle::ty::{self, AssocItemContainer, RegionKind, Ty, TypeFoldable, TypeVisitor};
1412
use rustc_span::symbol::Ident;
1513
use rustc_span::{MultiSpan, Span};
@@ -342,12 +340,12 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
342340
) -> Option<(Ident, &'tcx hir::Ty<'tcx>)> {
343341
let tcx = self.tcx();
344342
match tcx.hir().get_if_local(def_id) {
345-
Some(Node::ImplItem(ImplItem { ident, hir_id, .. })) => {
346-
match tcx.hir().find(tcx.hir().get_parent_item(*hir_id)) {
343+
Some(Node::ImplItem(impl_item)) => {
344+
match tcx.hir().find(tcx.hir().get_parent_item(impl_item.hir_id())) {
347345
Some(Node::Item(Item {
348346
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
349347
..
350-
})) => Some((*ident, self_ty)),
348+
})) => Some((impl_item.ident, self_ty)),
351349
_ => None,
352350
}
353351
}

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
600600
if let Some(Node::Item(item)) = cx.tcx.hir().find(hir_id) {
601601
if let hir::VisibilityKind::Inherited = item.vis.node {
602602
for impl_item_ref in items {
603-
self.private_traits.insert(impl_item_ref.id.hir_id);
603+
self.private_traits.insert(impl_item_ref.id.hir_id());
604604
}
605605
}
606606
}
@@ -644,15 +644,14 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
644644

645645
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
646646
// If the method is an impl for a trait, don't doc.
647-
if method_context(cx, impl_item.hir_id) == MethodLateContext::TraitImpl {
647+
if method_context(cx, impl_item.hir_id()) == MethodLateContext::TraitImpl {
648648
return;
649649
}
650650

651-
let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
652-
let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());
651+
let (article, desc) = cx.tcx.article_and_description(impl_item.def_id.to_def_id());
653652
self.check_missing_docs_attrs(
654653
cx,
655-
Some(impl_item.hir_id),
654+
Some(impl_item.hir_id()),
656655
&impl_item.attrs,
657656
impl_item.span,
658657
article,
@@ -1378,7 +1377,7 @@ impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
13781377
}
13791378

13801379
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
1381-
self.perform_lint(cx, "item", impl_item.hir_id, &impl_item.vis, impl_item.span, false);
1380+
self.perform_lint(cx, "item", impl_item.hir_id(), &impl_item.vis, impl_item.span, false);
13821381
}
13831382
}
13841383

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
314314
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
315315
let generics = self.context.generics.take();
316316
self.context.generics = Some(&impl_item.generics);
317-
self.with_lint_attrs(impl_item.hir_id, &impl_item.attrs, |cx| {
318-
cx.with_param_env(impl_item.hir_id, |cx| {
317+
self.with_lint_attrs(impl_item.hir_id(), &impl_item.attrs, |cx| {
318+
cx.with_param_env(impl_item.hir_id(), |cx| {
319319
lint_callback!(cx, check_impl_item, impl_item);
320320
hir_visit::walk_impl_item(cx, impl_item);
321321
lint_callback!(cx, check_impl_item_post, impl_item);

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

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

639639
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
640-
self.with_lint_attrs(impl_item.hir_id, &impl_item.attrs, |builder| {
640+
self.with_lint_attrs(impl_item.hir_id(), &impl_item.attrs, |builder| {
641641
intravisit::walk_impl_item(builder, impl_item);
642642
});
643643
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ impl<'a> FnLikeNode<'a> {
235235
},
236236
Node::ImplItem(ii) => match ii.kind {
237237
hir::ImplItemKind::Fn(ref sig, body) => {
238-
method(ii.hir_id, ii.ident, sig, Some(&ii.vis), body, ii.span, &ii.attrs)
238+
method(ii.hir_id(), ii.ident, sig, Some(&ii.vis), body, ii.span, &ii.attrs)
239239
}
240240
_ => bug!("impl method FnLikeNode that is not fn-like"),
241241
},

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

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

403403
fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) {
404-
debug_assert_eq!(
405-
ii.hir_id.owner,
406-
self.definitions.opt_hir_id_to_local_def_id(ii.hir_id).unwrap()
407-
);
408-
self.with_dep_node_owner(ii.hir_id.owner, ii, |this, hash| {
409-
this.insert_with_hash(ii.span, ii.hir_id, Node::ImplItem(ii), hash);
404+
self.with_dep_node_owner(ii.def_id, ii, |this, hash| {
405+
this.insert_with_hash(ii.span, ii.hir_id(), Node::ImplItem(ii), hash);
410406

411-
this.with_parent(ii.hir_id, |this| {
407+
this.with_parent(ii.hir_id(), |this| {
412408
intravisit::walk_impl_item(this, ii);
413409
});
414410
});

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl<'hir> Map<'hir> {
315315
}
316316

317317
pub fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> {
318-
match self.find(id.hir_id).unwrap() {
318+
match self.find(id.hir_id()).unwrap() {
319319
Node::ImplItem(item) => item,
320320
_ => bug!(),
321321
}

Diff for: compiler/rustc_middle/src/ty/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ fn foo(&self) -> Self::T { String::new() }
836836
})) => {
837837
for item in &items[..] {
838838
if let hir::AssocItemKind::Type = item.kind {
839-
if self.type_of(self.hir().local_def_id(item.id.hir_id)) == found {
839+
if self.type_of(item.id.def_id) == found {
840840
db.span_label(item.span, "expected this associated type");
841841
return true;
842842
}

Diff for: compiler/rustc_mir/src/monomorphize/collector.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1060,8 +1060,7 @@ impl ItemLikeVisitor<'v> for RootCollector<'_, 'v> {
10601060

10611061
fn visit_impl_item(&mut self, ii: &'v hir::ImplItem<'v>) {
10621062
if let hir::ImplItemKind::Fn(hir::FnSig { .. }, _) = ii.kind {
1063-
let def_id = self.tcx.hir().local_def_id(ii.hir_id);
1064-
self.push_if_root(def_id);
1063+
self.push_if_root(ii.def_id);
10651064
}
10661065
}
10671066

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub(crate) fn target_from_impl_item<'tcx>(
2929
match impl_item.kind {
3030
hir::ImplItemKind::Const(..) => Target::AssocConst,
3131
hir::ImplItemKind::Fn(..) => {
32-
let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id);
32+
let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id());
3333
let containing_item = tcx.hir().expect_item(parent_hir_id);
3434
let containing_impl_is_for_trait = match &containing_item.kind {
3535
hir::ItemKind::Impl(impl_) => impl_.of_trait.is_some(),
@@ -1121,7 +1121,7 @@ impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
11211121

11221122
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
11231123
let target = target_from_impl_item(self.tcx, impl_item);
1124-
self.check_attributes(impl_item.hir_id, &impl_item.attrs, &impl_item.span, target, None);
1124+
self.check_attributes(impl_item.hir_id(), &impl_item.attrs, &impl_item.span, target, None);
11251125
intravisit::walk_impl_item(self, impl_item)
11261126
}
11271127

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,11 @@ impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> {
420420
if of_trait.is_some()
421421
|| has_allow_dead_code_or_lang_attr(
422422
self.tcx,
423-
impl_item.hir_id,
423+
impl_item.hir_id(),
424424
&impl_item.attrs,
425425
)
426426
{
427-
self.worklist.push(impl_item_ref.id.hir_id);
427+
self.worklist.push(impl_item_ref.id.hir_id());
428428
}
429429
}
430430
}
@@ -664,9 +664,9 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
664664
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
665665
match impl_item.kind {
666666
hir::ImplItemKind::Const(_, body_id) => {
667-
if !self.symbol_is_live(impl_item.hir_id) {
667+
if !self.symbol_is_live(impl_item.hir_id()) {
668668
self.warn_dead_code(
669-
impl_item.hir_id,
669+
impl_item.hir_id(),
670670
impl_item.span,
671671
impl_item.ident.name,
672672
"used",
@@ -675,7 +675,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
675675
self.visit_nested_body(body_id)
676676
}
677677
hir::ImplItemKind::Fn(_, body_id) => {
678-
if !self.symbol_is_live(impl_item.hir_id) {
678+
if !self.symbol_is_live(impl_item.hir_id()) {
679679
// FIXME(66095): Because impl_item.span is annotated with things
680680
// like expansion data, and ident.span isn't, we use the
681681
// def_span method if it's part of a macro invocation
@@ -687,7 +687,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
687687
} else {
688688
impl_item.ident.span
689689
};
690-
self.warn_dead_code(impl_item.hir_id, span, impl_item.ident.name, "used");
690+
self.warn_dead_code(impl_item.hir_id(), span, impl_item.ident.name, "used");
691691
}
692692
self.visit_nested_body(body_id)
693693
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<'v, 'tcx> ItemLikeVisitor<'v> for DiagnosticItemCollector<'tcx> {
3535
}
3636

3737
fn visit_impl_item(&mut self, impl_item: &hir::ImplItem<'_>) {
38-
self.observe_item(&impl_item.attrs, impl_item.hir_id);
38+
self.observe_item(&impl_item.attrs, impl_item.hir_id());
3939
}
4040

4141
fn visit_foreign_item(&mut self, foreign_item: &hir::ForeignItem<'_>) {

0 commit comments

Comments
 (0)