Skip to content

Commit 38e56c5

Browse files
TbkhiNoratrieb
Tbkhi
authored andcommitted
Update compiler-src.md
Various link additions and minor edits for clarity. revisions fix line length adding links minor edits
1 parent 5317005 commit 38e56c5

File tree

1 file changed

+92
-79
lines changed

1 file changed

+92
-79
lines changed

src/compiler-src.md

+92-79
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,64 @@
22

33
<!-- toc -->
44

5-
Now that we have [seen what the compiler does](./overview.md), let's take a
6-
look at the structure of the [`rust-lang/rust`] repository, where the rustc
7-
source code lives.
5+
Now that we have [seen what the compiler does][orgch],
6+
let's take a look at the structure of the [`rust-lang/rust`] repository,
7+
where the rustc source code lives.
88

99
[`rust-lang/rust`]: https://github.com/rust-lang/rust
1010

11-
> You may find it helpful to read the ["Overview of the compiler"](./overview.md)
11+
> You may find it helpful to read the ["Overview of the compiler"][orgch]
1212
> chapter, which introduces how the compiler works, before this one.
1313
14+
[orgch]: ./overview.md
15+
1416
## Workspace structure
1517

16-
The `rust-lang/rust` repository consists of a single large cargo workspace
17-
containing the compiler, the standard libraries (`core`, `alloc`, `std`,
18-
`proc_macro`, etc), and `rustdoc`, along with the build system and a bunch of
19-
tools and submodules for building a full Rust distribution.
18+
The [`rust-lang/rust`] repository consists of a single large cargo workspace
19+
containing the compiler, the standard libraries ([`core`], [`alloc`],[ `std`],
20+
[`proc_macro`], [`etc`]), and [`rustdoc`], along with the build system and a
21+
bunch of tools and submodules for building a full Rust distribution.
2022

2123
The repository consists of three main directories:
2224

23-
- `compiler/` contains the source code for `rustc`. It consists of many crates
25+
- [`compiler/`] contains the source code for `rustc`. It consists of many crates
2426
that together make up the compiler.
2527

26-
- `library/` contains the standard libraries (`core`, `alloc`, `std`,
27-
`proc_macro`, `test`), as well as the Rust runtime (`backtrace`, `rtstartup`,
28-
`lang_start`).
28+
- [`library/`] contains the standard libraries ([`core`], [`alloc`], [`std`],
29+
[`proc_macro`], [`test`]), as well as the Rust runtime ([`backtrace`], [`rtstartup`],
30+
[`lang_start`]).
2931

30-
- `tests/` contains the compiler tests.
32+
- [`tests/`] contains the compiler tests.
3133

32-
- `src/` contains the source code for rustdoc, clippy, cargo, the build system,
34+
- [`src/`] contains the source code for [`rustdoc`], [`clippy`], [`cargo`], the build system,
3335
language docs, etc.
3436

37+
[`alloc`]: https://github.com/rust-lang/rust/tree/master/library/alloc
38+
[`backtrace`]: https://github.com/rust-lang/backtrace-rs/
39+
[`cargo`]: https://github.com/rust-lang/cargo
40+
[`clippy`]: https://github.com/rust-lang/rust/tree/master/src/tools/clippy
41+
[`compiler/`]: https://github.com/rust-lang/rust/tree/master/compiler
42+
[`core`]: https://github.com/rust-lang/rust/tree/master/library/core
43+
[`etc`]: https://github.com/rust-lang/rust/tree/master/src/etc
44+
[`lang_start`]: https://github.com/rust-lang/rust/blob/master/library/std/src/rt.rs
45+
[`library/`]: https://github.com/rust-lang/rust/tree/master/library
46+
[`proc_macro`]: https://github.com/rust-lang/rust/tree/master/library/proc_macro
47+
[`rtstartup`]: https://github.com/rust-lang/rust/tree/master/library/rtstartup
48+
[`rust-lang/rust`]: https://github.com/rust-lang/rust
49+
[`rustdoc`]: https://github.com/rust-lang/rust/tree/master/src/tools/rustdoc
50+
[`src/`]: https://github.com/rust-lang/rust/tree/master/src
51+
[`std`]: https://github.com/rust-lang/rust/tree/master/library/std
52+
[`test`]: https://github.com/rust-lang/rust/tree/master/library/test
53+
[`tests/`]: https://github.com/rust-lang/rust/tree/master/tests
54+
3555
## Compiler
3656

