Skip to content

Commit 0f5e7cd

Browse files
Treat macros as HIR items
1 parent d0a10b2 commit 0f5e7cd

File tree

26 files changed

+158
-180
lines changed

26 files changed

+158
-180
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
170170
self.lower_item_id_use_tree(use_tree, i.id, &mut vec);
171171
vec
172172
}
173-
ItemKind::MacroDef(..) => SmallVec::new(),
174173
ItemKind::Fn(..) | ItemKind::Impl(box ImplKind { of_trait: None, .. }) => {
175174
smallvec![i.id]
176175
}
@@ -212,28 +211,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
212211
pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item<'hir>> {
213212
let mut ident = i.ident;
214213
let mut vis = self.lower_visibility(&i.vis, None);
215-
216-
if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = i.kind {
217-
if !macro_rules || self.sess.contains_name(&i.attrs, sym::macro_export) {
218-
let hir_id = self.lower_node_id(i.id);
219-
self.lower_attrs(hir_id, &i.attrs);
220-
let body = P(self.lower_mac_args(body));
221-
self.insert_macro_def(hir::MacroDef {
222-
ident,
223-
vis,
224-
def_id: hir_id.expect_owner(),
225-
span: i.span,
226-
ast: MacroDef { body, macro_rules },
227-
});
228-
} else {
229-
for a in i.attrs.iter() {
230-
let a = self.lower_attr(a);
231-
self.non_exported_macro_attrs.push(a);
232-
}
233-
}
234-
return None;
235-
}
236-
237214
let hir_id = self.lower_node_id(i.id);
238215
let attrs = self.lower_attrs(hir_id, &i.attrs);
239216
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, &mut vis, &i.kind);
@@ -465,7 +442,26 @@ impl<'hir> LoweringContext<'_, 'hir> {
465442
self.lower_generics(generics, ImplTraitContext::disallowed()),
466443
self.lower_param_bounds(bounds, ImplTraitContext::disallowed()),
467444
),
468-
ItemKind::MacroDef(..) | ItemKind::MacCall(..) => {
445+
ItemKind::MacroDef(MacroDef { ref body, macro_rules }) => {
446+
let is_exported = !macro_rules
447+
|| attrs.map_or(false, |a| self.sess.contains_name(a, sym::macro_export));
448+
449+
let body = P(self.lower_mac_args(body));
450+
451+
hir::ItemKind::Macro {
452+
// `is_exported` is duplicated, to make matching more convenient.
453+
is_exported,
454+
macro_def: hir::MacroDef {
455+
is_exported,
456+
ident: *ident,
457+
vis: *vis,
458+
def_id: hir_id.expect_owner(),
459+
span,
460+
ast: MacroDef { body, macro_rules },
461+
},
462+
}
463+
}
464+
ItemKind::MacCall(..) => {
469465
panic!("`TyMac` should have been expanded by now")
470466
}
471467
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ struct LoweringContext<'a, 'hir: 'a> {
103103
/// The items being lowered are collected here.
104104
owners: IndexVec<LocalDefId, Option<hir::OwnerNode<'hir>>>,
105105
bodies: BTreeMap<hir::BodyId, hir::Body<'hir>>,
106-
non_exported_macro_attrs: Vec<ast::Attribute>,
107106

108107
trait_impls: BTreeMap<DefId, Vec<LocalDefId>>,
109108

@@ -330,7 +329,6 @@ pub fn lower_crate<'a, 'hir>(
330329
trait_impls: BTreeMap::new(),
331330
modules: BTreeMap::new(),
332331
attrs: BTreeMap::default(),
333-
non_exported_macro_attrs: Vec::new(),
334332
catch_scopes: Vec::new(),
335333
loop_scopes: Vec::new(),
336334
is_in_loop_condition: false,
@@ -551,7 +549,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
551549
}
552550

553551
let krate = hir::Crate {
554-
non_exported_macro_attrs: self.arena.alloc_from_iter(self.non_exported_macro_attrs),
555552
owners: self.owners,
556553
bodies: self.bodies,
557554
body_ids,
@@ -600,13 +597,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
600597
id
601598
}
602599

603-
fn insert_macro_def(&mut self, item: hir::MacroDef<'hir>) {
604-
let def_id = item.def_id;
605-
let item = self.arena.alloc(item);
606-
self.owners.ensure_contains_elem(def_id, || None);
607-
self.owners[def_id] = Some(hir::OwnerNode::MacroDef(item));
608-
}
609-
610600
fn allocate_hir_id_counter(&mut self, owner: NodeId) -> hir::HirId {
611601
// Set up the counter if needed.
612602
self.item_local_id_counters.entry(owner).or_insert(0);

compiler/rustc_hir/src/def.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ impl DefKind {
218218
| DefKind::Impl => None,
219219
}
220220
}
221+
222+
pub fn is_macro(self) -> bool {
223+
match self {
224+
DefKind::Macro(..) => true,
225+
_ => false,
226+
}
227+
}
221228
}
222229

