Only store a LocalDefId in hir::MacroDef.

This commit is contained in:
Camille GILLOT 2021-01-31 18:20:18 +01:00
parent ff14cac621
commit c4e7427081
16 changed files with 47 additions and 29 deletions

View File

@ -234,13 +234,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = i.kind {
if !macro_rules || self.sess.contains_name(&i.attrs, sym::macro_export) {
let hir_id = self.lower_node_id(i.id);
let def_id = self.lower_node_id(i.id).expect_owner();
let body = P(self.lower_mac_args(body));
self.exported_macros.push(hir::MacroDef {
ident,
vis,
attrs,
hir_id,
def_id,
span: i.span,
ast: MacroDef { body, macro_rules },
});

View File

@ -761,16 +761,22 @@ impl Crate<'_> {
/// A macro definition, in this crate or imported from another.
///
/// Not parsed directly, but created on macro import or `macro_rules!` expansion.
#[derive(Debug, HashStable_Generic)]
#[derive(Debug)]
pub struct MacroDef<'hir> {
pub ident: Ident,
pub vis: Visibility<'hir>,
pub attrs: &'hir [Attribute],
pub hir_id: HirId,
pub def_id: LocalDefId,
pub span: Span,
pub ast: ast::MacroDef,
}
impl MacroDef<'_> {
pub fn hir_id(&self) -> HirId {
HirId::make_owner(self.def_id)
}
}
/// A block of statements `{ .. }`, which may have a label (in this case the
/// `targeted_by_break` field will be `true`) and may be `unsafe` by means of
/// the `rules` being anything but `DefaultBlock`.
@ -2941,7 +2947,8 @@ impl<'hir> Node<'hir> {
Node::Item(Item { def_id, .. })
| Node::TraitItem(TraitItem { def_id, .. })
| Node::ImplItem(ImplItem { def_id, .. })
| Node::ForeignItem(ForeignItem { def_id, .. }) => Some(HirId::make_owner(*def_id)),
| Node::ForeignItem(ForeignItem { def_id, .. })
| Node::MacroDef(MacroDef { def_id, .. }) => Some(HirId::make_owner(*def_id)),
Node::Field(StructField { hir_id, .. })
| Node::AnonConst(AnonConst { hir_id, .. })
| Node::Expr(Expr { hir_id, .. })
@ -2952,7 +2959,6 @@ impl<'hir> Node<'hir> {
| Node::Arm(Arm { hir_id, .. })
| Node::Block(Block { hir_id, .. })
| Node::Local(Local { hir_id, .. })
| Node::MacroDef(MacroDef { hir_id, .. })
| Node::Lifetime(Lifetime { hir_id, .. })
| Node::Param(Param { hir_id, .. })
| Node::GenericParam(GenericParam { hir_id, .. }) => Some(*hir_id),

View File

@ -489,7 +489,7 @@ pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) {
}
pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef<'v>) {
visitor.visit_id(macro_def.hir_id);
visitor.visit_id(macro_def.hir_id());
visitor.visit_ident(macro_def.ident);
walk_list!(visitor, visit_attribute, macro_def.attrs);
}

View File

@ -1,8 +1,8 @@
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
use crate::hir::{
BodyId, Expr, ForeignItem, ForeignItemId, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem,
TraitItemId, Ty, VisibilityKind,
BodyId, Expr, ForeignItem, ForeignItemId, ImplItem, ImplItemId, Item, ItemId, MacroDef, Mod,
TraitItem, TraitItemId, Ty, VisibilityKind,
};
use crate::hir_id::{HirId, ItemLocalId};
use rustc_span::def_id::{DefPathHash, LocalDefId};
@ -203,3 +203,17 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
});
}
}
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for MacroDef<'_> {
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
let MacroDef { ident, ref attrs, def_id: _, ref ast, ref vis, span } = *self;
hcx.hash_hir_item_like(|hcx| {
ident.name.hash_stable(hcx, hasher);
attrs.hash_stable(hcx, hasher);
ast.hash_stable(hcx, hasher);
vis.hash_stable(hcx, hasher);
span.hash_stable(hcx, hasher);
});
}
}

View File