37-
The compiler is implemented in the various `compiler/` crates.
38-
The `compiler/` crates all have names starting with `rustc_*`. These are a
57+
The compiler is implemented in the various [`compiler/`] crates.
58+
The [`compiler/`] crates all have names starting with `rustc_*`. These are a
3959
collection of around 50 interdependent crates ranging in size from tiny to
4060
huge. There is also the `rustc` crate which is the actual binary (i.e. the
4161
`main` function); it doesn't actually do anything besides calling the
42-
`rustc_driver` crate, which drives the various parts of compilation in other
62+
[`rustc_driver`] crate, which drives the various parts of compilation in other
4363
crates.
4464

4565
The dependency structure of these crates is complex, but roughly it is
@@ -58,16 +78,16 @@ something like this:
5878
[`Span`]), or error reporting: [`rustc_data_structures`],
5979
[`rustc_span`], [`rustc_errors`], etc.
6080

61-
[main]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/fn.main.html
81+
[`rustc_data_structures`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_data_structures/index.html
6282
[`rustc_driver`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/index.html
83+
[`rustc_errors`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/index.html
6384
[`rustc_interface`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/index.html
6485
[`rustc_middle`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/index.html
65-
[`rustc_data_structures`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_data_structures/index.html
6686
[`rustc_span`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/index.html
6787
[`Span`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/struct.Span.html
68-
[`rustc_errors`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/index.html
88+
[main]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/fn.main.html
6989

70-
You can see the exact dependencies by reading the `Cargo.toml` for the various
90+
You can see the exact dependencies by reading the [`Cargo.toml`] for the various
7191
crates, just like a normal Rust crate.
7292

7393
One final thing: [`src/llvm-project`] is a submodule for our fork of LLVM.
@@ -78,117 +98,110 @@ compiler can interface with it.
7898
Most of this book is about the compiler, so we won't have any further
7999
explanation of these crates here.
80100

81-
[`src/llvm-project`]: https://github.com/rust-lang/rust/tree/master/src/
82101
[`compiler/rustc_llvm`]: https://github.com/rust-lang/rust/tree/master/compiler/rustc_llvm
102+
[`src/llvm-project`]: https://github.com/rust-lang/rust/tree/master/src/
103+
[`Cargo.toml`]: https://github.com/rust-lang/rust/blob/master/Cargo.toml
83104

84105
### Big picture
85106

86-
The dependency structure is influenced by two main factors:
107+
The dependency structure of the compiler is influenced by two main factors:
87108

88109
1. Organization. The compiler is a _huge_ codebase; it would be an impossibly
89110
large crate. In part, the dependency structure reflects the code structure
90111
of the compiler.
91-
2. Compile time. By breaking the compiler into multiple crates, we can take
112+
2. Compile-time. By breaking the compiler into multiple crates, we can take
92113
better advantage of incremental/parallel compilation using cargo. In
93114
particular, we try to have as few dependencies between crates as possible so
94115
that we don't have to rebuild as many crates if you change one.
95116

96117
At the very bottom of the dependency tree are a handful of crates that are used
97118
by the whole compiler (e.g. [`rustc_span`]). The very early parts of the
98-
compilation process (e.g. parsing and the AST) depend on only these.
99-
100-
After the AST is constructed and other early analysis is done, the compiler's [query system][query]
101-
gets set up. The query system is set up in a clever way using function
102-
pointers. This allows us to break dependencies between crates, allowing more
103-
parallel compilation.
104-
The query system is defined in [`rustc_middle`], so nearly all
105-
subsequent parts of the compiler depend on this crate. It is a really large
106-
crate, leading to long compile times. Some efforts have been made to move stuff
107-
out of it with limited success. Another unfortunate side effect is that sometimes
108-
related functionality gets scattered across different crates. For example,
109-
linting functionality is scattered across earlier parts of the crate,
110-
[`rustc_lint`], [`rustc_middle`], and other places.
111-
119+
compilation process (e.g. [parsing and the Abstract Syntax Tree (`AST`)][parser])
120+
depend on only these.
121+
122+
After the [`AST`][parser] is constructed and other early analysis is done, the
123+
compiler's [query system][query] gets set up. The query system is set up in a
124+
clever way using function pointers. This allows us to break dependencies
125+
between crates, allowing more parallel compilation. The query system is defined
126+
in [`rustc_middle`], so nearly all subsequent parts of the compiler depend on
127+
this crate. It is a really large crate, leading to long compile times. Some
128+
efforts have been made to move stuff out of it with varying success. Another
129+
side-effect is that sometimes related functionality gets scattered across
130+
different crates. For example, linting functionality is found across earlier
131+
parts of the crate, [`rustc_lint`], [`rustc_middle`], and other places.
132+
133+
Ideally there would be fewer, more cohesive crates, with incremental and
134+
parallel compilation making sure compile times stay reasonable. However,
135+
incremental and parallel compilation haven't gotten good enough for that yet,
136+
so breaking things into separate crates has been our solution so far.
137+
138+
At the top of the dependency tree is [`rustc_driver`] and [`rustc_interface`]
139+
which is an unstable wrapper around the query system helping drive various
140+
stages of compilation. Other consumers of the compiler may use this interface
141+
in different ways (e.g. [`rustdoc`] or maybe eventually `rust-analyzer`). The
142+
[`rustc_driver`] crate first parses command line arguments and then uses
143+
[`rustc_interface`] to drive the compilation to completion.
144+
145+
[parser]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/index.html
112146
[`rustc_lint`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/index.html
113-
114-
Ideally there would be fewer, more
115-
cohesive crates, with incremental and parallel compilation making sure compile
116-
times stay reasonable. However, our incremental and parallel compilation haven't
117-
gotten good enough for that yet, so breaking things into separate crates has
118-
been our solution so far.
119-
120-
At the top of the dependency tree are the [`rustc_interface`] and
121-
[`rustc_driver`] crates. [`rustc_interface`] is an unstable wrapper around the
122-
query system that helps to drive the various stages of compilation. Other
123-
consumers of the compiler may use this interface in different ways (e.g.
124-
rustdoc or maybe eventually rust-analyzer). The [`rustc_driver`] crate first
125-
parses command line arguments and then uses [`rustc_interface`] to drive the
126-
compilation to completion.
127-
128147
[query]: ./query.md
129148

130-
[orgch]: ./overview.md
131-
132149
## rustdoc
133150

134-
The bulk of `rustdoc` is in [`librustdoc`]. However, the `rustdoc` binary
151+
The bulk of [`rustdoc`] is in [`librustdoc`]. However, the [`rustdoc`] binary
135152
itself is [`src/tools/rustdoc`], which does nothing except call [`rustdoc::main`].
136153

137-
There is also javascript and CSS for the rustdocs in [`src/tools/rustdoc-js`]
154+
There is also `JavaScript` and `CSS` for the docs in [`src/tools/rustdoc-js`]
138155
and [`src/tools/rustdoc-themes`].
139156

140-
You can read more about rustdoc in [this chapter][rustdocch].
157+
You can read more about [`rustdoc`] in [this chapter][`rustdoc`].
141158

142159
[`librustdoc`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustdoc/index.html
143160
[`rustdoc::main`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustdoc/fn.main.html
144-
[`src/tools/rustdoc`]: https://github.com/rust-lang/rust/tree/master/src/tools/rustdoc
145161
[`src/tools/rustdoc-js`]: https://github.com/rust-lang/rust/tree/master/src/tools/rustdoc-js
146162
[`src/tools/rustdoc-themes`]: https://github.com/rust-lang/rust/tree/master/src/tools/rustdoc-themes
147-
148-
[rustdocch]: ./rustdoc.md
163+
[`src/tools/rustdoc`]: https://github.com/rust-lang/rust/tree/master/src/tools/rustdoc
164+
[`rustdoc`]: ./rustdoc.md
149165

150166
## Tests
151167

152168
The test suite for all of the above is in [`tests/`]. You can read more
153169
about the test suite [in this chapter][testsch].
154170

155-
The test harness itself is in [`src/tools/compiletest`].
156-
157-
[testsch]: ./tests/intro.md
171+
The test harness is in [`src/tools/compiletest/`][`compiletest/`].
158172

159173
[`tests/`]: https://github.com/rust-lang/rust/tree/master/tests
160-
[`src/tools/compiletest`]: https://github.com/rust-lang/rust/tree/master/src/tools/compiletest
174+
[testsch]: ./tests/intro.md
161175

162176
## Build System
163177

164178
There are a number of tools in the repository just for building the compiler,
165-
standard library, rustdoc, etc, along with testing, building a full Rust
179+
standard library, [`rustdoc`], etc, along with testing, building a full Rust
166180
distribution, etc.
167181

168-
One of the primary tools is [`src/bootstrap`]. You can read more about
182+
One of the primary tools is [`src/bootstrap/`]. You can read more about
169183
bootstrapping [in this chapter][bootstch]. The process may also use other tools
170-
from `src/tools/`, such as [`tidy`] or [`compiletest`].
171-
172-
[`src/bootstrap`]: https://github.com/rust-lang/rust/tree/master/src/bootstrap
173-
[`tidy`]: https://github.com/rust-lang/rust/tree/master/src/tools/tidy
174-
[`compiletest`]: https://github.com/rust-lang/rust/tree/master/src/tools/compiletest
184+
from [`src/tools/`], such as [`tidy/`] or [`compiletest/`].
175185

186+
[`compiletest/`]: https://github.com/rust-lang/rust/tree/master/src/tools/compiletest
187+
[`src/bootstrap/`]: https://github.com/rust-lang/rust/tree/master/src/bootstrap
188+
[`src/tools/`]: https://github.com/rust-lang/rust/tree/master/src/tools
189+
[`tidy/`]: https://github.com/rust-lang/rust/tree/master/src/tools/tidy
176190
[bootstch]: ./building/bootstrapping/intro.md
177191

178192
## Standard library
179193

180-
The standard library crates are all in `library/`. They have intuitive names
181-
like `std`, `core`, `alloc`, etc. There is also `proc_macro`, `test`, and
182-
other runtime libraries.
183-
184194
This code is fairly similar to most other Rust crates except that it must be
185-
built in a special way because it can use unstable features.
195+
built in a special way because it can use unstable ([`nightly`]) features.
196+
The standard library is sometimes referred to as [`libstd or the "standard facade"`].
197+
198+
[`libstd or the "standard facade"`]: https://rust-lang.github.io/rfcs/0040-libstd-facade.html
199+
[`nightly`]: https://doc.rust-lang.org/nightly/nightly-rustc/
186200

187201
## Other
188202

189203
There are a lot of other things in the `rust-lang/rust` repo that are related
190-
to building a full Rust distribution. Most of the time you don't need to worry
191-
about them.
204+
to building a full Rust distribution. Most of the time you don't need to worry about them.
192205

193206
These include:
194207
- [`src/ci`]: The CI configuration. This actually quite extensive because we

0 commit comments

Comments
 (0)