223230
/// The resolution of a path or export.

compiler/rustc_hir/src/hir.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -670,9 +670,6 @@ pub struct ModuleItems {
670670
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
671671
#[derive(Debug)]
672672
pub struct Crate<'hir> {
673-
// Attributes from non-exported macros, kept only for collecting the library feature list.
674-
pub non_exported_macro_attrs: &'hir [Attribute],
675-
676673
pub owners: IndexVec<LocalDefId, Option<OwnerNode<'hir>>>,
677674
pub bodies: BTreeMap<BodyId, Body<'hir>>,
678675
pub trait_impls: BTreeMap<DefId, Vec<LocalDefId>>,
@@ -768,20 +765,14 @@ impl Crate<'_> {
768765
_ => None,
769766
})
770767
}
771-
772-
pub fn exported_macros<'hir>(&'hir self) -> impl Iterator<Item = &'hir MacroDef<'hir>> + 'hir {
773-
self.owners.iter().filter_map(|owner| match owner {
774-
Some(OwnerNode::MacroDef(macro_def)) => Some(*macro_def),
775-
_ => None,
776-
})
777-
}
778768
}
779769

780770
/// A macro definition, in this crate or imported from another.
781771
///
782772
/// Not parsed directly, but created on macro import or `macro_rules!` expansion.
783773
#[derive(Debug)]
784774
pub struct MacroDef<'hir> {
775+
pub is_exported: bool,
785776
pub ident: Ident,
786777
pub vis: Visibility<'hir>,
787778
pub def_id: LocalDefId,
@@ -2605,7 +2596,7 @@ pub struct PolyTraitRef<'hir> {
26052596

26062597
pub type Visibility<'hir> = Spanned<VisibilityKind<'hir>>;
26072598

