Skip to content

Commit b51fa3c

Browse files
notriddletshepang
authored andcommitted
Update rustdoc-internals.md
1 parent b1b6d69 commit b51fa3c

File tree

1 file changed

+141
-87
lines changed

1 file changed

+141
-87
lines changed

src/rustdoc-internals.md

+141-87
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,61 @@ see the ["Rustdoc overview" chapter](./rustdoc.md).
77

88
## From crate to clean
99

10-
In `core.rs` are two central items: the `DocContext` struct, and the `run_core`
11-
function. The latter is where rustdoc calls out to rustc to compile a crate to
12-
the point where rustdoc can take over. The former is a state container used
13-
when crawling through a crate to gather its documentation.
10+
In `core.rs` are two central items: the `DocContext` struct, and the
11+
`run_global_ctxt` function. The latter is where rustdoc calls out to rustc to
12+
compile a crate to the point where rustdoc can take over. The former is a state
13+
container used when crawling through a crate to gather its documentation.
1414

1515
The main process of crate crawling is done in `clean/mod.rs` through several
16-
implementations of the `Clean` trait defined within. This is a conversion
17-
trait, which defines one method:
16+
functions with names that start with `clean_`. Each function accepts an `hir`
17+
or `ty` data structure, and outputs a `clean` structure used by rustdoc. For
18+
example, this function for converting lifetimes:
1819

1920
```rust,ignore
20-
pub trait Clean<T> {
21-
fn clean(&self, cx: &DocContext) -> T;
21+
fn clean_lifetime<'tcx>(lifetime: &hir::Lifetime, cx: &mut DocContext<'tcx>) -> Lifetime {
22+
let def = cx.tcx.named_bound_var(lifetime.hir_id);
23+
if let Some(
24+
rbv::ResolvedArg::EarlyBound(node_id)
25+
| rbv::ResolvedArg::LateBound(_, _, node_id)
26+
| rbv::ResolvedArg::Free(_, node_id),
27+
) = def
28+
{
29+
if let Some(lt) = cx.substs.get(&node_id).and_then(|p| p.as_lt()).cloned() {
30+
return lt;
31+
}
32+
}
33+
Lifetime(lifetime.ident.name)
2234
}
2335
```
2436

2537
`clean/mod.rs` also defines the types for the "cleaned" AST used later on to
26-
render documentation pages. Each usually accompanies an implementation of
27-
`Clean` that takes some AST or HIR type from rustc and converts it into the
38+
render documentation pages. Each usually accompanies a `clean` function
39+
that takes some AST or HIR type from rustc and converts it into the
2840
appropriate "cleaned" type. "Big" items like modules or associated items may
29-
have some extra processing in its `Clean` implementation, but for the most part
41+
have some extra processing in its `clean` function, but for the most part
3042
these impls are straightforward conversions. The "entry point" to this module
31-
is the `impl Clean<Crate> for visit_ast::RustdocVisitor`, which is called by
32-
`run_core` above.
33-
34-
You see, I actually lied a little earlier: There's another AST transformation
35-
that happens before the events in `clean/mod.rs`. In `visit_ast.rs` is the
36-
type `RustdocVisitor`, which *actually* crawls a `rustc_hir::Crate` to get the first
37-
intermediate representation, defined in `doctree.rs`. This pass is mainly to
38-
get a few intermediate wrappers around the HIR types and to process visibility
39-
and inlining. This is where `#[doc(inline)]`, `#[doc(no_inline)]`, and
40-
`#[doc(hidden)]` are processed, as well as the logic for whether a `pub use`
41-
should get the full page or a "Reexport" line in the module page.
43+
is `clean::krate`, which is called by
44+
`run_global_ctxt` above.
45+
46+
The first step in `clean::krate` is to invoke `visit_ast::RustdocVisitor` to
47+
process the module tree into an intermediate `visit_ast::Module`. This is the
48+
step that actually crawls the `rustc_hir::Crate`, normalizing various aspects
49+
of name resolution, such as:
50+
51+
* showing `#[macro_export]`-ed macros at the crate root, regardless of where
52+
they're defined
53+
* inlining public `use` exports of private items, or showing a "Reexport"
54+
line in the module page
55+
* inlining items with `#[doc(hidden)]` if the base item is hidden but the
56+
reexport is not
57+
* handling `#[doc(inline)]` and `#[doc(no_inline)]`
58+
* handling import globs and cycles, so there are no duplicates or infinite
59+
directory trees
60+
61+
After this step, `clean::krate` invokes `clean_doc_module`, which actually
62+
converts the HIR items to the cleaned AST. This is also the step where cross-
63+
crate inlining is performed, which requires converting `rustc_middle` data
64+
structures into the cleaned AST instead.
4265

