Skip to content

Commit f74d01c

Browse files
committed
Rollup merge of rust-lang#49028 - QuietMisdreavus:the-dark-forbidden-corners-of-rustdoc, r=frewsxcv
add an "unstable features" chapter to the rustdoc book There are several rustdoc features that currently are undocumented, but also don't fit with the rest of the Rustdoc Book since they're also unstable. Some of these have corresponding feature gates and chapters in the Unstable Book, but many don't, and i wanted a place to talk about them officially. Goal: talk about everything rustdoc can do that needs nightly - [x] Feature gates (extensions to the doc attribute that can be caught by the compiler) - [x] doc(cfg) - [x] doc(masked) - [x] doc(spotlight) - [x] doc(include) - [x] Command-line flags (features that require a CLI flag to use, where the flag itself is a `-Z` command or otherwise requires `-Z unstable-options` before rustdoc will accept it) - [x] markdown-before-content/markdown-after-content - [x] playground-url - [x] display-warnings - [x] crate-version - [x] linker - [x] sort-modules-by-appearance - [x] themes/theme-checker - [x] resource-suffix - [x] `-Z force-unstable-if-unmarked` - [x] Nightly-gated functionality (features that are gated by requiring a nightly build without needing a CLI flag or a feature gate to unlock) - [x] intra-links - [x] error numbers for `compile_fail` doctests
2 parents 7c0c7ef + b996f9d commit f74d01c

File tree

2 files changed

+363
-0
lines changed

2 files changed

+363
-0
lines changed