2608-
#[derive(Debug)]
2599+
#[derive(Copy, Clone, Debug)]
26092600
pub enum VisibilityKind<'hir> {
26102601
Public,
26112602
Crate(CrateSugar),
@@ -2789,6 +2780,8 @@ pub enum ItemKind<'hir> {
27892780
Mod(Mod<'hir>),
27902781
/// An external module, e.g. `extern { .. }`.
27912782
ForeignMod { abi: Abi, items: &'hir [ForeignItemRef<'hir>] },
2783+
/// A MBE macro (`macro_rules!` or `macro`).
2784+
Macro { is_exported: bool, macro_def: MacroDef<'hir> },
27922785
/// Module-level inline assembly (from `global_asm!`).
27932786
GlobalAsm(&'hir InlineAsm<'hir>),
27942787
/// A type alias, e.g., `type Foo = Bar<u8>`.
@@ -2852,6 +2845,7 @@ impl ItemKind<'_> {
28522845
ItemKind::Fn(..) => "function",
28532846
ItemKind::Mod(..) => "module",
28542847
ItemKind::ForeignMod { .. } => "extern block",
2848+
ItemKind::Macro { .. } => "macro",
28552849
ItemKind::GlobalAsm(..) => "global asm item",
28562850
ItemKind::TyAlias(..) => "type alias",
28572851
ItemKind::OpaqueTy(..) => "opaque type",

compiler/rustc_hir/src/intravisit.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ pub trait Visitor<'v>: Sized {
484484
pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) {
485485
let top_mod = krate.module();
486486
visitor.visit_mod(top_mod, top_mod.inner, CRATE_HIR_ID);
487-
walk_list!(visitor, visit_macro_def, krate.exported_macros());
488487
for (&id, attrs) in krate.attrs.iter() {
489488
for a in *attrs {
490489
visitor.visit_attribute(id, a)
@@ -594,6 +593,10 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
594593
visitor.visit_id(item.hir_id());
595594
walk_list!(visitor, visit_foreign_item_ref, items);
596595
}
596+
ItemKind::Macro { ref macro_def, .. } => {
597+
visitor.visit_id(item.hir_id());
598+
visitor.visit_macro_def(macro_def)
599+
}
597600
ItemKind::GlobalAsm(asm) => {
598601
visitor.visit_id(item.hir_id());
599602
walk_inline_asm(visitor, asm);

compiler/rustc_hir/src/stable_hash_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
193193

194194
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for MacroDef<'_> {
195195
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
196-
let MacroDef { ident, def_id: _, ref ast, ref vis, span } = *self;
196+
let MacroDef { is_exported: _, ident, def_id: _, ref ast, ref vis, span } = *self;
197197

198198
hcx.hash_hir_item_like(|hcx| {
199199
ident.name.hash_stable(hcx, hasher);

compiler/rustc_hir/src/target.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ impl Target {
113113
ItemKind::Fn(..) => Target::Fn,
114114
ItemKind::Mod(..) => Target::Mod,
115115
ItemKind::ForeignMod { .. } => Target::ForeignMod,
116+
ItemKind::Macro { .. } => Target::MacroDef,
116117
ItemKind::GlobalAsm(..) => Target::GlobalAsm,
117118
ItemKind::TyAlias(..) => Target::TyAlias,
118119
ItemKind::OpaqueTy(..) => Target::OpaqueTy,

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_ast as ast;
44
use rustc_ast::util::parser::{self, AssocOp, Fixity};
55
use rustc_ast_pretty::pp::Breaks::{Consistent, Inconsistent};
66
use rustc_ast_pretty::pp::{self, Breaks};
7+
use rustc_ast_pretty::pprust::state::MacHeader;
78
use rustc_ast_pretty::pprust::{Comments, PrintState};
89
use rustc_hir as hir;
910
use rustc_hir::{GenericArg, GenericParam, GenericParamKind, Node};
@@ -660,6 +661,28 @@ impl<'a> State<'a> {
660661
}
661662
self.bclose(item.span);
662663
}
664+
hir::ItemKind::Macro { ref macro_def, .. } => {
665+
let (kw, has_bang) = if macro_def.ast.macro_rules {
666+
("macro_rules", true)
667+
} else {
668+
self.print_visibility(&item.vis);
669+
("macro", false)
670+
};
671+
672+
self.print_mac_common(
673+
Some(MacHeader::Keyword(kw)),
674+
has_bang,
675+
Some(item.ident),
676+
macro_def.ast.body.delim(),
677+
&macro_def.ast.body.inner_tokens(),
678+
true,
679+
item.span,
680+
);
681+
682+
if macro_def.ast.body.need_semicolon() {
683+
self.word(";");
684+
}
685+
}
663686
hir::ItemKind::GlobalAsm(ref asm) => {
664687
self.head(visibility_qualified(&item.vis, "global_asm!"));
665688
self.print_inline_asm(asm);

compiler/rustc_lint/src/builtin.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -586,24 +586,6 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
586586

587587
fn check_crate(&mut self, cx: &LateContext<'_>, krate: &hir::Crate<'_>) {
588588
self.check_missing_docs_attrs(cx, CRATE_DEF_ID, krate.module().inner, "the", "crate");
589-
590-
for macro_def in krate.exported_macros() {
591-
// Non exported macros should be skipped, since `missing_docs` only
592-
// applies to externally visible items.
593-
if !cx.access_levels.is_exported(macro_def.def_id) {
594-
continue;
595-
}
596-
597-
let attrs = cx.tcx.hir().attrs(macro_def.hir_id());
598-
let has_doc = attrs.iter().any(|a| has_doc(cx.sess(), a));
599-
if !has_doc {
600-
cx.struct_span_lint(
601-
MISSING_DOCS,
602-
cx.tcx.sess.source_map().guess_head_span(macro_def.span),
603-
|lint| lint.build("missing documentation for macro").emit(),
604-
);
605-
}
606-
}
607589
}
608590

609591
fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) {
@@ -635,7 +617,8 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
635617
return;
636618
}
637619

638-
hir::ItemKind::TyAlias(..)
620+
hir::ItemKind::Macro { is_exported: true, .. }
621+
| hir::ItemKind::TyAlias(..)
639622
| hir::ItemKind::Fn(..)
640623
| hir::ItemKind::Mod(..)
641624
| hir::ItemKind::Enum(..)

compiler/rustc_lint/src/late.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,6 @@ fn late_lint_pass_crate<'tcx, T: LateLintPass<'tcx>>(tcx: TyCtxt<'tcx>, pass: T)
453453
lint_callback!(cx, check_crate, krate);
454454

455455
hir_visit::walk_crate(cx, krate);
456-
for attr in krate.non_exported_macro_attrs {
457-
// This HIR ID is a lie, since the macro ID isn't available.
458-
cx.visit_attribute(hir::CRATE_HIR_ID, attr);
459-
}
460456

461457
lint_callback!(cx, check_crate_post, krate);
462458
})

0 commit comments

Comments
 (0)