Skip to content

Commit dc5a000

Browse files
committed
Revamp rustdoc docs about documentation using cfg
- Move `cfg(doc)` out of `unstable-features`. It's not unstable. - Remove outdated reference to `everybody_loops`. - Improve wording in various places - Give an example of code this allows (and does not allow) - Link to `cfg(doc)` in `doc(cfg)` documentation. Since one is stable and the other is not, don't combine them. - Cleanup wording for `doc(cfg)` - Incorporate changes from rust-lang#76849 - Mention that `doc(cfg)` is also for features
1 parent 7820135 commit dc5a000

File tree

2 files changed

+50
-21
lines changed

2 files changed

+50
-21
lines changed

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

+36-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The features listed on this page fall outside the rest of the main categories.
44

5-
## `#[cfg(doc)]`: Documenting platform-/feature-specific information
5+
## `#[cfg(doc)]`: Documenting platform-specific or feature-specific information
66

77
For conditional compilation, Rustdoc treats your crate the same way the compiler does. Only things
88
from the host target are available (or from the given `--target` if present), and everything else is
@@ -17,7 +17,7 @@ with other `#[cfg]` filters on it, you can write something like `#[cfg(any(windo
1717
This will preserve the item either when built normally on Windows, or when being documented
1818
anywhere.
1919

20-
Please note that this feature is not passed to doctests.
20+
Please note that this `cfg` is not passed to doctests.
2121

2222
Example:
2323

@@ -33,6 +33,40 @@ pub struct UnixToken;
3333
Here, the respective tokens can only be used by dependent crates on their respective platforms, but
3434
they will both appear in documentation.
3535

36+
### Interactions between platform-specific docs
37+
38+
Rustdoc does not have a magic way to compile documentation 'as-if' you'd run it once for each
39+
platform (such a magic wand has been called the ['holy grail of rustdoc'][#1998]). Instead,
40+
it sees *all* of your code at once, the same way the Rust compiler would if you passed it
41+
`--cfg doc`. However, Rustdoc has a trick up its sleeve to handle platform-specific code if it
42+
*does* receive it.
43+
44+
To document your crate, Rustdoc only needs to know the public signature of your functions.
45+
In particular, it doesn't have to know how any of your functions are implemented, so it ignores
46+
all type errors and name resolution errors with function bodies. Note that this does *not*
47+
work for anything outside a function body: since Rustdoc documents your types, it has to
48+
know what those types are! For example, this code will work regardless of the platform:
49+
50+
<!-- `ignore` because doc-tests are run with `rustc`, not `rustdoc` -->
51+
```ignore
52+
pub fn f() {
53+
use std::os::windows::ffi::OsStrExt;
54+
}
55+
```
56+
57+
but this will not, because the unknown type is part of the function signature:
58+
59+
```ignore
60+
pub fn f() -> std::os::windows::ffi::EncodeWide<'static> {
61+
unimplemented!()
62+
}
63+
```
64+
65+
For a more realistic example of code this allows, see [the rustdoc test suite][realistic-async].
66+
67+
[#1998]: https://github.com/rust-lang/rust/issues/1998
68+
[realistic-async]: https://github.com/rust-lang/rust/blob/b146000e910ccd60bdcde89363cb6aa14ecc0d95/src/test/rustdoc-ui/error-in-impl-trait/realistic-async.rs
69+
3670
## Add aliases for an item in documentation search
3771

3872
This feature allows you to add alias(es) to an item when using the `rustdoc` search through the

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

+14-19
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,16 @@ plain text.
4343
These features operate by extending the `#[doc]` attribute, and thus can be caught by the compiler
4444
and enabled with a `#![feature(...)]` attribute in your crate.
4545

46-
### Documenting platform-/feature-specific information
46+
### `#[doc(cfg)]`: Recording what platforms or features are required for code to be present
4747

48-
Because of the way Rustdoc documents a crate, the documentation it creates is specific to the target
49-
rustc compiles for. Anything that's specific to any other target is dropped via `#[cfg]` attribute
50-
processing early in the compilation process. However, Rustdoc has a trick up its sleeve to handle
51-
platform-specific code if it *does* receive it.
48+
You can use `#[doc(cfg(...))]` to tell Rustdoc exactly which platform items appear on.
49+
This has two effects:
5250

53-
Because Rustdoc doesn't need to fully compile a crate to binary, it replaces function bodies with
54-
`loop {}` to prevent having to process more than necessary. This means that any code within a
55-
function that requires platform-specific pieces is ignored. Combined with a special attribute,
56-
`#[doc(cfg(...))]`, you can tell Rustdoc exactly which platform something is supposed to run on,
57-
ensuring that doctests are only run on the appropriate platforms.
58-
59-
The `#[doc(cfg(...))]` attribute has another effect: When Rustdoc renders documentation for that
60-
item, it will be accompanied by a banner explaining that the item is only available on certain
61-
platforms.
62-
63-
For Rustdoc to document an item, it needs to see it, regardless of what platform it's currently
64-
running on. To aid this, Rustdoc sets the flag `#[cfg(doc)]` when running on your crate.
65-
Combining this with the target platform of a given item allows it to appear when building your crate
66-
normally on that platform, as well as when building documentation anywhere.
51+
1. doctests will only run on the appropriate platforms, and
52+
2. When Rustdoc renders documentation for that item, it will be accompanied by a banner explaining
53+
that the item is only available on certain platforms.
6754

55+
`#[doc(cfg)]` is intended to be used alongside [`#[cfg(doc)]`][cfg-doc].
6856
For example, `#[cfg(any(windows, doc))]` will preserve the item either on Windows or during the
6957
documentation process. Then, adding a new attribute `#[doc(cfg(windows))]` will tell Rustdoc that
7058
the item is supposed to be used on Windows. For example:
@@ -81,6 +69,12 @@ pub struct WindowsToken;
8169
#[cfg(any(unix, doc))]
8270
#[doc(cfg(unix))]
8371
pub struct UnixToken;
72+
73+
/// Token struct that is only available with the `serde` feature
74+
#[cfg(feature = "serde")]
75+
#[doc(cfg(feature = "serde"))]
76+
#[derive(serde::Deserialize)]
77+
pub struct SerdeToken;
8478
```
8579

8680
In this sample, the tokens will only appear on their respective platforms, but they will both appear
@@ -90,6 +84,7 @@ in documentation.
9084
`#![feature(doc_cfg)]` feature gate. For more information, see [its chapter in the Unstable
9185
Book][unstable-doc-cfg] and [its tracking issue][issue-doc-cfg].
9286

87+
[cfg-doc]: ./advanced-features.md
9388
[unstable-doc-cfg]: ../unstable-book/language-features/doc-cfg.html
9489
[issue-doc-cfg]: https://github.com/rust-lang/rust/issues/43781
9590

0 commit comments

Comments
 (0)