about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-11-29 07:05:49 +0000
committerbors <bors@rust-lang.org>2020-11-29 07:05:49 +0000
commit3cbb56f80b2fbf5a3b405212665918dbffc44d39 (patch)
treef26fd41a0aea2d035120ec2f81dba27f86ae3a6d
parent6add378d6b77a727697f2920a9cc85a63766297e (diff)
parentd23b57c1a8d1002df09c6213bd28884d973e116a (diff)
downloadrust-3cbb56f80b2fbf5a3b405212665918dbffc44d39.tar.gz
rust-3cbb56f80b2fbf5a3b405212665918dbffc44d39.zip
Auto merge of #79455 - CraftSpider:master, r=jyn514
Remove doctree::Macro and distinguish between `macro_rules!` and `pub macro`

This is a part of #78082, removing doctree::Macro. Uses the changes in #79372

Fixes #76761
-rw-r--r--src/librustdoc/clean/mod.rs55
-rw-r--r--src/librustdoc/doctree.rs11
-rw-r--r--src/librustdoc/visit_ast.rs21
-rw-r--r--src/test/rustdoc/decl_macro.rs39
-rw-r--r--src/test/rustdoc/decl_macro_priv.rs14
5 files changed, 97 insertions, 43 deletions
diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs
index 221c818736e..9e8ce452924 100644
--- a/src/librustdoc/clean/mod.rs
+++ b/src/librustdoc/clean/mod.rs
@@ -2327,22 +2327,49 @@ impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Ident>) {
     }
 }
 
-impl Clean<Item> for doctree::Macro {
+impl Clean<Item> for (&hir::MacroDef<'_>, Option<Ident>) {
     fn clean(&self, cx: &DocContext<'_>) -> Item {
-        Item::from_def_id_and_parts(
-            self.def_id,
-            Some(self.name.clean(cx)),
-            MacroItem(Macro {
-                source: format!(
-                    "macro_rules! {} {{\n{}}}",
-                    self.name,
-                    self.matchers
+        let (item, renamed) = self;
+        let name = renamed.unwrap_or(item.ident).name;
+        let tts = item.ast.body.inner_tokens().trees().collect::<Vec<_>>();
+        // Extract the spans of all matchers. They represent the "interface" of the macro.
+        let matchers = tts.chunks(4).map(|arm| arm[0].span()).collect::<Vec<_>>();
+        let source = if item.ast.macro_rules {
+            format!(
+                "macro_rules! {} {{\n{}}}",
+                name,
+                matchers
+                    .iter()
+                    .map(|span| { format!("    {} => {{ ... }};\n", span.to_src(cx)) })
+                    .collect::<String>(),
+            )
+        } else {
+            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::<String>(),
+                )
+            } else {
+                format!(
+                    "{}macro {} {{\n{}}}",
+                    vis.print_with_space(),
+                    name,
+                    matchers
                         .iter()
-                        .map(|span| { format!("    {} => {{ ... }};\n", span.to_src(cx)) })
-                        .collect::<String>()
-                ),
-                imported_from: self.imported_from.clean(cx),
-            }),
+                        .map(|span| { format!("    {} => {{ ... }},\n", span.to_src(cx)) })
+                        .collect::<String>(),
+                )
+            }
+        };
+
+        Item::from_hir_id_and_parts(
+            item.hir_id,
+            Some(name),
+            MacroItem(Macro { source, imported_from: None }),
             cx,
         )
     }
diff --git a/src/librustdoc/doctree.rs b/src/librustdoc/doctree.rs
index 20f747e2014..3961870a1bf 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<Ident>)>,
     crate foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Ident>)>,
-    crate macros: Vec<Macro>,
+    crate macros: Vec<(&'hir hir::MacroDef<'hir>, Option<Ident>)>,
     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<Span>,
-    crate imported_from: Option<Symbol>,
-}
-
 #[derive(Debug)]
 crate struct Import<'hir> {
     crate name: Symbol,
diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs
index 02152edbbc2..4028293076d 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<Symbol>) -> Macro {
-        debug!("visit_local_macro: {}", def.ident);
-        let tts = def.ast.body.inner_tokens().trees().collect::<Vec<_>>();
-        // 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,
-        }
-    }
 }
diff --git a/src/test/rustdoc/decl_macro.rs b/src/test/rustdoc/decl_macro.rs
new file mode 100644
index 00000000000..e48a56f906c
--- /dev/null
+++ b/src/test/rustdoc/decl_macro.rs
@@ -0,0 +1,39 @@
+#![feature(decl_macro)]
+
+// @has decl_macro/macro.my_macro.html //pre 'pub macro my_macro() {'
+// @has - //pre '...'
+// @has - //pre '}'
+pub macro my_macro() {
+
+}
+
+// @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)*) {
+
+}
+
+// @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),+) => {
+
+    }
+}
+
+// @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) => {}
+}
diff --git a/src/test/rustdoc/decl_macro_priv.rs b/src/test/rustdoc/decl_macro_priv.rs
new file mode 100644
index 00000000000..4e1279e34d9
--- /dev/null
+++ b/src/test/rustdoc/decl_macro_priv.rs
@@ -0,0 +1,14 @@
+// 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 'pub macro priv_macro() {'
+// @has - //pre '...'
+// @has - //pre '}'
+macro priv_macro() {}