Skip to content

Only store a LocalDefId in some HIR nodes #81611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Only store a LocalDefId in hir::TraitItem.
  • Loading branch information
cjgillot committed Feb 15, 2021
commit a871a0f11196ed028edeedc4843338f702880672
6 changes: 3 additions & 3 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
self.lctx.with_hir_id_owner(item.id, |lctx| match ctxt {
AssocCtxt::Trait => {
let hir_item = lctx.lower_trait_item(item);
let id = hir::TraitItemId { hir_id: hir_item.hir_id };
let id = hir_item.trait_item_id();
lctx.trait_items.insert(id, hir_item);
lctx.modules.get_mut(&lctx.current_module).unwrap().trait_items.insert(id);
}
Expand Down Expand Up @@ -846,7 +846,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
};

hir::TraitItem {
hir_id: self.lower_node_id(i.id),
def_id: trait_item_def_id,
ident: i.ident,
attrs: self.lower_attrs(&i.attrs),
generics,
Expand All @@ -866,7 +866,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
AssocItemKind::MacCall(..) => unimplemented!(),
};
let id = hir::TraitItemId { hir_id: self.lower_node_id(i.id) };
let id = hir::TraitItemId { def_id: self.lower_node_id(i.id).expect_owner() };
let defaultness = hir::Defaultness::Default { has_value: has_default };
hir::TraitItemRef { id, ident: i.ident, span: i.span, defaultness, kind }
}
Expand Down
29 changes: 24 additions & 5 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1907,7 +1907,14 @@ pub struct FnSig<'hir> {
// so it can fetched later.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)]
pub struct TraitItemId {
pub hir_id: HirId,
pub def_id: LocalDefId,
}

impl TraitItemId {
pub fn hir_id(&self) -> HirId {
// Items are always HIR owners.
HirId::make_owner(self.def_id)
}
}

/// Represents an item declaration within a trait declaration,
Expand All @@ -1917,13 +1924,24 @@ pub struct TraitItemId {
#[derive(Debug)]
pub struct TraitItem<'hir> {
pub ident: Ident,
pub hir_id: HirId,
pub def_id: LocalDefId,
pub attrs: &'hir [Attribute],
pub generics: Generics<'hir>,
pub kind: TraitItemKind<'hir>,
pub span: Span,
}

impl TraitItem<'_> {
pub fn hir_id(&self) -> HirId {
// Items are always HIR owners.
HirId::make_owner(self.def_id)
}

pub fn trait_item_id(&self) -> TraitItemId {
TraitItemId { def_id: self.def_id }
}
}

