Skip to content

Commit 6618af2

Browse files
authored
Rollup merge of rust-lang#61351 - GuillaumeGomez:stabilize-cfg-rustdoc, r=QuietMisdreavus
Stabilize cfg(doc) cc rust-lang#43781.
2 parents 0c987c5 + 0d7a7b5 commit 6618af2

File tree

9 files changed

+51
-19
lines changed

9 files changed

+51
-19
lines changed

src/doc/rustdoc/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
- [Documentation tests](documentation-tests.md)
88
- [Lints](lints.md)
99
- [Passes](passes.md)
10+
- [Advanced Features](advanced-features.md)
1011
- [Unstable features](unstable-features.md)
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Advanced Features
2+
3+
The features listed on this page fall outside the rest of the main categories.
4+
5+
## `#[cfg(doc)]`: Documenting platform-/feature-specific information
6+
7+
For conditional compilation, Rustdoc treats your crate the same way the compiler does: Only things
8+
from the host target are available (or from the given `--target` if present), and everything else is
9+
"filtered out" from the crate. This can cause problems if your crate is providing different things
10+
on different targets and you want your documentation to reflect all the available items you
11+
provide.
12+
13+
If you want to make sure an item is seen by Rustdoc regardless of what platform it's targeting,
14+
you can apply `#[cfg(doc)]` to it. Rustdoc sets this whenever it's building documentation, so
15+
anything that uses that flag will make it into documentation it generates. To apply this to an item
16+
with other `#[cfg]` filters on it, you can write something like `#[cfg(any(windows, doc))]`.
17+
This will preserve the item either when built normally on Windows, or when being documented
18+
anywhere.
19+
20+
Please note that this feature is not passed to doctests.
21+
22+
Example:
23+
24+
```rust
25+
/// Token struct that can only be used on Windows.
26+
#[cfg(any(windows, doc))]
27+
pub struct WindowsToken;
28+
/// Token struct that can only be used on Unix.
29+
#[cfg(any(unix, doc))]
30+
pub struct UnixToken;
31+
```
32+
33+
Here, the respective tokens can only be used by dependent crates on their respective platforms, but
34+
they will both appear in documentation.

src/librustdoc/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
250250

251251
let extern_names: Vec<String> = externs.iter().map(|(s,_)| s).cloned().collect();
252252

253-
// Add the rustdoc cfg into the doc build.
253+
// Add the doc cfg into the doc build.
254254
cfgs.push("doc".to_string());
255255

256256
let cpath = Some(input.clone());

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/libsyntax_pos/symbol.rs

-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,6 @@ symbols! {
627627
rustc_test_marker,
628628
rustc_then_this_would_need,
629629
rustc_variance,
630-
rustdoc,
631630
rustfmt,
632631
rust_eh_personality,
633632
rust_eh_unwind_resume,

src/test/ui/cfg-rustdoc.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#[cfg(doc)]
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)