about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorNixon Enraght-Moony <nixon.emoony@gmail.com>2021-03-24 15:54:20 +0000
committerNixon Enraght-Moony <nixon.emoony@gmail.com>2021-03-24 16:54:13 +0000
commit9ba92972ed6a1a39afa5993da97e016eea907be2 (patch)
tree5559412e5521051f794cf4254f9ec335b0e80810 /src
parentce21447c01c8ea6ce4f4c9dd2c18266439200f1d (diff)
downloadrust-9ba92972ed6a1a39afa5993da97e016eea907be2.tar.gz
rust-9ba92972ed6a1a39afa5993da97e016eea907be2.zip
Don't call `item` on modules for json renderer
Closes #80664
Diffstat (limited to 'src')
-rw-r--r--src/librustdoc/formats/renderer.rs7
-rw-r--r--src/librustdoc/html/render/context.rs2
-rw-r--r--src/librustdoc/json/conversions.rs3
-rw-r--r--src/librustdoc/json/mod.rs6
-rw-r--r--src/test/rustdoc-json/reexport/in_root_and_mod.rs15
-rw-r--r--src/test/rustdoc-json/reexport/in_root_and_mod_pub.rs20
-rw-r--r--src/test/rustdoc-json/reexport/rename_private.rs14
7 files changed, 64 insertions, 3 deletions
diff --git a/src/librustdoc/formats/renderer.rs b/src/librustdoc/formats/renderer.rs
index 9dcef3a20d6..4e0f3a4e3c3 100644
--- a/src/librustdoc/formats/renderer.rs
+++ b/src/librustdoc/formats/renderer.rs
@@ -13,6 +13,11 @@ crate trait FormatRenderer<'tcx>: Sized {
     /// Gives a description of the renderer. Used for performance profiling.
     fn descr() -> &'static str;
 
