Skip to content

Commit 3cbb56f

Browse files
committed
Auto merge of rust-lang#79455 - CraftSpider:master, r=jyn514
Remove doctree::Macro and distinguish between `macro_rules!` and `pub macro` This is a part of rust-lang#78082, removing doctree::Macro. Uses the changes in rust-lang#79372 Fixes rust-lang#76761
2 parents 6add378 + d23b57c commit 3cbb56f

File tree

5 files changed

+97
-43
lines changed

5 files changed

+97
-43
lines changed

src/librustdoc/clean/mod.rs

+41-14
Original file line numberDiff line numberDiff line change
@@ -2327,22 +2327,49 @@ impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Ident>) {
23272327
}
23282328
}
23292329

2330-
impl Clean<Item> for doctree::Macro {
2330+
impl Clean<Item> for (&hir::MacroDef<'_>, Option<Ident>) {
23312331
fn clean(&self, cx: &DocContext<'_>) -> Item {
2332-
Item::from_def_id_and_parts(
2333-
self.def_id,
2334-
Some(self.name.clean(cx)),
2335-
MacroItem(Macro {
2336-
source: format!(
2337-
"macro_rules! {} {{\n{}}}",
2338-
self.name,
2339-
self.matchers
2332+
let (item, renamed) = self;
2333+
let name = renamed.unwrap_or(item.ident).name;
2334+
let tts = item.ast.body.inner_tokens().trees().collect::<Vec<_>>();
2335+
// Extract the spans of all matchers. They represent the "interface" of the macro.
2336+
let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect::<Vec<_>>();
2337+
let source = if item.ast.macro_rules {
2338+
format!(
2339+
"macro_rules! {} {{\n{}}}",
2340+
name,
2341+
matchers
2342+
.iter()
2343+
.map(|span| { format!(" {} => {{ ... }};\n", span.to_src(cx)) })
2344+
.collect::<String>(),
2345+
)
2346+
} else {
2347+
let vis = item.vis.clean(cx);
2348+
2349+
if matchers.len() <= 1 {
2350+
format!(
2351+
"{}macro {}{} {{\n ...\n}}",
2352+
vis.print_with_space(),
2353+
name,
2354+
matchers.iter().map(|span| span.to_src(cx)).collect::<String>(),
2355+
)
2356+
} else {
2357+
format!(
2358+
"{}macro {} {{\n{}}}",
2359+
vis.print_with_space(),
2360+
name,
2361+
matchers
23402362
.iter()
2341-
.map(|span| { format!(" {} => {{ ... }};\n", span.to_src(cx)) })
2342-
.collect::<String>()
2343-
),
2344-
imported_from: self.imported_from.clean(cx),
2345-
}),
2363+
.map(|span| { format!(" {} => {{ ... }},\n", span.to_src(cx)) })
2364+
.collect::<String>(),
2365+
)
2366+
}
2367+
};
2368+
2369+
Item::from_hir_id_and_parts(
2370+
item.hir_id,
2371+
Some(name),
2372+
MacroItem(Macro { source, imported_from: None }),
23462373
cx,
23472374
)
23482375
}

src/librustdoc/doctree.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ crate struct Module<'hir> {
1818
// (item, renamed)
1919
crate items: Vec<(&'hir hir::Item<'hir>, Option<Ident>)>,
2020
crate foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Ident>)>,
21-
crate macros: Vec<Macro>,
21+
crate macros: Vec<(&'hir hir::MacroDef<'hir>, Option<Ident>)>,
2222
crate is_crate: bool,
2323
}
2424

@@ -56,15 +56,6 @@ crate struct Variant<'hir> {
5656
crate def: &'hir hir::VariantData<'hir>,
5757
}
5858

59-
// For Macro we store the DefId instead of the NodeId, since we also create
60-
// these imported macro_rules (which only have a DUMMY_NODE_ID).
61-
crate struct Macro {
62-
crate name: Symbol,
63-
crate def_id: hir::def_id::DefId,
64-
crate matchers: Vec<Span>,
65-
crate imported_from: Option<Symbol>,
66-
}
67-
6859
#[derive(Debug)]
6960
crate struct Import<'hir> {
7061
crate name: Symbol,

