about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2022-05-06 20:05:41 +0200
committerGitHub <noreply@github.com>2022-05-06 20:05:41 +0200
commit93b86d69b649a0c4f1ba5ea9d141c1af633be576 (patch)
tree216d58097e814113f1e00f9f3e66ecbefe7912a3
parentc3ddd59f5b0ed31c83a4421fdbdfb73db8ba378f (diff)
parentfb2f97a37e8a3541368004daa5ddb38ec3048fe8 (diff)
downloadrust-93b86d69b649a0c4f1ba5ea9d141c1af633be576.tar.gz
rust-93b86d69b649a0c4f1ba5ea9d141c1af633be576.zip
Rollup merge of #96748 - GuillaumeGomez:reexports-in-search, r=notriddle
Fixes reexports in search

Fixes #96681.

At some point we stopped reexporting items in search so this PR fixes it.

It also adds a regression test.

r? ```@notriddle```
-rw-r--r--src/librustdoc/formats/cache.rs11
-rw-r--r--src/librustdoc/html/render/mod.rs11
-rw-r--r--src/librustdoc/html/render/print_item.rs7
-rw-r--r--src/librustdoc/html/static/js/search.js3
-rw-r--r--src/test/rustdoc-gui/search-reexport.goml29
-rw-r--r--src/test/rustdoc-gui/sidebar.goml19
-rw-r--r--src/test/rustdoc-gui/src/test_docs/lib.rs3
7 files changed, 71 insertions, 12 deletions
diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs
index b4d2772b31d..30190138750 100644
--- a/src/librustdoc/formats/cache.rs
+++ b/src/librustdoc/formats/cache.rs
@@ -248,7 +248,16 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
         }
 
         // Index this method for searching later on.
