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

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

+2-2
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
});

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

+10-4
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),

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

+1-1
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
}

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

+16-2
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+
}

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

+1-1
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);

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

+1-1
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
}

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

+3-3
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
);

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

+1-1
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

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

+1-1
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,

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

+1-1
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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
246246
}
247247

248248
fn visit_macro_def(&mut self, macro_def: &'v hir::MacroDef<'v>) {
249-
self.record("MacroDef", Id::Node(macro_def.hir_id), macro_def);
249+
self.record("MacroDef", Id::Node(macro_def.hir_id()), macro_def);
250250
hir_visit::walk_macro_def(self, macro_def)
251251
}
252252
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
473473

474474
fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) {
475475
self.annotate(
476-
md.hir_id,
476+
md.hir_id(),
477477
&md.attrs,
478478
md.span,
479479
AnnotationKind::Required,
@@ -599,7 +599,7 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
599599
}
600600

601601
fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) {
602-
self.check_missing_stability(md.hir_id, md.span);
602+
self.check_missing_stability(md.hir_id(), md.span);
603603
}
604604

605605
// Note that we don't need to `check_missing_stability` for default generic parameters,

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -867,14 +867,12 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
867867
// `#[macro_export]`-ed `macro_rules!` are `Public` since they
868868
// ignore their containing path to always appear at the crate root.
869869
if md.ast.macro_rules {
870-
self.update(md.hir_id, Some(AccessLevel::Public));
870+
self.update(md.hir_id(), Some(AccessLevel::Public));
871871
}
872872
return;
873873
}
874874

875-
let macro_module_def_id =
876-
ty::DefIdTree::parent(self.tcx, self.tcx.hir().local_def_id(md.hir_id).to_def_id())
877-
.unwrap();
875+
let macro_module_def_id = ty::DefIdTree::parent(self.tcx, md.def_id.to_def_id()).unwrap();
878876
let hir_id = macro_module_def_id
879877
.as_local()
880878
.map(|def_id| self.tcx.hir().local_def_id_to_hir_id(def_id));
@@ -884,7 +882,7 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
884882
_ => return,
885883
};
886884
let level = if md.vis.node.is_pub() { self.get(module_id) } else { None };
887-
let new_level = self.update(md.hir_id, level);
885+
let new_level = self.update(md.hir_id(), level);
888886
if new_level.is_none() {
889887
return;
890888
}

Diff for: src/librustdoc/clean/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2291,7 +2291,7 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
22912291
)
22922292
} else {
22932293
let vis = item.vis.clean(cx);
2294-
let def_id = cx.tcx.hir().local_def_id(item.hir_id).to_def_id();
2294+
let def_id = item.def_id.to_def_id();
22952295

22962296
if matchers.len() <= 1 {
22972297
format!(
@@ -2314,7 +2314,7 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
23142314
};
23152315

23162316
Item::from_hir_id_and_parts(
2317-
item.hir_id,
2317+
item.hir_id(),
23182318
Some(name),
23192319
MacroItem(Macro { source, imported_from: None }),
23202320
cx,

Diff for: src/librustdoc/doctest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ impl<'a, 'hir, 'tcx> intravisit::Visitor<'hir> for HirCollector<'a, 'hir, 'tcx>
11121112
self.visit_testable(
11131113
macro_def.ident.to_string(),
11141114
&macro_def.attrs,
1115-
macro_def.hir_id,
1115+
macro_def.hir_id(),
11161116
macro_def.span,
11171117
|_| (),
11181118
);

Diff for: src/librustdoc/visit_ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
8989
// (since a direct parent isn't necessarily a module, c.f. #77828).
9090
let macro_parent_def_id = {
9191
use rustc_middle::ty::DefIdTree;
92-
tcx.parent(tcx.hir().local_def_id(def.hir_id).to_def_id()).unwrap()
92+
tcx.parent(def.def_id.to_def_id()).unwrap()
9393
};
9494
let macro_parent_path = tcx.def_path(macro_parent_def_id);
9595
// HACK: rustdoc has no way to lookup `doctree::Module`s by their HirId. Instead,

0 commit comments

Comments
 (0)