Skip to content

Commit c4e7427

Browse files
committed
Only store a LocalDefId in hir::MacroDef.
1 parent ff14cac commit c4e7427

File tree

16 files changed

+47
-29
lines changed

16 files changed

+47
-29
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
234234

235235
if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = i.kind {
236236
if !macro_rules || self.sess.contains_name(&i.attrs, sym::macro_export) {
237-
let hir_id = self.lower_node_id(i.id);
237+
let def_id = self.lower_node_id(i.id).expect_owner();
238238
let body = P(self.lower_mac_args(body));
239239
self.exported_macros.push(hir::MacroDef {
240240
ident,
241241
vis,
242242
attrs,
243-
hir_id,
243+
def_id,
244244
span: i.span,
245245
ast: MacroDef { body, macro_rules },
246246
});

compiler/rustc_hir/src/hir.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -761,16 +761,22 @@ impl Crate<'_> {
761761
/// A macro definition, in this crate or imported from another.
762762
///
763763
/// Not parsed directly, but created on macro import or `macro_rules!` expansion.
764-
#[derive(Debug, HashStable_Generic)]
764+
#[derive(Debug)]
765765
pub struct MacroDef<'hir> {
766766
pub ident: Ident,
767767
pub vis: Visibility<'hir>,
768768
pub attrs: &'hir [Attribute],
769-
pub hir_id: HirId,
769+
pub def_id: LocalDefId,
770770
pub span: Span,
771771
pub ast: ast::MacroDef,
772772
}
773773

774+
impl MacroDef<'_> {
775+
pub fn hir_id(&self) -> HirId {
776+
HirId::make_owner(self.def_id)
777+
}
778+
}
779+
774780
/// A block of statements `{ .. }`, which may have a label (in this case the
775781
/// `targeted_by_break` field will be `true`) and may be `unsafe` by means of
776782
/// the `rules` being anything but `DefaultBlock`.
@@ -2941,7 +2947,8 @@ impl<'hir> Node<'hir> {
29412947
Node::Item(Item { def_id, .. })
29422948
| Node::TraitItem(TraitItem { def_id, .. })
29432949
| Node::ImplItem(ImplItem { def_id, .. })
2944-
| Node::ForeignItem(ForeignItem { def_id, .. }) => Some(HirId::make_owner(*def_id)),
2950+
| Node::ForeignItem(ForeignItem { def_id, .. })
2951+
| Node::MacroDef(MacroDef { def_id, .. }) => Some(HirId::make_owner(*def_id)),
29452952
Node::Field(StructField { hir_id, .. })
29462953
| Node::AnonConst(AnonConst { hir_id, .. })
29472954
| Node::Expr(Expr { hir_id, .. })
@@ -2952,7 +2959,6 @@ impl<'hir> Node<'hir> {
29522959
| Node::Arm(Arm { hir_id, .. })
29532960
| Node::Block(Block { hir_id, .. })
29542961
| Node::Local(Local { hir_id, .. })
2955-
| Node::MacroDef(MacroDef { hir_id, .. })
29562962
| Node::Lifetime(Lifetime { hir_id, .. })
29572963
| Node::Param(Param { hir_id, .. })
29582964
| Node::GenericParam(GenericParam { hir_id, .. }) => Some(*hir_id),

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) {
489489
}
490490

491491
pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef<'v>) {
492-
visitor.visit_id(macro_def.hir_id);
492+
visitor.visit_id(macro_def.hir_id());
493493
visitor.visit_ident(macro_def.ident);
494494
walk_list!(visitor, visit_attribute, macro_def.attrs);
495495
}

compiler/rustc_hir/src/stable_hash_impls.rs