src/librustdoc/visit_ast.rs

+2-19
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
7171
None,
7272
);
7373
// Attach the crate's exported macros to the top-level module:
74-
module
75-
.macros
76-
.extend(krate.exported_macros.iter().map(|def| self.visit_local_macro(def, None)));
74+
module.macros.extend(krate.exported_macros.iter().map(|def| (def, None)));
7775
module.is_crate = true;
7876

7977
self.cx.renderinfo.get_mut().exact_paths = self.exact_paths;
@@ -216,7 +214,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
216214
true
217215
}
218216
Node::MacroDef(def) if !glob => {
219-
om.macros.push(self.visit_local_macro(def, renamed.map(|i| i.name)));
217+
om.macros.push((def, renamed));
220218
true
221219
}
222220
_ => false,
@@ -339,19 +337,4 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
339337
om.foreigns.push((item, renamed));
340338
}
341339
}
342-
343-
// Convert each `exported_macro` into a doc item.
344-
fn visit_local_macro(&self, def: &'tcx hir::MacroDef<'_>, renamed: Option<Symbol>) -> Macro {
345-
debug!("visit_local_macro: {}", def.ident);
346-
let tts = def.ast.body.inner_tokens().trees().collect::<Vec<_>>();
347-
// Extract the spans of all matchers. They represent the "interface" of the macro.
348-
let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect();
349-
350-
Macro {
351-
def_id: self.cx.tcx.hir().local_def_id(def.hir_id).to_def_id(),
352-
name: renamed.unwrap_or(def.ident.name),
353-
matchers,
354-
imported_from: None,
355-
}
356-
}
357340
}

src/test/rustdoc/decl_macro.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#![feature(decl_macro)]
2+
3+
// @has decl_macro/macro.my_macro.html //pre 'pub macro my_macro() {'
4+
// @has - //pre '...'
5+
// @has - //pre '}'
6+
pub macro my_macro() {
7+
8+
}
9+
10+
// @has decl_macro/macro.my_macro_2.html //pre 'pub macro my_macro_2($($tok:tt)*) {'
11+
// @has - //pre '...'
12+
// @has - //pre '}'
13+
pub macro my_macro_2($($tok:tt)*) {
14+
15+
}
16+
17+
// @has decl_macro/macro.my_macro_multi.html //pre 'pub macro my_macro_multi {'
18+
// @has - //pre '(_) => { ... },'
19+
// @has - //pre '($foo:ident . $bar:expr) => { ... },'
20+
// @has - //pre '($($foo:literal),+) => { ... }'
21+
// @has - //pre '}'
22+
pub macro my_macro_multi {
23+
(_) => {
24+
25+
},
26+
($foo:ident . $bar:expr) => {
27+
28+
},
29+
($($foo:literal),+) => {
30+
31+
}
32+
}
33+
34+
// @has decl_macro/macro.by_example_single.html //pre 'pub macro by_example_single($foo:expr) {'
35+
// @has - //pre '...'
36+
// @has - //pre '}'
37+
pub macro by_example_single {
38+
($foo:expr) => {}
39+
}

src/test/rustdoc/decl_macro_priv.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// compile-flags: --document-private-items
2+
3+
#![feature(decl_macro)]
4+
5+
// @has decl_macro_priv/macro.crate_macro.html //pre 'pub(crate) macro crate_macro() {'
6+
// @has - //pre '...'
7+
// @has - //pre '}'
8+
pub(crate) macro crate_macro() {}
9+
10+
// @has decl_macro_priv/macro.priv_macro.html //pre 'macro priv_macro() {'
11+
// @!has - //pre 'pub macro priv_macro() {'
12+
// @has - //pre '...'
13+
// @has - //pre '}'
14+
macro priv_macro() {}

0 commit comments

Comments
 (0)