Skip to content

Commit 21ed6e6

Browse files
committed
Address review comments
1 parent a19cdc7 commit 21ed6e6

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src/appendix-code-index.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Item | Kind | Short description | Chapter |
88
----------------|----------|-----------------------------|--------------------|-------------------
99
`CodeMap` | struct | The CodeMap maps the AST nodes to their source code | [The parser] | [src/libsyntax/codemap.rs](https://github.com/rust-lang/rust/blob/master/src/libsyntax/codemap.rs)
1010
`CompileState` | struct | State that is passed to a callback at each compiler pass | [The Rustc Driver] | [src/librustc_driver/driver.rs](https://github.com/rust-lang/rust/blob/master/src/librustc_driver/driver.rs)
11+
`DocContext` | struct | A state container used by rustdoc when crawling through a crate to gather its documentation | [Rustdoc] | [src/librustdoc/core.rs](https://github.com/rust-lang/rust/blob/master/src/librustdoc/core.rs)
1112
`ast::Crate` | struct | Syntax-level representation of a parsed crate | [The parser] | [src/librustc/hir/mod.rs](https://github.com/rust-lang/rust/blob/master/src/libsyntax/ast.rs)
1213
`hir::Crate` | struct | More abstract, compiler-friendly form of a crate's AST | [The Hir] | [src/librustc/hir/mod.rs](https://github.com/rust-lang/rust/blob/master/src/librustc/hir/mod.rs)
1314
`ParseSess` | struct | This struct contains information about a parsing session | [the Parser] | [src/libsyntax/parse/mod.rs](https://github.com/rust-lang/rust/blob/master/src/libsyntax/parse/mod.rs)

src/rustdoc.md

+38-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ library. This chapter is about how it works. (A new implementation is also [unde
55

66
[under way]: https://github.com/steveklabnik/rustdoc
77

8-
Rustdoc is implemented entirely within the crate `librustdoc`. After partially compiling a crate to
9-
get its AST (technically the HIR map) from rustc, librustdoc performs two major steps past that to
10-
render a set of documentation:
8+
Rustdoc is implemented entirely within the crate [`librustdoc`][rd]. It runs
9+
the compiler up to the point where we have an internal representation of a
10+
crate (HIR) and the ability to run some queries about the types of items. [HIR]
11+
and [queries] are discussed in the linked chapters.
12+
13+
[HIR]: ./hir.html
14+
[queries]: ./query.html
15+
[rd]: https://github.com/rust-lang/rust/tree/master/src/librustdoc
16+
17+
`librustdoc` performs two major steps after that to render a set of
18+
documentation:
1119

1220
* "Clean" the AST into a form that's more suited to creating documentation (and slightly more
1321
resistant to churn in the compiler).
@@ -16,10 +24,12 @@ render a set of documentation:
1624
Naturally, there's more than just this, and those descriptions simplify out lots of details, but
1725
that's the high-level overview.
1826

19-
(Side note: this is a library crate! The `rustdoc` binary is crated using the project in
20-
`src/tools/rustdoc`. Note that literally all that does is call the `main()` that's in this crate's
27+
(Side note: `librustdoc` is a library crate! The `rustdoc` binary is crated using the project in
28+
[`src/tools/rustdoc`][bin]. Note that literally all that does is call the `main()` that's in this crate's
2129
`lib.rs`, though.)
2230

31+
[bin]: https://github.com/rust-lang/rust/tree/master/src/tools/rustdoc
32+
2333
## Cheat sheet
2434

2535
* Use `x.py build --stage 1 src/libstd src/tools/rustdoc` to make a useable rustdoc you can run on
@@ -87,13 +97,32 @@ These do things like combine the separate "attributes" into a single string and
8797
whitespace to make the document easier on the markdown parser, or drop items that are not public or
8898
deliberately hidden with `#[doc(hidden)]`. These are all implemented in the `passes/` directory, one
8999
file per pass. By default, all of these passes are run on a crate, but the ones regarding dropping
90-
private/hidden items can be bypassed by passing `--document-private-items` to rustdoc.
100+
private/hidden items can be bypassed by passing `--document-private-items` to rustdoc. Note that
101+
unlike the previous set of AST transformations, the passes happen on the _cleaned_ crate.
91102

92103
(Strictly speaking, you can fine-tune the passes run and even add your own, but [we're trying to
93104
deprecate that][44136]. If you need finer-grain control over these passes, please let us know!)
94105

95106
[44136]: https://github.com/rust-lang/rust/issues/44136
96107

108+
Here is current (as of this writing) list of passes:
109+
110+
- `collapse-docs` is necessary because each line of a doc comment is given as a
111+
separate doc attribute, and this will combine them into a single string with
112+
line breaks between each attribute.
113+
- `unindent-comments` is necessary because the convention for writing
114+
documentation is to provide a space between the `///` or `//!` marker and the
115+
text, and stripping that leading space will make the text easier to parse by
116+
the Markdown parser. (In my experience it's less necessary now that we have a
117+
Commonmark-compliant parser, since it doesn't have a problem with headers
118+
that have a space before the `##` that marks the heading.)
119+
- `strip-priv-imports` is necessary because rustdoc will handle *public*
120+
imports by either inlining the item's documentation to the module or creating
121+
a "Reexports" section with the import in it. The pass ensures that all of
122+
these imports are actually relevant to documentation.
123+
- `strip-hidden` and `strip-private` also remove items that are not relevant
124+
for public documentation.
125+
97126
## From clean to crate
98127

99128
This is where the "second phase" in rustdoc begins. This phase primarily lives in the `html/`
@@ -160,6 +189,9 @@ handing them off to the libtest test runner. One notable location in `test.rs` i
160189
`make_test`, which is where hand-written doctests get transformed into something that can be
161190
executed.
162191

192+
Some extra reading about `make_test` can be found
193+
[here](https://quietmisdreavus.net/code/2018/02/23/how-the-doctests-get-made/).
194+
163195
## Dotting i's and crossing t's
164196

165197
So that's rustdoc's code in a nutshell, but there's more things in the repo that deal with it. Since

0 commit comments

Comments
 (0)