Lines changed: 16 additions & 2 deletions
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, ForeignItem, ForeignItemId, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem,
5-
TraitItemId, Ty, VisibilityKind,
4+
BodyId, Expr, ForeignItem, ForeignItemId, ImplItem, ImplItemId, Item, ItemId, MacroDef, Mod,
5+
TraitItem, TraitItemId, Ty, VisibilityKind,
66
};
77
use crate::hir_id::{HirId, ItemLocalId};
88
use rustc_span::def_id::{DefPathHash, LocalDefId};
@@ -203,3 +203,17 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
203203
});
204204
}
205205
}
206+
207+
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for MacroDef<'_> {
208+
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
209+
let MacroDef { ident, ref attrs, def_id: _, ref ast, ref vis, span } = *self;
210+
211+
hcx.hash_hir_item_like(|hcx| {
212+
ident.name.hash_stable(hcx, hasher);
213+
attrs.hash_stable(hcx, hasher);
214+
ast.hash_stable(hcx, hasher);
215+
vis.hash_stable(hcx, hasher);
216+
span.hash_stable(hcx, hasher);
217+
});
218+
}
219+
}

compiler/rustc_lint/src/levels.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> LintLevelMap {
4141
let push = builder.levels.push(&krate.item.attrs, &store, true);
4242
builder.levels.register_id(hir::CRATE_HIR_ID);
4343
for macro_def in krate.exported_macros {
44-
builder.levels.register_id(macro_def.hir_id);
44+
builder.levels.register_id(macro_def.hir_id());
4545
}
4646
intravisit::walk_crate(&mut builder, krate);
4747
builder.levels.pop(push);

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1494,7 +1494,7 @@ impl EncodeContext<'a, 'tcx> {
14941494

14951495
/// Serialize the text of exported macros
14961496
fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef<'_>) {
1497-
let def_id = self.tcx.hir().local_def_id(macro_def.hir_id).to_def_id();
1497+
let def_id = macro_def.def_id.to_def_id();
14981498
record!(self.tables.kind[def_id] <- EntryKind::MacroDef(self.lazy(macro_def.ast.clone())));
14991499
self.encode_ident_span(def_id, macro_def.ident);
15001500
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,15 +517,15 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
517517
// Exported macros are visited directly from the crate root,
518518
// so they do not have `parent_node` set.
519519
// Find the correct enclosing module from their DefKey.
520-
let def_key = self.definitions.def_key(macro_def.hir_id.owner);
520+
let def_key = self.definitions.def_key(macro_def.def_id);
521521
let parent = def_key.parent.map_or(hir::CRATE_HIR_ID, |local_def_index| {
522522
self.definitions.local_def_id_to_hir_id(LocalDefId { local_def_index })
523523
});
524524
self.with_parent(parent, |this| {
525-
this.with_dep_node_owner(macro_def.hir_id.owner, macro_def, |this, hash| {
525+
this.with_dep_node_owner(macro_def.def_id, macro_def, |this, hash| {
526526
this.insert_with_hash(
527527
macro_def.span,
528-
macro_def.hir_id,
528+
macro_def.hir_id(),
529529
Node::MacroDef(macro_def),
530530
hash,
531531
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ impl<'hir> Map<'hir> {
500500
V: Visitor<'hir>,
501501
{
502502
for id in self.krate().exported_macros {
503-
visitor.visit_macro_def(self.expect_macro_def(id.hir_id));
503+
visitor.visit_macro_def(self.expect_macro_def(id.hir_id()));
504504
}
505505
}
506506

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,7 @@ impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
11551155

11561156
fn visit_macro_def(&mut self, macro_def: &'tcx hir::MacroDef<'tcx>) {
11571157
self.check_attributes(
1158-
macro_def.hir_id,
1158+
macro_def.hir_id(),
11591159
macro_def.attrs,
11601160
&macro_def.span,
11611161
Target::MacroDef,

compiler/rustc_passes/src/diagnostic_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn collect<'tcx>(tcx: TyCtxt<'tcx>) -> FxHashMap<Symbol, DefId> {
106106
tcx.hir().krate().visit_all_item_likes(&mut collector);
107107

108108
for m in tcx.hir().krate().exported_macros {
109-
collector.observe_item(m.attrs, m.hir_id);
109+
collector.observe_item(m.attrs, m.hir_id());
110110
}
111111

112112
collector.items

0 commit comments

Comments
 (0)