4366
The other major thing that happens in `clean/mod.rs` is the collection of doc
4467
comments and `#[doc=""]` attributes into a separate field of the Attributes
@@ -48,41 +71,28 @@ easier to collect this documentation later in the process.
4871
The primary output of this process is a `clean::Crate` with a tree of Items
4972
which describe the publicly-documentable items in the target crate.
5073

51-
### Hot potato
74+
### Passes anything but a gas station
75+
76+
(alternate title: [hot potato](https://www.youtube.com/watch?v=WNFBIt5HxdY))
5277

5378
Before moving on to the next major step, a few important "passes" occur over
54-
the documentation. These do things like combine the separate "attributes" into
55-
a single string to make the document easier on the markdown parser,
56-
or drop items that are not public or deliberately hidden with `#[doc(hidden)]`.
79+
the cleaned AST. Several of these passes are lints and reports, but some of
80+
them mutate or generate new items.
81+
5782
These are all implemented in the `passes/` directory, one file per pass.
5883
By default, all of these passes are run on a crate, but the ones
5984
regarding dropping private/hidden items can be bypassed by passing
6085
`--document-private-items` to rustdoc. Note that unlike the previous set of AST
6186
transformations, the passes are run on the _cleaned_ crate.
6287

63-
(Strictly speaking, you can fine-tune the passes run and even add your own, but
64-
[we're trying to deprecate that][44136]. If you need finer-grain control over
65-
these passes, please let us know!)
66-
67-
[44136]: https://github.com/rust-lang/rust/issues/44136
68-
69-
Here is the list of passes as of <!-- date-check --> November 2022:
88+
Here is the list of passes as of <!-- date-check --> March 2023:
7089

7190
- `calculate-doc-coverage` calculates information used for the `--show-coverage`
7291
flag.
7392

74-
- `check-bare-urls` detects links that are not linkified, e.g., in Markdown such as
75-
`Go to https://example.com/.` It suggests wrapping the link with angle brackets:
76-
`Go to <https://example.com/>.` to linkify it. This is the code behind the <!--
77-
date: 2022-05 --> `rustdoc::bare_urls` lint.
78-
79-
- `check-code-block-syntax` validates syntax inside Rust code blocks
80-
(<code>```rust</code>)
81-
82-
- `check-doc-test-visibility` runs doctest visibility–related lints.
83-
84-
- `check-invalid-html-tags` detects invalid HTML (like an unclosed `<span>`)
85-
in doc comments.
93+
- `check-doc-test-visibility` runs doctest visibility–related lints. This pass
94+
runs before `strip-private`, which is why it needs to be separate from
95+
`run-lints`.
8696

8797
- `collect-intra-doc-links` resolves [intra-doc links](https://doc.rust-lang.org/nightly/rustdoc/write-documentation/linking-to-items-by-name.html).
8898

@@ -92,44 +102,66 @@ Here is the list of passes as of <!-- date-check --> November 2022:
92102

93103
- `propagate-doc-cfg` propagates `#[doc(cfg(...))]` to child items.
94104

105+
- `run-lints` runs some of rustdoc's lints, defind in `passes/lint`. This is
106+
the last pass to run.
107+
108+
- `bare_urls` detects links that are not linkified, e.g., in Markdown such as
109+
`Go to https://example.com/.` It suggests wrapping the link with angle brackets:
110+
`Go to <https://example.com/>.` to linkify it. This is the code behind the <!--
111+
date: 2022-05 --> `rustdoc::bare_urls` lint.
112+
113+
- `check_code_block_syntax` validates syntax inside Rust code blocks
114+
(<code>```rust</code>)
115+
116+
- `html_tags` detects invalid HTML (like an unclosed `<span>`)
117+
in doc comments.
118+
119+
- `strip-hidden` and `strip-private` strip all `doc(hidden)` and private items
120+
from the output. `strip-private` implies `strip-priv-imports`. Basically, the
121+
goal is to remove items that are not relevant for public documentation. This
122+
pass is skipped when `--document-hidden-items` is passed.
123+
95124
- `strip-priv-imports` strips all private import statements (`use`, `extern
96125
crate`) from a crate. This is necessary because rustdoc will handle *public*
97126
imports by either inlining the item's documentation to the module or creating
98127
a "Reexports" section with the import in it. The pass ensures that all of
99-
these imports are actually relevant to documentation.
128+
these imports are actually relevant to documentation. It is technically
129+
only run when `--document-private-items` is passed, but `strip-private`
130+
accomplishes the same thing.
100131

101-
- `strip-hidden` and `strip-private` strip all `doc(hidden)` and private items
102-
from the output. `strip-private` implies `strip-priv-imports`. Basically, the
103-
goal is to remove items that are not relevant for public documentation.
132+
- `strip-private` strips all private items from a crate which cannot be seen
133+
externally. This pass is skipped when `--document-private-items` is passed.
104134

105135
There is also a `stripper` module in `passes/`, but it is a collection of
106136
utility functions for the `strip-*` passes and is not a pass itself.
107137

108-
## From clean to crate
138+
## From clean to HTML
109139

110140
This is where the "second phase" in rustdoc begins. This phase primarily lives
111-
in the `html/` folder, and it all starts with `run()` in `html/render.rs`. This
112-
code is responsible for setting up the `Context`, `SharedContext`, and `Cache`
113-
which are used during rendering, copying out the static files which live in
114-
every rendered set of documentation (things like the fonts, CSS, and JavaScript
115-
that live in `html/static/`), creating the search index, and printing out the
116-
source code rendering, before beginning the process of rendering all the
117-
documentation for the crate.
118-
119-
Several functions implemented directly on `Context` take the `clean::Crate` and
120-
set up some state between rendering items or recursing on a module's child
121-
items. From here the "page rendering" begins, via an enormous `write!()` call
122-
in `html/layout.rs`. The parts that actually generate HTML from the items and
123-
documentation occurs within a series of `std::fmt::Display` implementations and
124-
functions that pass around a `&mut std::fmt::Formatter`. The top-level
125-
implementation that writes out the page body is the `impl<'a> fmt::Display for
126-
Item<'a>` in `html/render.rs`, which switches out to one of several `item_*`
127-
functions based on the kind of `Item` being rendered.
141+
in the `formats/` and `html/` folders, and it all starts with
142+
`formats::run_format`. This code is responsible for setting up a type that
143+
`impl FormatRenderer`, which for HTML is [`Context`].
144+
145+
This structure contains methods that get called by `run_format` to drive the
146+
doc rendering, which includes:
147+
148+
* `init` generates `static.files`, as well as search index and `src/`
149+
* `item` generates the item HTML files themselves
150+
* `after_krate` generates other global resources like `all.html`
151+
152+
In `item`, the "page rendering" occurs, via a mixture of [Askama] templates
153+
and manual `write!()` calls, starting in `html/layout.rs`. The parts that have
154+
not been converted to templates occur within a series of `std::fmt::Display`
155+
implementations and functions that pass around a `&mut std::fmt::Formatter`.
156+
157+
The parts that actually generate HTML from the items and documentation start
158+
with `print_item` defined in `html/render/print_item.rs`, which switches out
159+
to one of several `item_*` functions based on kind of `Item` being rendered.
128160

129161
Depending on what kind of rendering code you're looking for, you'll probably
130-
find it either in `html/render.rs` for major items like "what sections should I
131-
print for a struct page" or `html/format.rs` for smaller component pieces like
132-
"how should I print a where clause as part of some other item".
162+
find it either in `html/render/mod.rs` for major items like "what sections
163+
should I print for a struct page" or `html/format/mod.rs` for smaller
164+
component pieces like "how should I print a where clause as part of some other item".
133165

134166
Whenever rustdoc comes across an item that should print hand-written
135167
documentation alongside, it calls out to `html/markdown.rs` which interfaces
@@ -148,23 +180,45 @@ to us"][video])
148180

149181
[video]: https://www.youtube.com/watch?v=hOLAGYmUQV0
150182

151-
It's important to note that the AST cleaning can ask the compiler for
152-
information (crucially, `DocContext` contains a `TyCtxt`), but page rendering
153-
cannot. The `clean::Crate` created within `run_core` is passed outside the
154-
compiler context before being handed to `html::render::run`. This means that a
155-
lot of the "supplementary data" that isn't immediately available inside an
156-
item's definition, like which trait is the `Deref` trait used by the language,
157-
needs to be collected during cleaning, stored in the `DocContext`, and passed
158-
along to the `SharedContext` during HTML rendering. This manifests as a bunch
159-
of shared state, context variables, and `RefCell`s.
160-
161-
Also of note is that some items that come from "asking the compiler" don't go
162-
directly into the `DocContext` - for example, when loading items from a foreign
163-
crate, rustdoc will ask about trait implementations and generate new `Item`s
164-
for the impls based on that information. This goes directly into the returned
165-
`Crate` rather than roundabout through the `DocContext`. This way, these
166-
implementations can be collected alongside the others, right before rendering
167-
the HTML.
183+
It's important to note that rustdoc can ask the compiler for type information
184+
directly, even during HTML generation. This [didn't used to be the case], and
185+
a lot of rustdoc's architecture was designed around not doing that, but a
186+
`TyCtxt` is now passed to `formats::renderer::run_format`, which is used to
187+
run generation for both HTML and the (unstable as of March 2023) JSON format.
188+
189+
[didn't used to be the case]: https://github.com/rust-lang/rust/pull/80090
190+
191+
This change has allowed other changes to remove data from the "clean" AST
192+
that can be easily derived from `TyCtxt` queries, and we'll usually accept
193+
PRs that remove fields from "clean" (it's been soft-deprecated), but this
194+
is complicated from two other constraints that rustdoc runs under:
195+
196+
* Docs can be generated for crates that don't actually pass type checking.
197+
This is used for generating docs that cover mutually-exclusive platform
198+
configurations, such as `libstd` having a single package of docs that
199+
cover all supported operating systems. This means rustdoc has to be able
200+
to generate docs from HIR.
201+
* Docs can inline across crates. Since crate metadata doesn't contain HIR,
202+
it must be possible to generate inlined docs from the `rustc_middle` data.
203+
204+
The "clean" AST acts as a common output format for both input formats. There
205+
is also some data in clean that doesn't correspond directly to HIR, such as
206+
synthetic `impl`s for auto traits and blanket `impl`s generated by the
207+
`collect-trait-impls` pass.
208+
209+
Some additional data is stored in
210+
`html::render::context::{Context, SharedContext}`. These two types serve as
211+
ways to segregate rustdoc's data for an eventual future with multithreaded doc
212+
generation, as well as just keeping things organized:
213+
214+
* [`Context`] stores data used for generating the current page, such as its
215+
path, a list of HTML IDs that have been used (to avoid duplicate `id=""`),
216+
and the pointer to `SharedContext`.
217+
* [`SharedContext`] stores data that does not vary by page, such as the `tcx`
218+
pointer, and a list of all types.
219+
220+
[`Context`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustdoc/html/render/context/struct.Context.html
221+
[`SharedContext`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustdoc/html/render/context/struct.SharedContext.html
168222

169223
## Other tricks up its sleeve
170224

0 commit comments

Comments
 (0)