Skip to content

Commit de0530d

Browse files
committed
Document non-exported macros with --document-private-items
1 parent 1c389ff commit de0530d

File tree

7 files changed

+39
-2
lines changed

7 files changed

+39
-2
lines changed

compiler/rustc_ast_lowering/src/item.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
216216
let attrs = self.lower_attrs(&i.attrs);
217217

218218
if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = i.kind {
219+
let hir_id = self.lower_node_id(i.id);
220+
let body = P(self.lower_mac_args(body));
219221
if !macro_rules || self.sess.contains_name(&i.attrs, sym::macro_export) {
220-
let hir_id = self.lower_node_id(i.id);
221-
let body = P(self.lower_mac_args(body));
222222
self.exported_macros.push(hir::MacroDef {
223223
ident,
224224
vis,
@@ -228,6 +228,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
228228
ast: MacroDef { body, macro_rules },
229229
});
230230
} else {
231+
self.non_exported_macros.push(hir::MacroDef {
232+
ident,
233+
vis,
234+
attrs,
235+
hir_id,
236+
span: i.span,
237+
ast: MacroDef { body, macro_rules },
238+
});
231239
self.non_exported_macro_attrs.extend(attrs.iter().cloned());
232240
}
233241
return None;

compiler/rustc_ast_lowering/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ struct LoweringContext<'a, 'hir: 'a> {
103103
impl_items: BTreeMap<hir::ImplItemId, hir::ImplItem<'hir>>,
104104
bodies: BTreeMap<hir::BodyId, hir::Body<'hir>>,
105105
exported_macros: Vec<hir::MacroDef<'hir>>,
106+
non_exported_macros: Vec<hir::MacroDef<'hir>>,
106107
non_exported_macro_attrs: Vec<ast::Attribute>,
107108

108109
trait_impls: BTreeMap<DefId, Vec<hir::HirId>>,
@@ -302,6 +303,7 @@ pub fn lower_crate<'a, 'hir>(
302303
trait_impls: BTreeMap::new(),
303304
modules: BTreeMap::new(),
304305
exported_macros: Vec::new(),
306+
non_exported_macros: Vec::new(),
305307
non_exported_macro_attrs: Vec::new(),
306308
catch_scopes: Vec::new(),
307309
loop_scopes: Vec::new(),
@@ -582,6 +584,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
582584
hir::Crate {
583585
item: hir::CrateItem { module, attrs, span: c.span },
584586
exported_macros: self.arena.alloc_from_iter(self.exported_macros),
587+
non_exported_macros: self.arena.alloc_from_iter(self.non_exported_macros),
585588
non_exported_macro_attrs: self.arena.alloc_from_iter(self.non_exported_macro_attrs),
586589
items: self.items,
587590
trait_items: self.trait_items,

compiler/rustc_hir/src/hir.rs

+1
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ pub struct CrateItem<'hir> {
599599
pub struct Crate<'hir> {
600600
pub item: CrateItem<'hir>,
601601
pub exported_macros: &'hir [MacroDef<'hir>],
602+
pub non_exported_macros: &'hir [MacroDef<'hir>],
602603
// Attributes from non-exported macros, kept only for collecting the library feature list.
603604
pub non_exported_macro_attrs: &'hir [Attribute],
604605

compiler/rustc_hir/src/intravisit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) {
463463
visitor.visit_mod(&krate.item.module, krate.item.span, CRATE_HIR_ID);
464464
walk_list!(visitor, visit_attribute, krate.item.attrs);
465465
walk_list!(visitor, visit_macro_def, krate.exported_macros);
466+
walk_list!(visitor, visit_macro_def, krate.non_exported_macros);
466467
}
467468

468469
pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef<'v>) {

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

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
108108
ref item,
109109
// These fields are handled separately:
110110
exported_macros: _,
111+
non_exported_macros: _,
111112
non_exported_macro_attrs: _,
112113
items: _,
113114
trait_items: _,

src/librustdoc/visit_ast.rs

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
7474
module
7575
.macros
7676
.extend(krate.exported_macros.iter().map(|def| self.visit_local_macro(def, None)));
77+
if self.cx.render_options.document_private {
78+
// If `--document-private-items` is passed, also attach the crate's
79+
// *non*-exported macros to the top-level module.
80+
module
81+
.macros
82+
.extend(krate.non_exported_macros.iter().map(|def| self.visit_local_macro(def, None)));
83+
}
7784
module.is_crate = true;
7885

7986
self.cx.renderinfo.get_mut().exact_paths = self.exact_paths;
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// compile-flags: --document-private-items
2+
3+
fn foo_fn() {}
4+
5+
pub fn pub_foo_fn() {}
6+
7+
macro_rules! foo_macro {
8+
() => { };
9+
}
10+
11+
#[macro_export]
12+
macro_rules! exported_foo_macro {
13+
() => { };
14+
}
15+
16+
// TODO: add `@has` checks

0 commit comments

Comments
 (0)