Skip to content

Add #[doc(search_hidden)] attribute #78525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
// -------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ symbols! {
doc_cfg,
doc_keyword,
doc_masked,
doc_search_hidden,
doc_spotlight,
doctest,
document_private_items,
Expand Down Expand Up @@ -964,6 +965,7 @@ symbols! {
sanitizer_runtime,
saturating_add,
saturating_sub,
search_hidden,
self_in_typedefs,
self_struct_ctor,
semitransparent,
Expand Down
16 changes: 16 additions & 0 deletions src/doc/rustdoc/src/unstable-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
4 changes: 4 additions & 0 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,10 @@ impl Attributes {
.filter(|v| !v.is_empty())
.collect::<FxHashSet<_>>()
}

pub fn is_search_hidden(&self) -> bool {
self.has_doc_flag(sym::search_hidden)
}
}

impl PartialEq for Attributes {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions src/librustdoc/html/render/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_(),
Expand Down
9 changes: 9 additions & 0 deletions src/test/rustdoc-js/search_hidden.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// exact-check

const QUERY = 'Bar';

const EXPECTED = {
'others': [
{ 'path': 'search_hidden', 'name': 'Bar2' },
],
};
8 changes: 8 additions & 0 deletions src/test/rustdoc-js/search_hidden.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(doc_search_hidden)]

#[doc(search_hidden)]
/// Foo
pub struct Bar;
Comment on lines +3 to +5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test that re-exports are still shown?

Suggested change
#[doc(search_hidden)]
/// Foo
pub struct Bar;
#[doc(search_hidden)]
/// Foo
pub struct Bar;
mod inner {
pub use Bar;
}

and then update search_hidden.js.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!


/// Bar
pub struct Bar2;
4 changes: 4 additions & 0 deletions src/test/ui/feature-gates/feature-gate-doc_search_hidden.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[doc(search_hidden)] //~ ERROR: `#[doc(search_hidden)]` is experimental
pub struct Bar;

fn main() {}
12 changes: 12 additions & 0 deletions src/test/ui/feature-gates/feature-gate-doc_search_hidden.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/rust-lang/rust/issues/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`.