From ccb9cc1200e26a5f0cdaac321bd10185d91e87e4 Mon Sep 17 00:00:00 2001 From: Rune Tynan Date: Thu, 26 Nov 2020 18:36:59 -0500 Subject: [PATCH 1/7] Remove doctree::Macro --- src/librustdoc/clean/mod.rs | 21 ++++++++++++++------- src/librustdoc/doctree.rs | 11 +---------- src/librustdoc/visit_ast.rs | 21 ++------------------- 3 files changed, 17 insertions(+), 36 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 03fda94a6dfc5..f417eb6740cdf 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2313,21 +2313,28 @@ impl Clean for (&hir::ForeignItem<'_>, Option) { } } -impl Clean for doctree::Macro { +impl Clean for (&hir::MacroDef<'_>, Option) { fn clean(&self, cx: &DocContext<'_>) -> Item { + let (item, renamed) = self; + let name = renamed.unwrap_or(item.ident).name; + let tts = item.ast.body.inner_tokens().trees().collect::>(); + // Extract the spans of all matchers. They represent the "interface" of the macro. + let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect::>(); + Item::from_def_id_and_parts( - self.def_id, - Some(self.name.clean(cx)), + cx.tcx.hir().local_def_id(item.hir_id).to_def_id(), + Some(name.clean(cx)), MacroItem(Macro { + // FIXME(#76761): Make this respect `macro_rules!` vs `pub macro` source: format!( "macro_rules! {} {{\n{}}}", - self.name, - self.matchers + name, + matchers .iter() .map(|span| { format!(" {} => {{ ... }};\n", span.to_src(cx)) }) - .collect::() + .collect::(), ), - imported_from: self.imported_from.clean(cx), + imported_from: None, }), cx, ) diff --git a/src/librustdoc/doctree.rs b/src/librustdoc/doctree.rs index 20f747e201415..3961870a1bfad 100644 --- a/src/librustdoc/doctree.rs +++ b/src/librustdoc/doctree.rs @@ -18,7 +18,7 @@ crate struct Module<'hir> { // (item, renamed) crate items: Vec<(&'hir hir::Item<'hir>, Option)>, crate foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option)>, - crate macros: Vec, + crate macros: Vec<(&'hir hir::MacroDef<'hir>, Option)>, crate is_crate: bool, } @@ -56,15 +56,6 @@ crate struct Variant<'hir> { crate def: &'hir hir::VariantData<'hir>, } -// For Macro we store the DefId instead of the NodeId, since we also create -// these imported macro_rules (which only have a DUMMY_NODE_ID). -crate struct Macro { - crate name: Symbol, - crate def_id: hir::def_id::DefId, - crate matchers: Vec, - crate imported_from: Option, -} - #[derive(Debug)] crate struct Import<'hir> { crate name: Symbol, diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 02152edbbc21c..4028293076df9 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -71,9 +71,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { None, ); // Attach the crate's exported macros to the top-level module: - module - .macros - .extend(krate.exported_macros.iter().map(|def| self.visit_local_macro(def, None))); + module.macros.extend(krate.exported_macros.iter().map(|def| (def, None))); module.is_crate = true; self.cx.renderinfo.get_mut().exact_paths = self.exact_paths; @@ -216,7 +214,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { true } Node::MacroDef(def) if !glob => { - om.macros.push(self.visit_local_macro(def, renamed.map(|i| i.name))); + om.macros.push((def, renamed)); true } _ => false, @@ -339,19 +337,4 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { om.foreigns.push((item, renamed)); } } - - // Convert each `exported_macro` into a doc item. - fn visit_local_macro(&self, def: &'tcx hir::MacroDef<'_>, renamed: Option) -> Macro { - debug!("visit_local_macro: {}", def.ident); - let tts = def.ast.body.inner_tokens().trees().collect::>(); - // Extract the spans of all matchers. They represent the "interface" of the macro. - let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect(); - - Macro { - def_id: self.cx.tcx.hir().local_def_id(def.hir_id).to_def_id(), - name: renamed.unwrap_or(def.ident.name), - matchers, - imported_from: None, - } - } } From 18276b2dbd4528edd7f41a0b3345438542167723 Mon Sep 17 00:00:00 2001 From: Rune Tynan Date: Thu, 26 Nov 2020 21:17:13 -0500 Subject: [PATCH 2/7] Apply review: use from_hir, add macro source fix. --- src/librustdoc/clean/mod.rs | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index f417eb6740cdf..d9a3c16fb5351 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2320,22 +2320,29 @@ impl Clean for (&hir::MacroDef<'_>, Option) { let tts = item.ast.body.inner_tokens().trees().collect::>(); // Extract the spans of all matchers. They represent the "interface" of the macro. let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect::>(); + let source = if item.ast.macro_rules { + format!( + "macro_rules! {} {{\n{}}}", + name, + matchers + .iter() + .map(|span| { format!(" {} => {{ ... }};\n", span.to_src(cx)) }) + .collect::(), + ) + } else { + // This code currently assumes that there will only be one or zero matchers, as syntax + // for multiple is not currently defined. + format!( + "pub macro {}({}) {{\n\t...\n}}", + name, + matchers.iter().map(|span| span.to_src(cx)).collect::(), + ) + }; - Item::from_def_id_and_parts( - cx.tcx.hir().local_def_id(item.hir_id).to_def_id(), + Item::from_hir_id_and_parts( + item.hir_id, Some(name.clean(cx)), - MacroItem(Macro { - // FIXME(#76761): Make this respect `macro_rules!` vs `pub macro` - source: format!( - "macro_rules! {} {{\n{}}}", - name, - matchers - .iter() - .map(|span| { format!(" {} => {{ ... }};\n", span.to_src(cx)) }) - .collect::(), - ), - imported_from: None, - }), + MacroItem(Macro { source, imported_from: None }), cx, ) } From c007f4354dad24c38639c96dd83af9b69bff50e6 Mon Sep 17 00:00:00 2001 From: Rune Tynan Date: Thu, 26 Nov 2020 21:57:11 -0500 Subject: [PATCH 3/7] Add test, fix pub macro impl, compile error --- src/librustdoc/clean/mod.rs | 5 +++-- src/test/rustdoc/macros_2.rs | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 src/test/rustdoc/macros_2.rs diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index d9a3c16fb5351..f16ad40d6e2f3 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2333,7 +2333,8 @@ impl Clean for (&hir::MacroDef<'_>, Option) { // This code currently assumes that there will only be one or zero matchers, as syntax // for multiple is not currently defined. format!( - "pub macro {}({}) {{\n\t...\n}}", + "{}macro {}{} {{\n\t...\n}}", + item.vis.clean(cx).print_with_space(), name, matchers.iter().map(|span| span.to_src(cx)).collect::(), ) @@ -2341,7 +2342,7 @@ impl Clean for (&hir::MacroDef<'_>, Option) { Item::from_hir_id_and_parts( item.hir_id, - Some(name.clean(cx)), + Some(name), MacroItem(Macro { source, imported_from: None }), cx, ) diff --git a/src/test/rustdoc/macros_2.rs b/src/test/rustdoc/macros_2.rs new file mode 100644 index 0000000000000..c1e4d6702c4a6 --- /dev/null +++ b/src/test/rustdoc/macros_2.rs @@ -0,0 +1,16 @@ + +#![feature(decl_macro)] + +// @has macros_2/macro.my_macro.html //pre 'pub macro my_macro() {' +// @has - //pre '...' +// @has - //pre '}' +pub macro my_macro() { + +} + +// @has macros_2/macro.my_macro_2.html //pre 'pub macro my_macro_2($($tok:tt)*) {' +// @has - //pre '...' +// @has - //pre '}' +pub macro my_macro_2($($tok:tt)*) { + +} From 949b72e9d901181966b7208f5e7c74ccc025d679 Mon Sep 17 00:00:00 2001 From: Rune Tynan Date: Thu, 26 Nov 2020 22:08:59 -0500 Subject: [PATCH 4/7] Update decl_macro test, add decl_macro_priv test for --document-private-items --- src/test/rustdoc/{macros_2.rs => decl_macro.rs} | 5 ++--- src/test/rustdoc/decl_macro_priv.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) rename src/test/rustdoc/{macros_2.rs => decl_macro.rs} (53%) create mode 100644 src/test/rustdoc/decl_macro_priv.rs diff --git a/src/test/rustdoc/macros_2.rs b/src/test/rustdoc/decl_macro.rs similarity index 53% rename from src/test/rustdoc/macros_2.rs rename to src/test/rustdoc/decl_macro.rs index c1e4d6702c4a6..45b4a3fac2168 100644 --- a/src/test/rustdoc/macros_2.rs +++ b/src/test/rustdoc/decl_macro.rs @@ -1,14 +1,13 @@ - #![feature(decl_macro)] -// @has macros_2/macro.my_macro.html //pre 'pub macro my_macro() {' +// @has decl_macro/macro.my_macro.html //pre 'pub macro my_macro() {' // @has - //pre '...' // @has - //pre '}' pub macro my_macro() { } -// @has macros_2/macro.my_macro_2.html //pre 'pub macro my_macro_2($($tok:tt)*) {' +// @has decl_macro/macro.my_macro_2.html //pre 'pub macro my_macro_2($($tok:tt)*) {' // @has - //pre '...' // @has - //pre '}' pub macro my_macro_2($($tok:tt)*) { diff --git a/src/test/rustdoc/decl_macro_priv.rs b/src/test/rustdoc/decl_macro_priv.rs new file mode 100644 index 0000000000000..6e2258adf6f56 --- /dev/null +++ b/src/test/rustdoc/decl_macro_priv.rs @@ -0,0 +1,13 @@ +// compile-flags: --document-private-items + +#![feature(decl_macro)] + +// @has decl_macro_priv/macro.crate_macro.html //pre 'pub(crate) macro crate_macro() {' +// @has - //pre '...' +// @has - //pre '}' +pub(crate) macro crate_macro() {} + +// @has decl_macro_priv/macro.priv_macro.html //pre 'macro priv_macro() {' +// @has - //pre '...' +// @has - //pre '}' +macro priv_macro() {} From a61c09a897e671814ea104ace21a40eaac1d9a9c Mon Sep 17 00:00:00 2001 From: Rune Tynan Date: Sat, 28 Nov 2020 00:02:46 -0500 Subject: [PATCH 5/7] Update src/test/rustdoc/decl_macro_priv.rs Co-authored-by: Joshua Nelson --- src/test/rustdoc/decl_macro_priv.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/rustdoc/decl_macro_priv.rs b/src/test/rustdoc/decl_macro_priv.rs index 6e2258adf6f56..4e1279e34d933 100644 --- a/src/test/rustdoc/decl_macro_priv.rs +++ b/src/test/rustdoc/decl_macro_priv.rs @@ -8,6 +8,7 @@ pub(crate) macro crate_macro() {} // @has decl_macro_priv/macro.priv_macro.html //pre 'macro priv_macro() {' +// @!has - //pre 'pub macro priv_macro() {' // @has - //pre '...' // @has - //pre '}' macro priv_macro() {} From ff690931b7aeff7c1b0f5bf6a33ce2b1b980ab70 Mon Sep 17 00:00:00 2001 From: Rune Tynan Date: Sat, 28 Nov 2020 20:46:17 -0500 Subject: [PATCH 6/7] Add support for multi-argument decl macros --- src/librustdoc/clean/mod.rs | 28 ++++++++++++++++++++-------- src/test/rustdoc/decl_macro.rs | 17 +++++++++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index f16ad40d6e2f3..54719e4d6a24e 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2330,14 +2330,26 @@ impl Clean for (&hir::MacroDef<'_>, Option) { .collect::(), ) } else { - // This code currently assumes that there will only be one or zero matchers, as syntax - // for multiple is not currently defined. - format!( - "{}macro {}{} {{\n\t...\n}}", - item.vis.clean(cx).print_with_space(), - name, - matchers.iter().map(|span| span.to_src(cx)).collect::(), - ) + let vis = item.vis.clean(cx); + + if matchers.len() <= 1 { + format!( + "{}macro {}{} {{\n ...\n}}", + vis.print_with_space(), + name, + matchers.iter().map(|span| span.to_src(cx)).collect::(), + ) + } else { + format!( + "{}macro {} {{\n{}}}", + vis.print_with_space(), + name, + matchers + .iter() + .map(|span| { format!(" {} => {{ ... }},\n", span.to_src(cx)) }) + .collect::(), + ) + } }; Item::from_hir_id_and_parts( diff --git a/src/test/rustdoc/decl_macro.rs b/src/test/rustdoc/decl_macro.rs index 45b4a3fac2168..42a2979948336 100644 --- a/src/test/rustdoc/decl_macro.rs +++ b/src/test/rustdoc/decl_macro.rs @@ -13,3 +13,20 @@ pub macro my_macro() { pub macro my_macro_2($($tok:tt)*) { } + +// @has decl_macro/macro.my_macro_multi.html //pre 'pub macro my_macro_multi {' +// @has - //pre '(_) => { ... },' +// @has - //pre '($foo:ident . $bar:expr) => { ... },' +// @has - //pre '($($foo:literal),+) => { ... }' +// @has - //pre '}' +pub macro my_macro_multi { + (_) => { + + }, + ($foo:ident . $bar:expr) => { + + }, + ($($foo:literal),+) => { + + } +} From d23b57c1a8d1002df09c6213bd28884d973e116a Mon Sep 17 00:00:00 2001 From: Rune Tynan Date: Sat, 28 Nov 2020 21:32:07 -0500 Subject: [PATCH 7/7] Add test for macro by example syntax in decl macros with only one option --- src/test/rustdoc/decl_macro.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/rustdoc/decl_macro.rs b/src/test/rustdoc/decl_macro.rs index 42a2979948336..e48a56f906c95 100644 --- a/src/test/rustdoc/decl_macro.rs +++ b/src/test/rustdoc/decl_macro.rs @@ -30,3 +30,10 @@ pub macro my_macro_multi { } } + +// @has decl_macro/macro.by_example_single.html //pre 'pub macro by_example_single($foo:expr) {' +// @has - //pre '...' +// @has - //pre '}' +pub macro by_example_single { + ($foo:expr) => {} +}