diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index f20084497671f..d37c7b6233761 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -261,6 +261,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { masked => doc_masked spotlight => doc_spotlight keyword => doc_keyword + search_hidden => doc_search_hidden ); } } diff --git a/compiler/rustc_feature/src/active.rs b/compiler/rustc_feature/src/active.rs index ad926a810e6bf..b3fc4df63a6b4 100644 --- a/compiler/rustc_feature/src/active.rs +++ b/compiler/rustc_feature/src/active.rs @@ -610,6 +610,9 @@ declare_features! ( /// Allows unsized fn parameters. (active, unsized_fn_params, "1.49.0", Some(48055), None), + /// Allows `#[doc(search_hidden)]`. + (active, doc_search_hidden, "1.49.0", Some(67368), None), + // ------------------------------------------------------------------------- // feature-group-end: actual feature gates // ------------------------------------------------------------------------- diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 1a6c45b6c80d2..22f1024e1c44b 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -447,6 +447,7 @@ symbols! { doc_cfg, doc_keyword, doc_masked, + doc_search_hidden, doc_spotlight, doctest, document_private_items, @@ -964,6 +965,7 @@ symbols! { sanitizer_runtime, saturating_add, saturating_sub, + search_hidden, self_in_typedefs, self_struct_ctor, semitransparent, diff --git a/src/doc/rustdoc/src/unstable-features.md b/src/doc/rustdoc/src/unstable-features.md index b43070510413a..4a03e0d4d7be9 100644 --- a/src/doc/rustdoc/src/unstable-features.md +++ b/src/doc/rustdoc/src/unstable-features.md @@ -422,3 +422,19 @@ Calculating code examples follows these rules: * static * typedef 2. If one of the previously listed items has a code example, then it'll be counted. + +### Hide an item from the doc search + +If you want an item to be visible from the documentation but to not show up when using +the documentation search, you can use the `#[doc(search_hidden)]` attribute. For +example: + +```rust +#![feature(doc_search_hidden)] + +#[doc(search_hidden)] +pub struct Bar; +``` + +If you look for "Bar" in the search, it won't appear. However, the type will appear in +the documentation like it normally should. diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 32b3f69ecd4f0..212f7f30693c7 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -718,6 +718,10 @@ impl Attributes { .filter(|v| !v.is_empty()) .collect::>() } + + pub fn is_search_hidden(&self) -> bool { + self.has_doc_flag(sym::search_hidden) + } } impl PartialEq for Attributes { diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index b99321e8484c9..09a00a153d3e2 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -308,7 +308,7 @@ impl DocFolder for Cache { // A crate has a module at its root, containing all items, // which should not be indexed. The crate-item itself is // inserted later on when serializing the search-index. - if item.def_id.index != CRATE_DEF_INDEX { + if item.def_id.index != CRATE_DEF_INDEX && !item.attrs.is_search_hidden() { self.search_index.push(IndexItem { ty: item.type_(), name: s.to_string(), diff --git a/src/librustdoc/html/render/cache.rs b/src/librustdoc/html/render/cache.rs index cf785d362cd11..2bf771146070b 100644 --- a/src/librustdoc/html/render/cache.rs +++ b/src/librustdoc/html/render/cache.rs @@ -73,6 +73,9 @@ pub fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String { // Attach all orphan items to the type's definition if the type // has since been learned. for &(did, ref item) in orphan_impl_items { + if item.attrs.is_search_hidden() { + continue; + } if let Some(&(ref fqp, _)) = paths.get(&did) { search_index.push(IndexItem { ty: item.type_(), diff --git a/src/test/rustdoc-js/search_hidden.js b/src/test/rustdoc-js/search_hidden.js new file mode 100644 index 0000000000000..c22ee0854b7c3 --- /dev/null +++ b/src/test/rustdoc-js/search_hidden.js @@ -0,0 +1,9 @@ +// exact-check + +const QUERY = 'Bar'; + +const EXPECTED = { + 'others': [ + { 'path': 'search_hidden', 'name': 'Bar2' }, + ], +}; diff --git a/src/test/rustdoc-js/search_hidden.rs b/src/test/rustdoc-js/search_hidden.rs new file mode 100644 index 0000000000000..19801513cc1d9 --- /dev/null +++ b/src/test/rustdoc-js/search_hidden.rs @@ -0,0 +1,8 @@ +#![feature(doc_search_hidden)] + +#[doc(search_hidden)] +/// Foo +pub struct Bar; + +/// Bar +pub struct Bar2; diff --git a/src/test/ui/feature-gates/feature-gate-doc_search_hidden.rs b/src/test/ui/feature-gates/feature-gate-doc_search_hidden.rs new file mode 100644 index 0000000000000..7f8975ad70641 --- /dev/null +++ b/src/test/ui/feature-gates/feature-gate-doc_search_hidden.rs @@ -0,0 +1,4 @@ +#[doc(search_hidden)] //~ ERROR: `#[doc(search_hidden)]` is experimental +pub struct Bar; + +fn main() {} diff --git a/src/test/ui/feature-gates/feature-gate-doc_search_hidden.stderr b/src/test/ui/feature-gates/feature-gate-doc_search_hidden.stderr new file mode 100644 index 0000000000000..d2f757a53ac22 --- /dev/null +++ b/src/test/ui/feature-gates/feature-gate-doc_search_hidden.stderr @@ -0,0 +1,12 @@ +error[E0658]: `#[doc(search_hidden)]` is experimental + --> $DIR/feature-gate-doc_search_hidden.rs:1:1 + | +LL | #[doc(search_hidden)] + | ^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #67368 for more information + = help: add `#![feature(doc_search_hidden)]` to the crate attributes to enable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0658`.