/// Represents a trait method's body (or just argument names).
#[derive(Encodable, Debug, HashStable_Generic)]
pub enum TraitFn<'hir> {
Expand Down Expand Up @@ -2885,9 +2903,10 @@ impl<'hir> Node<'hir> {

pub fn hir_id(&self) -> Option<HirId> {
match self {
Node::Item(Item { def_id, .. }) => Some(HirId::make_owner(*def_id)),
Node::Item(Item { def_id, .. }) | Node::TraitItem(TraitItem { def_id, .. }) => {
Some(HirId::make_owner(*def_id))
}
Node::ForeignItem(ForeignItem { hir_id, .. })
| Node::TraitItem(TraitItem { hir_id, .. })
| Node::ImplItem(ImplItem { hir_id, .. })
| Node::Field(StructField { hir_id, .. })
| Node::AnonConst(AnonConst { hir_id, .. })
Expand Down Expand Up @@ -2922,7 +2941,7 @@ mod size_asserts {
rustc_data_structures::static_assert_size!(super::Ty<'static>, 72);

rustc_data_structures::static_assert_size!(super::Item<'static>, 200);
rustc_data_structures::static_assert_size!(super::TraitItem<'static>, 152);
rustc_data_structures::static_assert_size!(super::TraitItem<'static>, 144);
rustc_data_structures::static_assert_size!(super::ImplItem<'static>, 168);
rustc_data_structures::static_assert_size!(super::ForeignItem<'static>, 160);
}
8 changes: 4 additions & 4 deletions compiler/rustc_hir/src/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,12 +964,12 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
visitor.visit_generics(&trait_item.generics);
match trait_item.kind {
TraitItemKind::Const(ref ty, default) => {
visitor.visit_id(trait_item.hir_id);
visitor.visit_id(trait_item.hir_id());
visitor.visit_ty(ty);
walk_list!(visitor, visit_nested_body, default);
}
TraitItemKind::Fn(ref sig, TraitFn::Required(param_names)) => {
visitor.visit_id(trait_item.hir_id);
visitor.visit_id(trait_item.hir_id());
visitor.visit_fn_decl(&sig.decl);
for &param_name in param_names {
visitor.visit_ident(param_name);
Expand All @@ -981,11 +981,11 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
&sig.decl,
body_id,
trait_item.span,
trait_item.hir_id,
trait_item.hir_id(),
);
}
TraitItemKind::Type(bounds, ref default) => {
visitor.visit_id(trait_item.hir_id);
visitor.visit_id(trait_item.hir_id());
walk_list!(visitor, visit_param_bound, bounds);
walk_list!(visitor, visit_ty, default);
}
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_hir/src/stable_hash_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
}

impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
type KeyType = (DefPathHash, ItemLocalId);
type KeyType = DefPathHash;

#[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
self.hir_id.to_stable_hash_key(hcx)
fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
hcx.local_def_path_hash(self.def_id)
}
}

Expand Down Expand Up @@ -109,7 +109,7 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItemId {

impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItemId {
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
hcx.hash_reference_to_item(self.hir_id, hasher)
hcx.hash_reference_to_item(self.hir_id(), hasher)
}
}

Expand Down Expand Up @@ -139,7 +139,7 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for VisibilityKind<'_>

impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItem<'_> {
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
let TraitItem { hir_id: _, ident, ref attrs, ref generics, ref kind, span } = *self;
let TraitItem { def_id: _, ident, ref attrs, ref generics, ref kind, span } = *self;

hcx.hash_hir_item_like(|hcx| {
ident.name.hash_stable(hcx, hasher);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ impl<'a> State<'a> {
}

pub fn print_trait_item(&mut self, ti: &hir::TraitItem<'_>) {
self.ann.pre(self, AnnNode::SubItem(ti.hir_id));
self.ann.pre(self, AnnNode::SubItem(ti.hir_id()));
self.hardbreak_if_not_bol();
self.maybe_print_comment(ti.span.lo());
self.print_outer_attributes(&ti.attrs);
Expand Down Expand Up @@ -969,7 +969,7 @@ impl<'a> State<'a> {
);
}
}
self.ann.post(self, AnnNode::SubItem(ti.hir_id))
self.ann.post(self, AnnNode::SubItem(ti.hir_id()))
}

pub fn print_impl_item(&mut self, ii: &hir::ImplItem<'_>) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_incremental/src/assert_dep_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl Visitor<'tcx> for IfThisChanged<'tcx> {
}

fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
self.process_attrs(trait_item.hir_id, &trait_item.attrs);
self.process_attrs(trait_item.hir_id(), &trait_item.attrs);
intravisit::walk_trait_item(self, trait_item);
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_incremental/src/persist/dirty_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ impl ItemLikeVisitor<'tcx> for DirtyCleanVisitor<'tcx> {
}

fn visit_trait_item(&mut self, item: &hir::TraitItem<'_>) {
self.check_item(item.hir_id, item.span);
self.check_item(item.hir_id(), item.span);
}

fn visit_impl_item(&mut self, item: &hir::ImplItem<'_>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorRepor
use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::{walk_ty, ErasedMap, NestedVisitorMap, Visitor};
use rustc_hir::{
self as hir, GenericBound, ImplItem, Item, ItemKind, Lifetime, LifetimeName, Node, TraitItem,
TyKind,
self as hir, GenericBound, ImplItem, Item, ItemKind, Lifetime, LifetimeName, Node, TyKind,
};
use rustc_middle::ty::{self, AssocItemContainer, RegionKind, Ty, TypeFoldable, TypeVisitor};
use rustc_span::symbol::Ident;
Expand Down Expand Up @@ -352,8 +351,8 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
_ => None,
}
}
Some(Node::TraitItem(TraitItem { ident, hir_id, .. })) => {
let parent_id = tcx.hir().get_parent_item(*hir_id);
Some(Node::TraitItem(trait_item)) => {
let parent_id = tcx.hir().get_parent_item(trait_item.hir_id());
match tcx.hir().find(parent_id) {
Some(Node::Item(Item { kind: ItemKind::Trait(..), .. })) => {
// The method being called is defined in the `trait`, but the `'static`
Expand Down Expand Up @@ -389,7 +388,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
})
.next()
{
Some(self_ty) => Some((*ident, self_ty)),
Some(self_ty) => Some((trait_item.ident, self_ty)),
_ => None,
}
}
Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
if let hir::VisibilityKind::Inherited = it.vis.node {
self.private_traits.insert(it.hir_id());
for trait_item_ref in trait_item_refs {
self.private_traits.insert(trait_item_ref.id.hir_id);
self.private_traits.insert(trait_item_ref.id.hir_id());
}
return;
}
Expand Down Expand Up @@ -626,16 +626,15 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
}

fn check_trait_item(&mut self, cx: &LateContext<'_>, trait_item: &hir::TraitItem<'_>) {
if self.private_traits.contains(&trait_item.hir_id) {
if self.private_traits.contains(&trait_item.hir_id()) {
return;
}

let def_id = cx.tcx.hir().local_def_id(trait_item.hir_id);
let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());
let (article, desc) = cx.tcx.article_and_description(trait_item.def_id.to_def_id());

self.check_missing_docs_attrs(
cx,
Some(trait_item.hir_id),
Some(trait_item.hir_id()),
&trait_item.attrs,
trait_item.span,
article,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
let generics = self.context.generics.take();
self.context.generics = Some(&trait_item.generics);
self.with_lint_attrs(trait_item.hir_id, &trait_item.attrs, |cx| {
cx.with_param_env(trait_item.hir_id, |cx| {
self.with_lint_attrs(trait_item.hir_id(), &trait_item.attrs, |cx| {
cx.with_param_env(trait_item.hir_id(), |cx| {
lint_callback!(cx, check_trait_item, trait_item);
hir_visit::walk_trait_item(cx, trait_item);
lint_callback!(cx, check_trait_item_post, trait_item);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ impl<'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'_, 'tcx> {
}

fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
self.with_lint_attrs(trait_item.hir_id, &trait_item.attrs, |builder| {
self.with_lint_attrs(trait_item.hir_id(), &trait_item.attrs, |builder| {
intravisit::walk_trait_item(builder, trait_item);
});
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/hir/map/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl<'a> FnLikeNode<'a> {
},
Node::TraitItem(ti) => match ti.kind {
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
method(ti.hir_id, ti.ident, sig, None, body, ti.span, &ti.attrs)
method(ti.hir_id(), ti.ident, sig, None, body, ti.span, &ti.attrs)
}
_ => bug!("trait method FnLikeNode that is not fn-like"),
},
Expand Down
10 changes: 3 additions & 7 deletions compiler/rustc_middle/src/hir/map/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,10 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
}

fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {
debug_assert_eq!(
ti.hir_id.owner,
self.definitions.opt_hir_id_to_local_def_id(ti.hir_id).unwrap()
);
self.with_dep_node_owner(ti.hir_id.owner, ti, |this, hash| {
this.insert_with_hash(ti.span, ti.hir_id, Node::TraitItem(ti), hash);
self.with_dep_node_owner(ti.def_id, ti, |this, hash| {
this.insert_with_hash(ti.span, ti.hir_id(), Node::TraitItem(ti), hash);

this.with_parent(ti.hir_id, |this| {
this.with_parent(ti.hir_id(), |this| {
intravisit::walk_trait_item(this, ti);
});
});
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ impl<'hir> Map<'hir> {
}

pub fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir> {
match self.find(id.hir_id).unwrap() {
match self.find(id.hir_id()).unwrap() {
Node::TraitItem(item) => item,
_ => bug!(),
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ fn foo(&self) -> Self::T { String::new() }
// an assoc type as a return type (#72076).
if let hir::Defaultness::Default { has_value: true } = item.defaultness
{
if self.type_of(self.hir().local_def_id(item.id.hir_id)) == found {
if self.type_of(item.id.def_id) == found {
db.span_label(
item.span,
"associated type defaults can't be assumed inside the \
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,13 @@ impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {

fn visit_trait_item(&mut self, trait_item: &'tcx TraitItem<'tcx>) {
let target = Target::from_trait_item(trait_item);
self.check_attributes(trait_item.hir_id, &trait_item.attrs, &trait_item.span, target, None);
self.check_attributes(
trait_item.hir_id(),
&trait_item.attrs,
&trait_item.span,
target,
None,
);
intravisit::walk_trait_item(self, trait_item)
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,9 @@ impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> {
fn visit_trait_item(&mut self, trait_item: &hir::TraitItem<'_>) {
use hir::TraitItemKind::{Const, Fn};
if matches!(trait_item.kind, Const(_, Some(_)) | Fn(_, hir::TraitFn::Provided(_)))
&& has_allow_dead_code_or_lang_attr(self.tcx, trait_item.hir_id, &trait_item.attrs)
&& has_allow_dead_code_or_lang_attr(self.tcx, trait_item.hir_id(), &trait_item.attrs)
{
self.worklist.push(trait_item.hir_id);
self.worklist.push(trait_item.hir_id());
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/diagnostic_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<'v, 'tcx> ItemLikeVisitor<'v> for DiagnosticItemCollector<'tcx> {
}

fn visit_trait_item(&mut self, trait_item: &hir::TraitItem<'_>) {
self.observe_item(&trait_item.attrs, trait_item.hir_id);
self.observe_item(&trait_item.attrs, trait_item.hir_id());
}

fn visit_impl_item(&mut self, impl_item: &hir::ImplItem<'_>) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/hir_id_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'a, 'hir> ItemLikeVisitor<'hir> for OuterVisitor<'a, 'hir> {

fn visit_trait_item(&mut self, i: &'hir hir::TraitItem<'hir>) {
let mut inner_visitor = self.new_inner_visitor(self.hir_map);
inner_visitor.check(i.hir_id, |this| intravisit::walk_trait_item(this, i));
inner_visitor.check(i.hir_id(), |this| intravisit::walk_trait_item(this, i));
}

fn visit_impl_item(&mut self, i: &'hir hir::ImplItem<'hir>) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/hir_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
}

fn visit_trait_item(&mut self, ti: &'v hir::TraitItem<'v>) {
self.record("TraitItem", Id::Node(ti.hir_id), ti);
self.record("TraitItem", Id::Node(ti.hir_id()), ti);
hir_visit::walk_trait_item(self, ti)
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl ItemLikeVisitor<'v> for LanguageItemCollector<'tcx> {
fn visit_trait_item(&mut self, trait_item: &hir::TraitItem<'_>) {
self.check_for_lang(
Target::from_trait_item(trait_item),
trait_item.hir_id,
trait_item.hir_id(),
trait_item.attrs,
)
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_passes/src/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {

fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem<'tcx>) {
self.annotate(
ti.hir_id,
ti.hir_id(),
&ti.attrs,
ti.span,
AnnotationKind::Required,
Expand Down Expand Up @@ -571,7 +571,7 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
}

fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem<'tcx>) {
self.check_missing_stability(ti.hir_id, ti.span);
self.check_missing_stability(ti.hir_id(), ti.span);
intravisit::walk_trait_item(self, ti);
}

Expand Down
Loading