-        if let Some(ref s) = item.name {
+        if let Some(ref s) = item.name.or_else(|| {
+            if item.is_stripped() {
+                None
+            } else if let clean::ImportItem(ref i) = *item.kind &&
+                let clean::ImportKind::Simple(s) = i.kind {
+                Some(s)
+            } else {
+                None
+            }
+        }) {
             let (parent, is_inherent_impl_item) = match *item.kind {
                 clean::StrippedItem(..) => ((None, None), false),
                 clean::AssocConstItem(..) | clean::AssocTypeItem(..)
diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs
index fedeb449b2e..19582481910 100644
--- a/src/librustdoc/html/render/mod.rs
+++ b/src/librustdoc/html/render/mod.rs
@@ -2542,7 +2542,16 @@ fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
 
     let item_sections_in_use: FxHashSet<_> = items
         .iter()
-        .filter(|it| !it.is_stripped() && it.name.is_some())
+        .filter(|it| {
+            !it.is_stripped()
+                && it
+                    .name
+                    .or_else(|| {
+                        if let clean::ImportItem(ref i) = *it.kind &&
+                            let clean::ImportKind::Simple(s) = i.kind { Some(s) } else { None }
+                    })
+                    .is_some()
+        })
         .map(|it| item_ty_to_section(it.type_()))
         .collect();
     for &sec in ItemSection::ALL.iter().filter(|sec| item_sections_in_use.contains(sec)) {
diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs
index f1915920b6d..0ad54abf807 100644
--- a/src/librustdoc/html/render/print_item.rs
+++ b/src/librustdoc/html/render/print_item.rs
@@ -346,7 +346,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
                 w.write_str(ITEM_TABLE_ROW_OPEN);
                 write!(
                     w,
-                    "<div class=\"item-left {stab}{add}import-item\">\
+                    "<div class=\"item-left {stab}{add}import-item\"{id}>\
                          <code>{vis}{imp}</code>\
                      </div>\
                      <div class=\"item-right docblock-short\">{stab_tags}</div>",
@@ -355,6 +355,11 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
                     vis = myitem.visibility.print_with_space(myitem.item_id, cx),
                     imp = import.print(cx),
                     stab_tags = stab_tags.unwrap_or_default(),
+                    id = match import.kind {
+                        clean::ImportKind::Simple(s) =>
+                            format!(" id=\"{}\"", cx.derive_id(format!("reexport.{}", s))),
+                        clean::ImportKind::Glob => String::new(),
+                    },
                 );
                 w.write_str(ITEM_TABLE_ROW_CLOSE);
             }
diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js
index 02370a1243a..2eafa540a1a 100644
--- a/src/librustdoc/html/static/js/search.js
+++ b/src/librustdoc/html/static/js/search.js
@@ -1510,6 +1510,9 @@ window.initSearch = rawSearchIndex => {
             displayPath = path + "::";
             href = window.rootPath + path.replace(/::/g, "/") + "/" +
                    name + "/index.html";
+        } else if (type === "import") {
+            displayPath = item.path + "::";
+            href = window.rootPath + item.path.replace(/::/g, "/") + "/index.html#reexport." + name;
         } else if (type === "primitive" || type === "keyword") {
             displayPath = "";
             href = window.rootPath + path.replace(/::/g, "/") +
diff --git a/src/test/rustdoc-gui/search-reexport.goml b/src/test/rustdoc-gui/search-reexport.goml
new file mode 100644
index 00000000000..557781d4810
--- /dev/null
+++ b/src/test/rustdoc-gui/search-reexport.goml
@@ -0,0 +1,29 @@
+// Checks that the reexports are present in the search index, can have
+// doc aliases and are highligted when their ID is the hash of the page.
+goto: file://|DOC_PATH|/test_docs/index.html
+local-storage: {"rustdoc-theme": "dark", "rustdoc-use-system-theme": "false"}
+reload:
+// First we check that the reexport has the correct ID and no background color.
+assert-text: ("//*[@id='reexport.TheStdReexport']", "pub use ::std as TheStdReexport;")
+assert-css: ("//*[@id='reexport.TheStdReexport']", {"background-color": "rgba(0, 0, 0, 0)"})
+write: (".search-input", "TheStdReexport")
+wait-for: "//a[@class='result-import']"
+assert-attribute: (
+    "//a[@class='result-import']",
+    {"href": "../test_docs/index.html#reexport.TheStdReexport"},
+)
+assert-text: ("//a[@class='result-import']", "test_docs::TheStdReexport")
+click: "//a[@class='result-import']"
+// We check that it has the background modified thanks to the focus.
+wait-for-css: ("//*[@id='reexport.TheStdReexport']", {"background-color": "rgb(73, 74, 61)"})
+
+// We now check that the alias is working as well on the reexport.
+write: (".search-input", "AliasForTheStdReexport")
+wait-for: "//a[@class='result-import']"
+assert-text: (
+    "//a[@class='result-import']",
+    "AliasForTheStdReexport - see test_docs::TheStdReexport",
+)
+// Same thing again, we click on it to ensure the background is once again set as expected.
+click: "//a[@class='result-import']"
+wait-for-css: ("//*[@id='reexport.TheStdReexport']", {"background-color": "rgb(73, 74, 61)"})
diff --git a/src/test/rustdoc-gui/sidebar.goml b/src/test/rustdoc-gui/sidebar.goml
index 6b79b00d3f7..32fe3334f36 100644
--- a/src/test/rustdoc-gui/sidebar.goml
+++ b/src/test/rustdoc-gui/sidebar.goml
@@ -13,15 +13,16 @@ assert-css: ("#all-types", {"color": "rgb(53, 109, 164)"})
 // We check that we have the crates list and that the "current" on is "test_docs".
 assert-text: (".sidebar-elems .crate > ul > li > a.current", "test_docs")
 // And we're also supposed to have the list of items in the current module.
-assert-text: (".sidebar-elems section ul > li:nth-child(1)", "Modules")
-assert-text: (".sidebar-elems section ul > li:nth-child(2)", "Macros")
-assert-text: (".sidebar-elems section ul > li:nth-child(3)", "Structs")
-assert-text: (".sidebar-elems section ul > li:nth-child(4)", "Enums")
-assert-text: (".sidebar-elems section ul > li:nth-child(5)", "Traits")
-assert-text: (".sidebar-elems section ul > li:nth-child(6)", "Functions")
-assert-text: (".sidebar-elems section ul > li:nth-child(7)", "Type Definitions")
-assert-text: (".sidebar-elems section ul > li:nth-child(8)", "Unions")
-assert-text: (".sidebar-elems section ul > li:nth-child(9)", "Keywords")
+assert-text: (".sidebar-elems section ul > li:nth-child(1)", "Re-exports")
+assert-text: (".sidebar-elems section ul > li:nth-child(2)", "Modules")
+assert-text: (".sidebar-elems section ul > li:nth-child(3)", "Macros")
+assert-text: (".sidebar-elems section ul > li:nth-child(4)", "Structs")
+assert-text: (".sidebar-elems section ul > li:nth-child(5)", "Enums")
+assert-text: (".sidebar-elems section ul > li:nth-child(6)", "Traits")
+assert-text: (".sidebar-elems section ul > li:nth-child(7)", "Functions")
+assert-text: (".sidebar-elems section ul > li:nth-child(8)", "Type Definitions")
+assert-text: (".sidebar-elems section ul > li:nth-child(9)", "Unions")
+assert-text: (".sidebar-elems section ul > li:nth-child(10)", "Keywords")
 assert-text: ("#structs + .item-table .item-left > a", "Foo")
 click: "#structs + .item-table .item-left > a"
 
diff --git a/src/test/rustdoc-gui/src/test_docs/lib.rs b/src/test/rustdoc-gui/src/test_docs/lib.rs
index 348b1a65c78..b6fe9eb2565 100644
--- a/src/test/rustdoc-gui/src/test_docs/lib.rs
+++ b/src/test/rustdoc-gui/src/test_docs/lib.rs
@@ -274,3 +274,6 @@ impl EmptyTrait3 for HasEmptyTraits {}
 
 mod macros;
 pub use macros::*;
+
+#[doc(alias = "AliasForTheStdReexport")]
+pub use ::std as TheStdReexport;