Skip to content

Commit 086c8d3

Browse files
Stabilize cfg rustdoc
1 parent f1b882b commit 086c8d3

File tree

6 files changed

+46
-17
lines changed

6 files changed

+46
-17
lines changed

src/doc/rustdoc/src/the-doc-attribute.md

+31
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,34 @@ the `strip-hidden` pass is removed.
214214
Since primitive types are defined in the compiler, there's no place to attach documentation
215215
attributes. This attribute is used by the standard library to provide a way to generate
216216
documentation for primitive types.
217+
218+
## `#[cfg(rustdoc)]`: Documenting platform-/feature-specific information
219+
220+
For conditional compilation, Rustdoc treats your crate the same way the compiler does: Only things
221+
from the host target are available (or from the given `--target` if present), and everything else is
222+
"filtered out" from the crate. This can cause problems if your crate is providing different things
223+
on different targets and you want your documentation to reflect all the available items you
224+
provide.
225+
226+
If you want to make sure an item is seen by Rustdoc regardless of what platform it's targeting,
227+
you can apply `#[cfg(rustdoc)]` to it. Rustdoc sets this whenever it's building documentation, so
228+
anything that uses that flag will make it into documentation it generates. To apply this to an item
229+
with other `#[cfg]` filters on it, you can write something like `#[cfg(any(windows, rustdoc))]`.
230+
This will preserve the item either when built normally on Windows, or when being documented
231+
anywhere.
232+
233+
Please note that this feature won't be passed when building doctests.
234+
235+
Example:
236+
237+
```rust
238+
/// Token struct that can only be used on Windows.
239+
#[cfg(any(windows, rustdoc))]
240+
pub struct WindowsToken;
241+
/// Token struct that can only be used on Unix.
242+
#[cfg(any(unix, rustdoc))]
243+
pub struct UnixToken;
244+
```
245+
246+
Here, the respective tokens can only be used by dependent crates on their respective platforms, but
247+
they will both appear in documentation.

src/libsyntax/feature_gate/builtin_attrs.rs

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ const GATED_CFGS: &[(Symbol, Symbol, GateFn)] = &[
3030
(sym::target_thread_local, sym::cfg_target_thread_local, cfg_fn!(cfg_target_thread_local)),
3131
(sym::target_has_atomic, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),
3232
(sym::target_has_atomic_load_store, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),
33-
(sym::doc, sym::doc_cfg, cfg_fn!(doc_cfg)),
3433
];
3534

3635
#[derive(Debug)]

src/test/ui/cfg-rustdoc.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#[cfg(rustdoc)]
2+
pub struct Foo;
3+
4+
fn main() {
5+
let f = Foo; //~ ERROR
6+
}

src/test/ui/cfg-rustdoc.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0425]: cannot find value `Foo` in this scope
2+
--> $DIR/cfg-rustdoc.rs:5:13
3+
|
4+
LL | let f = Foo;
5+
| ^^^ not found in this scope
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0425`.

src/test/ui/feature-gates/feature-gate-doc_cfg-cfg-rustdoc.rs

-4
This file was deleted.

src/test/ui/feature-gates/feature-gate-doc_cfg-cfg-rustdoc.stderr

-12
This file was deleted.

0 commit comments

Comments
 (0)