@ -41,7 +41,7 @@ fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> LintLevelMap {
let push = builder.levels.push(&krate.item.attrs, &store, true);
builder.levels.register_id(hir::CRATE_HIR_ID);
for macro_def in krate.exported_macros {
builder.levels.register_id(macro_def.hir_id);
builder.levels.register_id(macro_def.hir_id());
}
intravisit::walk_crate(&mut builder, krate);
builder.levels.pop(push);

View File

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

View File

@ -517,15 +517,15 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
// Exported macros are visited directly from the crate root,
// so they do not have `parent_node` set.
// Find the correct enclosing module from their DefKey.
let def_key = self.definitions.def_key(macro_def.hir_id.owner);
let def_key = self.definitions.def_key(macro_def.def_id);
let parent = def_key.parent.map_or(hir::CRATE_HIR_ID, |local_def_index| {
self.definitions.local_def_id_to_hir_id(LocalDefId { local_def_index })
});
self.with_parent(parent, |this| {
this.with_dep_node_owner(macro_def.hir_id.owner, macro_def, |this, hash| {
this.with_dep_node_owner(macro_def.def_id, macro_def, |this, hash| {
this.insert_with_hash(
macro_def.span,
macro_def.hir_id,
macro_def.hir_id(),
Node::MacroDef(macro_def),
hash,
);

View File

@ -500,7 +500,7 @@ impl<'hir> Map<'hir> {
V: Visitor<'hir>,
{
for id in self.krate().exported_macros {
visitor.visit_macro_def(self.expect_macro_def(id.hir_id));
visitor.visit_macro_def(self.expect_macro_def(id.hir_id()));
}
}

View File

@ -1155,7 +1155,7 @@ impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
fn visit_macro_def(&mut self, macro_def: &'tcx hir::MacroDef<'tcx>) {
self.check_attributes(
macro_def.hir_id,
macro_def.hir_id(),
macro_def.attrs,
&macro_def.span,
Target::MacroDef,

View File

@ -106,7 +106,7 @@ fn collect<'tcx>(tcx: TyCtxt<'tcx>) -> FxHashMap<Symbol, DefId> {
tcx.hir().krate().visit_all_item_likes(&mut collector);
for m in tcx.hir().krate().exported_macros {
collector.observe_item(m.attrs, m.hir_id);
collector.observe_item(m.attrs, m.hir_id());
}
collector.items

View File

@ -246,7 +246,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
}
fn visit_macro_def(&mut self, macro_def: &'v hir::MacroDef<'v>) {
self.record("MacroDef", Id::Node(macro_def.hir_id), macro_def);
self.record("MacroDef", Id::Node(macro_def.hir_id()), macro_def);
hir_visit::walk_macro_def(self, macro_def)
}
}

View File

@ -473,7 +473,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) {
self.annotate(
md.hir_id,
md.hir_id(),
&md.attrs,
md.span,
AnnotationKind::Required,
@ -599,7 +599,7 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
}
fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) {
self.check_missing_stability(md.hir_id, md.span);
self.check_missing_stability(md.hir_id(), md.span);
}
// Note that we don't need to `check_missing_stability` for default generic parameters,

View File

@ -867,14 +867,12 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
// `#[macro_export]`-ed `macro_rules!` are `Public` since they
// ignore their containing path to always appear at the crate root.
if md.ast.macro_rules {
self.update(md.hir_id, Some(AccessLevel::Public));
self.update(md.hir_id(), Some(AccessLevel::Public));
}
return;
}
let macro_module_def_id =
ty::DefIdTree::parent(self.tcx, self.tcx.hir().local_def_id(md.hir_id).to_def_id())
.unwrap();
let macro_module_def_id = ty::DefIdTree::parent(self.tcx, md.def_id.to_def_id()).unwrap();
let hir_id = macro_module_def_id
.as_local()
.map(|def_id| self.tcx.hir().local_def_id_to_hir_id(def_id));
@ -884,7 +882,7 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
_ => return,
};
let level = if md.vis.node.is_pub() { self.get(module_id) } else { None };
let new_level = self.update(md.hir_id, level);
let new_level = self.update(md.hir_id(), level);
if new_level.is_none() {
return;
}

View File

@ -2291,7 +2291,7 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
)
} else {
let vis = item.vis.clean(cx);
let def_id = cx.tcx.hir().local_def_id(item.hir_id).to_def_id();
let def_id = item.def_id.to_def_id();
if matchers.len() <= 1 {
format!(
@ -2314,7 +2314,7 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
};
Item::from_hir_id_and_parts(
item.hir_id,
item.hir_id(),
Some(name),
MacroItem(Macro { source, imported_from: None }),
cx,

View File

@ -1112,7 +1112,7 @@ impl<'a, 'hir, 'tcx> intravisit::Visitor<'hir> for HirCollector<'a, 'hir, 'tcx>
self.visit_testable(
macro_def.ident.to_string(),
&macro_def.attrs,
macro_def.hir_id,
macro_def.hir_id(),
macro_def.span,
|_| (),
);

View File

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