Skip to content

Commit 7eaade0

Browse files
Update syntax information for doc(auto_cfg(hide/show())) in the rustdoc book
1 parent 417dbc1 commit 7eaade0

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

src/doc/rustdoc/src/unstable-features.md

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -850,12 +850,49 @@ pub mod futures {
850850

851851
Then, the `unix` cfg will never be displayed into the documentation.
852852

853-
Rustdoc currently hides `doc` and `doctest` attributes by default and reserves the right to change the list of "hidden by default" attributes.
853+
The syntax of `hide` is as follows: you can list as many `cfg` name as you want:
854+
855+
```rust,ignore (nightly)
856+
#[doc(auto_cfg(hide(feature, target_os)))]
857+
```
858+
859+
With the above example, it means that `#[cfg(feature)]` and `#[cfg(target_os)]` won't be displayed in the docs. However, `#[cfg(target_os = "linux)]` or `#[cfg(feature = "something")]` will be displayed because only the key without values was marked as hidden. if you want to hide some values, you can do:
860+
861+
```rust,ignore (nightly)
862+
#[doc(auto_cfg(hide(feature, target_os, values("something", "linux"))))]
863+
```
864+
865+
In this case, `#[cfg(feature = "linux")]`, `#[cfg(feature = "something")]`, `#[cfg(target_os = "something")]` and `#[cfg(target_os = "linux")]` will be hidden. All listed keys will be impacted by `values()`. You can split them by having two `hide`:
866+
867+
```rust,ignore (nightly)
868+
#[doc(auto_cfg(
869+
hide(feature, values("something")),
870+
hide(target_os, values("linux"))))]
871+
```
872+
873+
Now, only `#[cfg(feature = "something")]` and `#[cfg(target_os = "linux")]` will be hidden. If you want to hide all values of a key, you can use `any()`:
874+
875+
```rust,ignore (nightly)
876+
#[doc(auto_cfg(hide(feature, values(any()))))]
877+
```
878+
879+
To be noted, if you have `values()`, it will be considered the same as `values(any())`. If you want to hide when there is no value (while also hiding values at the same time), you can use `none()`:
880+
881+
```rust,ignore (nightly)
882+
#[doc(auto_cfg(hide(feature, values("something", none()))))]
883+
```
884+
885+
If the previous example, both `#[cfg(feature)]` and `#[cfg(feature = "something")]` will be hidden.
886+
887+
Rustdoc currently hides `test`, `doc` and `doctest` attributes by default and reserves the right to change the list of "hidden by default" attributes.
854888

855889
The attribute accepts only a list of identifiers or key/value items. So you can write:
856890

857891
```rust,ignore (nightly)
858-
#[doc(auto_cfg(hide(unix, doctest, feature = "something")))]
892+
#[doc(auto_cfg(
893+
hide(unix, doctest),
894+
hide(feature, values("something")),
895+
))]
859896
#[doc(auto_cfg(hide()))]
860897
```
861898

@@ -865,7 +902,7 @@ But you cannot write:
865902
#[doc(auto_cfg(hide(not(unix))))]
866903
```
867904

868-
So if we use `doc(auto_cfg(hide(unix)))`, it means it will hide all mentions of `unix`:
905+
So if we use `doc(auto_cfg(hide(unix)))`, it means it will hide all mentions of `unix` without a value:
869906

870907
```rust,ignore (nightly)
871908
#[cfg(unix)] // nothing displayed
@@ -919,6 +956,8 @@ The reason behind this is that `doc(auto_cfg = ...)` enables or disables the fea
919956
This attribute does the opposite of `#[doc(auto_cfg(hide(...)))]`: if you used `#[doc(auto_cfg(hide(...)))]` and want to revert its effect on an item and its descendants, you can use `#[doc(auto_cfg(show(...)))]`.
920957
It only applies to `#[doc(auto_cfg = true)]`, not to `#[doc(cfg(...))]`.
921958

959+
It follows the same syntax rules as for `#[doc(auto_cfg(hide(...)))]`.
960+
922961
For example:
923962

924963
```rust,ignore (nightly)
@@ -936,7 +975,10 @@ pub mod futures {
936975
The attribute accepts only a list of identifiers or key/value items. So you can write:
937976

938977
```rust,ignore (nightly)
939-
#[doc(auto_cfg(show(unix, doctest, feature = "something")))]
978+
#[doc(auto_cfg(
979+
show(unix, doctest),
980+
show(feature, values("something")),
981+
))]
940982
#[doc(auto_cfg(show()))]
941983
```
942984

tests/rustdoc-ui/doc-cfg-4.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Checks that you cannot have any item inside `any()` and
1+
// Checks that you cannot have any item inside `any()` and `none()`.
22

33
#![deny(invalid_doc_attributes)]
44
#![feature(doc_cfg)]

0 commit comments

Comments
 (0)