src/doc/rustdoc/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
- [The `#[doc]` attribute](the-doc-attribute.md)
66
- [Documentation tests](documentation-tests.md)
77
- [Passes](passes.md)
8+
- [Unstable features](unstable-features.md)
+362
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,362 @@
1+
# Unstable features
2+
3+
Rustdoc is under active developement, and like the Rust compiler, some features are only available
4+
on the nightly releases. Some of these are new and need some more testing before they're able to get
5+
released to the world at large, and some of them are tied to features in the Rust compiler that are
6+
themselves unstable. Several features here require a matching `#![feature(...)]` attribute to
7+
enable, and thus are more fully documented in the [Unstable Book]. Those sections will link over
8+
there as necessary.
9+
10+
[Unstable Book]: ../unstable-book/index.html
11+
12+
## Nightly-gated functionality
13+
14+
These features just require a nightly build to operate. Unlike the other features on this page,
15+
these don't need to be "turned on" with a command-line flag or a `#![feature(...)]` attribute in
16+
your crate. This can give them some subtle fallback modes when used on a stable release, so be
17+
careful!
18+
19+
### Error numbers for `compile-fail` doctests
20+
21+
As detailed in [the chapter on documentation tests][doctest-attributes], you can add a
22+
`compile_fail` attribute to a doctest to state that the test should fail to compile. However, on
23+
nightly, you can optionally add an error number to state that a doctest should emit a specific error
24+
number:
25+
26+
[doctest-attributes]: documentation-tests.html#attributes
27+
28+
``````markdown
29+
```compile_fail,E0044
30+
extern { fn some_func<T>(x: T); }
31+
```
32+
``````
33+
34+
This is used by the error index to ensure that the samples that correspond to a given error number
35+
properly emit that error code. However, these error codes aren't guaranteed to be the only thing
36+
that a piece of code emits from version to version, so this is unlikely to be stabilized in the
37+
future.
38+
39+
Attempting to use these error numbers on stable will result in the code sample being interpreted as
40+
plain text.
41+
42+
### Linking to items by type
43+
44+
As designed in [RFC 1946], Rustdoc can parse paths to items when you use them as links. To resolve
45+
these type names, it uses the items currently in-scope, either by declaration or by `use` statement.
46+
For modules, the "active scope" depends on whether the documentation is written outside the module
47+
(as `///` comments on the `mod` statement) or inside the module (at `//!` comments inside the file
48+
or block). For all other items, it uses the enclosing module's scope.
49+
50+
[RFC 1946]: https://github.com/rust-lang/rfcs/pull/1946
51+
52+
For example, in the following code:
53+
54+
```rust
55+
/// Does the thing.
56+
pub fn do_the_thing(_: SomeType) {
57+
println!("Let's do the thing!");
58+
}
59+
60+
/// Token you use to [`do_the_thing`].
61+
pub struct SomeType;
62+
```
63+
64+
The link to ``[`do_the_thing`]`` in `SomeType`'s docs will properly link to the page for `fn
65+
do_the_thing`. Note that here, rustdoc will insert the link target for you, but manually writing the
66+
target out also works:
67+
68+
```rust
69+
pub mod some_module {
70+
/// Token you use to do the thing.
71+
pub struct SomeStruct;
72+
}
73+
74+
/// Does the thing. Requires one [`SomeStruct`] for the thing to work.
75+
///
76+
/// [`SomeStruct`]: some_module::SomeStruct
77+
pub fn do_the_thing(_: some_module::SomeStruct) {
78+
println!("Let's do the thing!");
79+
}
80+
```
81+
82+
For more details, check out [the RFC][RFC 1946], and see [the tracking issue][43466] for more
83+
information about what parts of the feature are available.
84+
85+
[43466]: https://github.com/rust-lang/rust/issues/43466
86+
87+
## Extensions to the `#[doc]` attribute
88+
89+
These features operate by extending the `#[doc]` attribute, and thus can be caught by the compiler
90+
and enabled with a `#![feature(...)]` attribute in your crate.
91+
92+
### Documenting platform-/feature-specific information
93+
94+
Because of the way Rustdoc documents a crate, the documentation it creates is specific to the target
95+
rustc compiles for. Anything that's specific to any other target is dropped via `#[cfg]` attribute
96+
processing early in the compilation process. However, Rustdoc has a trick up its sleeve to handle
97+
platform-specific code if it *does* receive it.
98+
99+
Because Rustdoc doesn't need to fully compile a crate to binary, it replaces function bodies with
100+
`loop {}` to prevent having to process more than necessary. This means that any code within a
101+
function that requires platform-specific pieces is ignored. Combined with a special attribute,
102+
`#[doc(cfg(...))]`, you can tell Rustdoc exactly which platform something is supposed to run on,
103+
ensuring that doctests are only run on the appropriate platforms.
104+
105+
The `#[doc(cfg(...))]` attribute has another effect: When Rustdoc renders documentation for that
106+
item, it will be accompanied by a banner explaining that the item is only available on certain
107+
platforms.
108+
109+
As mentioned earlier, getting the items to Rustdoc requires some extra preparation. The standard
110+
library adds a `--cfg dox` flag to every Rustdoc command, but the same thing can be accomplished by
111+
adding a feature to your Cargo.toml and adding `--feature dox` (or whatever you choose to name the
112+
feature) to your `cargo doc` calls.
113+
114+
Either way, once you create an environment for the documentation, you can start to augment your
115+
`#[cfg]` attributes to allow both the target platform *and* the documentation configuration to leave
116+
the item in. For example, `#[cfg(any(windows, feature = "dox"))]` will preserve the item either on
117+
Windows or during the documentation process. Then, adding a new attribute `#[doc(cfg(windows))]`
118+
will tell Rustdoc that the item is supposed to be used on Windows. For example:
119+
120+
```rust
121+
#![feature(doc_cfg)]
122+
123+
/// Token struct that can only be used on Windows.
124+
#[cfg(any(windows, feature = "dox"))]
125+
#[doc(cfg(windows))]
126+
pub struct WindowsToken;
127+
128+
/// Token struct that can only be used on Unix.
129+
#[cfg(any(unix, feature = "dox"))]
130+
#[doc(cfg(unix))]
131+
pub struct UnixToken;
132+
```
133+
134+
In this sample, the tokens will only appear on their respective platforms, but they will both appear
135+
in documentation.
136+
137+
`#[doc(cfg(...))]` was introduced to be used by the standard library and currently requires the
138+
`#![feature(doc_cfg)]` feature gate. For more information, see [its chapter in the Unstable
139+
Book][unstable-doc-cfg] and [its tracking issue][issue-doc-cfg].
140+
141+
[unstable-doc-cfg]: ../unstable-book/language-features/doc-cfg.html
142+
[issue-doc-cfg]: https://github.com/rust-lang/rust/issues/43781
143+
144+
### Adding your trait to the "Important Traits" dialog
145+
146+
Rustdoc keeps a list of a few traits that are believed to be "fundamental" to a given type when
147+
implemented on it. These traits are intended to be the primary interface for their types, and are
148+
often the only thing available to be documented on their types. For this reason, Rustdoc will track
149+
when a given type implements one of these traits and call special attention to it when a function
150+
returns one of these types. This is the "Important Traits" dialog, visible as a circle-i button next
151+
to the function, which, when clicked, shows the dialog.
152+
153+
In the standard library, the traits that qualify for inclusion are `Iterator`, `io::Read`, and
154+
`io::Write`. However, rather than being implemented as a hard-coded list, these traits have a
155+
special marker attribute on them: `#[doc(spotlight)]`. This means that you could apply this
156+
attribute to your own trait to include it in the "Important Traits" dialog in documentation.
157+
158+
The `#[doc(spotlight)]` attribute currently requires the `#![feature(doc_spotlight)]` feature gate.
159+
For more information, see [its chapter in the Unstable Book][unstable-spotlight] and [its tracking
160+
issue][issue-spotlight].
161+
162+
[unstable-spotlight]: ../unstable-book/language-features/doc-spotlight.html
163+
[issue-spotlight]: https://github.com/rust-lang/rust/issues/45040
164+
165+
### Exclude certain dependencies from documentation
166+
167+
The standard library uses several dependencies which, in turn, use several types and traits from the
168+
standard library. In addition, there are several compiler-internal crates that are not considered to
169+
be part of the official standard library, and thus would be a distraction to include in
170+
documentation. It's not enough to exclude their crate documentation, since information about trait
171+
implementations appears on the pages for both the type and the trait, which can be in different
172+
crates!
173+
174+
To prevent internal types from being included in documentation, the standard library adds an
175+
attribute to their `extern crate` declarations: `#[doc(masked)]`. This causes Rustdoc to "mask out"
176+
types from these crates when building lists of trait implementations.
177+
178+
The `#[doc(masked)]` attribute is intended to be used internally, and requires the
179+
`#![feature(doc_masked)]` feature gate. For more information, see [its chapter in the Unstable
180+
Book][unstable-masked] and [its tracking issue][issue-masked].
181+
182+
[unstable-masked]: ../unstable-book/language-features/doc-masked.html
183+
[issue-masked]: https://github.com/rust-lang/rust/issues/44027
184+
185+
### Include external files as API documentation
186+
187+
As designed in [RFC 1990], Rustdoc can read an external file to use as a type's documentation. This
188+
is useful if certain documentation is so long that it would break the flow of reading the source.
189+
Instead of writing it all inline, writing `#[doc(include = "sometype.md")]` (where `sometype.md` is
190+
a file adjacent to the `lib.rs` for the crate) will ask Rustdoc to instead read that file and use it
191+
as if it were written inline.
192+
193+
[RFC 1990]: https://github.com/rust-lang/rfcs/pull/1990
194+
195+
`#[doc(include = "...")]` currently requires the `#![feature(external_doc)]` feature gate. For more
196+
information, see [its chapter in the Unstable Book][unstable-include] and [its tracking
197+
issue][issue-include].
198+
199+
[unstable-include]: ../unstable-book/language-features/external-doc.html
200+
[issue-include]: https://github.com/rust-lang/rust/issues/44732
201+
202+
## Unstable command-line arguments
203+
204+
These features are enabled by passing a command-line flag to Rustdoc, but the flags in question are
205+
themselves marked as unstable. To use any of these options, pass `-Z unstable-options` as well as
206+
the flag in question to Rustdoc on the command-line. To do this from Cargo, you can either use the
207+
`RUSTDOCFLAGS` environment variable or the `cargo rustdoc` command.
208+
209+
### `--markdown-before-content`: include rendered Markdown before the content
210+
211+
Using this flag looks like this:
212+
213+
```bash
214+
$ rustdoc src/lib.rs -Z unstable-options --markdown-before-content extra.md
215+
$ rustdoc README.md -Z unstable-options --markdown-before-content extra.md
216+
```
217+
218+
Just like `--html-before-content`, this allows you to insert extra content inside the `<body>` tag
219+
but before the other content `rustdoc` would normally produce in the rendered documentation.
220+
However, instead of directly inserting the file verbatim, `rustdoc` will pass the files through a
221+
Markdown renderer before inserting the result into the file.
222+
223+
### `--markdown-after-content`: include rendered Markdown after the content
224+
225+
Using this flag looks like this:
226+
227+
```bash
228+
$ rustdoc src/lib.rs -Z unstable-options --markdown-after-content extra.md
229+
$ rustdoc README.md -Z unstable-options --markdown-after-content extra.md
230+
```
231+
232+
Just like `--html-after-content`, this allows you to insert extra content before the `</body>` tag
233+
but after the other content `rustdoc` would normally produce in the rendered documentation.
234+
However, instead of directly inserting the file verbatim, `rustdoc` will pass the files through a
235+
Markdown renderer before inserting the result into the file.
236+
237+
### `--playground-url`: control the location of the playground
238+
239+
Using this flag looks like this:
240+
241+
```bash
242+
$ rustdoc src/lib.rs -Z unstable-options --playground-url https://play.rust-lang.org/
243+
```
244+
245+
When rendering a crate's docs, this flag gives the base URL of the Rust Playground, to use for
246+
generating `Run` buttons. Unlike `--markdown-playground-url`, this argument works for standalone
247+
Markdown files *and* Rust crates. This works the same way as adding `#![doc(html_playground_url =
248+
"url")]` to your crate root, as mentioned in [the chapter about the `#[doc]`
249+
attribute][doc-playground]. Please be aware that the official Rust Playground at
250+
https://play.rust-lang.org does not have every crate available, so if your examples require your
251+
crate, make sure the playground you provide has your crate available.
252+
253+
[doc-playground]: the-doc-attribute.html#html_playground_url
254+
255+
If both `--playground-url` and `--markdown-playground-url` are present when rendering a standalone
256+
Markdown file, the URL given to `--markdown-playground-url` will take precedence. If both
257+
`--playground-url` and `#![doc(html_playground_url = "url")]` are present when rendering crate docs,
258+
the attribute will take precedence.
259+
260+
### `--crate-version`: control the crate version
261+
262+
Using this flag looks like this:
263+
264+
```bash
265+
$ rustdoc src/lib.rs -Z unstable-options --crate-version 1.3.37
266+
```
267+
268+
When `rustdoc` receives this flag, it will print an extra "Version (version)" into the sidebar of
269+
the crate root's docs. You can use this flag to differentiate between different versions of your
270+
library's documentation.
271+
272+
### `--linker`: control the linker used for documentation tests
273+
274+
Using this flag looks like this:
275+
276+
```bash
277+
$ rustdoc --test src/lib.rs -Z unstable-options --linker foo
278+
$ rustdoc --test README.md -Z unstable-options --linker foo
279+
```
280+
281+
When `rustdoc` runs your documentation tests, it needs to compile and link the tests as executables
282+
before running them. This flag can be used to change the linker used on these executables. It's
283+
equivalent to passing `-C linker=foo` to `rustc`.
284+
285+
### `--sort-modules-by-appearance`: control how items on module pages are sorted
286+
287+
Using this flag looks like this:
288+
289+
```bash
290+
$ rustdoc src/lib.rs -Z unstable-options --sort-modules-by-appearance
291+
```
292+
293+
Ordinarily, when `rustdoc` prints items in module pages, it will sort them alphabetically (taking
294+
some consideration for their stability, and names that end in a number). Giving this flag to
295+
`rustdoc` will disable this sorting and instead make it print the items in the order they appear in
296+
the source.
297+
298+
### `--themes`: provide additional themes
299+
300+
Using this flag looks like this:
301+
302+
```bash
303+
$ rustdoc src/lib.rs -Z unstable-options --themes theme.css
304+
```
305+
306+
Giving this flag to `rustdoc` will make it copy your theme into the generated crate docs and enable
307+
it in the theme selector. Note that `rustdoc` will reject your theme file if it doesn't style
308+
everything the "main" theme does. See `--theme-checker` below for details.
309+
310+
### `--theme-checker`: verify theme CSS for validity
311+
312+
Using this flag looks like this:
313+
314+
```bash
315+
$ rustdoc -Z unstable-options --theme-checker theme.css
316+
```
317+
318+
Before including your theme in crate docs, `rustdoc` will compare all the CSS rules it contains
319+
against the "main" theme included by default. Using this flag will allow you to see which rules are
320+
missing if `rustdoc` rejects your theme.
321+
322+
### `--resource-suffix`: modifying the name of CSS/JavaScript in crate docs
323+
324+
Using this flag looks like this:
325+
326+
```bash
327+
$ rustdoc src/lib.rs -Z unstable-options --resource-suffix suf
328+
```
329+
330+
When rendering docs, `rustdoc` creates several CSS and JavaScript files as part of the output. Since
331+
all these files are linked from every page, changing where they are can be cumbersome if you need to
332+
specially cache them. This flag will rename all these files in the output to include the suffix in
333+
the filename. For example, `main.css` would become `main-suf.css` with the above command.
334+
335+
### `--display-warnings`: display warnings when documenting or running documentation tests
336+
337+
Using this flag looks like this:
338+
339+
```bash
340+
$ rustdoc src/lib.rs -Z unstable-options --display-warnings
341+
$ rustdoc --test src/lib.rs -Z unstable-options --display-warnings
342+
```
343+
344+
The intent behind this flag is to allow the user to see warnings that occur within their library or
345+
their documentation tests, which are usually suppressed. However, [due to a
346+
bug][issue-display-warnings], this flag doesn't 100% work as intended. See the linked issue for
347+
details.
348+
349+
[issue-display-warnings]: https://github.com/rust-lang/rust/issues/41574
350+
351+
### `-Z force-unstable-if-unmarked`
352+
353+
Using this flag looks like this:
354+
355+
```bash
356+
$ rustdoc src/lib.rs -Z force-unstable-if-unmarked
357+
```
358+
359+
This is an internal flag intended for the standard library and compiler that applies an
360+
`#[unstable]` attribute to any dependent crate that doesn't have another stability attribute. This
361+
allows `rustdoc` to be able to generate documentation for the compiler crates and the standard
362+
library, as an equivalent command-line argument is provided to `rustc` when building those crates.

0 commit comments

Comments
 (0)