+    /// Whether to call `item` recursivly for modules
+    ///
+    /// This is true for html, and false for json. See #80664
+    const RUN_ON_MODULE: bool;
+
     /// Sets up any state required for the renderer. When this is called the cache has already been
     /// populated.
     fn init(
@@ -68,7 +73,7 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
 
     let unknown = Symbol::intern("<unknown item>");
     while let Some((mut cx, item)) = work.pop() {
-        if item.is_mod() {
+        if item.is_mod() && T::RUN_ON_MODULE {
             // modules are special because they add a namespace. We also need to
             // recurse into the items of the module as well.
             let name = item.name.as_ref().unwrap().to_string();
diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs
index 64d413a5f31..0ffb4d616da 100644
--- a/src/librustdoc/html/render/context.rs
+++ b/src/librustdoc/html/render/context.rs
@@ -290,6 +290,8 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
         "html"
     }
 
+    const RUN_ON_MODULE: bool = true;
+
     fn init(
         mut krate: clean::Crate,
         options: RenderOptions,
diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs
index 073209c2468..6da0d881640 100644
--- a/src/librustdoc/json/conversions.rs
+++ b/src/librustdoc/json/conversions.rs
@@ -179,7 +179,8 @@ fn from_clean_item_kind(item: clean::ItemKind, tcx: TyCtxt<'_>, name: &Option<Sy
             bounds: g.into_iter().map(Into::into).collect(),
             default: t.map(Into::into),
         },
-        StrippedItem(inner) => from_clean_item_kind(*inner, tcx, name),
+        // `convert_item` early returns `None` for striped items
+        StrippedItem(_) => unreachable!(),
         PrimitiveItem(_) | KeywordItem(_) => {
             panic!("{:?} is not supported for JSON output", item)
         }
diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs
index a4cdad69865..5d9c598549e 100644
--- a/src/librustdoc/json/mod.rs
+++ b/src/librustdoc/json/mod.rs
@@ -129,6 +129,8 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
         "json"
     }
 
+    const RUN_ON_MODULE: bool = false;
+
     fn init(
         krate: clean::Crate,
         options: RenderOptions,
@@ -169,8 +171,10 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
                 e.impls = self.get_impls(id)
             }
             let removed = self.index.borrow_mut().insert(from_def_id(id), new_item.clone());
+
             // FIXME(adotinthevoid): Currently, the index is duplicated. This is a sanity check
-            // to make sure the items are unique.
+            // to make sure the items are unique. The main place this happens is when an item, is
+            // reexported in more than one place. See `rustdoc-json/reexport/in_root_and_mod`
             if let Some(old_item) = removed {
                 assert_eq!(old_item, new_item);
             }
diff --git a/src/test/rustdoc-json/reexport/in_root_and_mod.rs b/src/test/rustdoc-json/reexport/in_root_and_mod.rs
new file mode 100644
index 00000000000..e3cecbdd7ff
--- /dev/null
+++ b/src/test/rustdoc-json/reexport/in_root_and_mod.rs
@@ -0,0 +1,15 @@
+#![feature(no_core)]
+#![no_core]
+
+mod foo {
+    // @set foo_id = in_root_and_mod.json "$.index[*][?(@.name=='Foo')].id"
+    pub struct Foo;
+}
+
+// @has - "$.index[*][?(@.name=='in_root_and_mod')].inner.items[*]" $foo_id
+pub use foo::Foo;
+
+pub mod bar {
+    // @has - "$.index[*][?(@.name=='bar')].inner.items[*]" $foo_id
+    pub use crate::foo::Foo;
+}
diff --git a/src/test/rustdoc-json/reexport/in_root_and_mod_pub.rs b/src/test/rustdoc-json/reexport/in_root_and_mod_pub.rs
new file mode 100644
index 00000000000..8adb05f7be8
--- /dev/null
+++ b/src/test/rustdoc-json/reexport/in_root_and_mod_pub.rs
@@ -0,0 +1,20 @@
+#![feature(no_core)]
+#![no_core]
+
+pub mod foo {
+    // @set bar_id = in_root_and_mod_pub.json "$.index[*][?(@.name=='Bar')].id"
+    // @has - "$.index[*][?(@.name=='foo')].inner.items[*]" $bar_id
+    pub struct Bar;
+}
+
+// @set root_import_id = - "$.index[*][?(@.inner.span=='foo::Bar')].id"
+// @is - "$.index[*][?(@.inner.span=='foo::Bar')].inner.id" $bar_id
+// @has - "$.index[*][?(@.name=='in_root_and_mod_pub')].inner.items[*]" $root_import_id
+pub use foo::Bar;
+
+pub mod baz {
+    // @set baz_import_id = - "$.index[*][?(@.inner.span=='crate::foo::Bar')].id"
+    // @is - "$.index[*][?(@.inner.span=='crate::foo::Bar')].inner.id" $bar_id
+    // @has - "$.index[*][?(@.name=='baz')].inner.items[*]" $baz_import_id
+    pub use crate::foo::Bar;
+}
diff --git a/src/test/rustdoc-json/reexport/rename_private.rs b/src/test/rustdoc-json/reexport/rename_private.rs
new file mode 100644
index 00000000000..fb8296f2337
--- /dev/null
+++ b/src/test/rustdoc-json/reexport/rename_private.rs
@@ -0,0 +1,14 @@
+// edition:2018
+
+#![no_core]
+#![feature(no_core)]
+// @!has rename_private.json "$.index[*][?(@.name=='inner')]"
+mod inner {
+    // @!has - "$.index[*][?(@.name=='Public')]"
+    pub struct Public;
+}
+
+// @set newname_id = - "$.index[*][?(@.name=='NewName')].id"
+// @is - "$.index[*][?(@.name=='NewName')].kind" \"struct\"
+// @has - "$.index[*][?(@.name=='rename_private')].inner.items[*]" $newname_id
+pub use inner::Public as NewName;