Skip to content

Commit e8370fd

Browse files
committed
update rustc driver chapters
1 parent 4013f45 commit e8370fd

File tree

4 files changed

+32
-56
lines changed

4 files changed

+32
-56
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
- [Part 2: How rustc works](./part-2-intro.md)
2828
- [High-level overview of the compiler source](./high-level-overview.md)
29-
- [The Rustc Driver](./rustc-driver.md)
29+
- [The Rustc Driver and Interface](./rustc-driver.md)
3030
- [Rustdoc](./rustdoc.md)
3131
- [Queries: demand-driven compilation](./query.md)
3232
- [The Query Evaluation Model in Detail](./queries/query-evaluation-model-in-detail.md)

src/appendix/code-index.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ compiler.
77
Item | Kind | Short description | Chapter | Declaration
88
----------------|----------|-----------------------------|--------------------|-------------------
99
`BodyId` | struct | One of four types of HIR node identifiers | [Identifiers in the HIR] | [src/librustc/hir/mod.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc/hir/struct.BodyId.html)
10-
`CompileState` | struct | State that is passed to a callback at each compiler pass | [The Rustc Driver] | [src/librustc_driver/driver.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/driver/struct.CompileState.html)
10+
`Compiler` | struct | Represents a compiler session and can be used to drive a compilation. | [The Rustc Driver and Interface] | [src/librustc_interface/interface.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/interface/struct.Compiler.html)
1111
`ast::Crate` | struct | A syntax-level representation of a parsed crate | [The parser] | [src/librustc/hir/mod.rs](https://doc.rust-lang.org/nightly/nightly-rustc/syntax/ast/struct.Crate.html)
1212
`hir::Crate` | struct | A more abstract, compiler-friendly form of a crate's AST | [The Hir] | [src/librustc/hir/mod.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc/hir/struct.Crate.html)
1313
`DefId` | struct | One of four types of HIR node identifiers | [Identifiers in the HIR] | [src/librustc/hir/def_id.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc/hir/def_id/struct.DefId.html)
@@ -18,8 +18,9 @@ Item | Kind | Short description | Chapter |
1818
`P` | struct | An owned immutable smart pointer. By contrast, `&T` is not owned, and `Box<T>` is not immutable. | None | [src/syntax/ptr.rs](https://doc.rust-lang.org/nightly/nightly-rustc/syntax/ptr/struct.P.html)
1919
`ParamEnv` | struct | Information about generic parameters or `Self`, useful for working with associated or generic items | [Parameter Environment] | [src/librustc/ty/mod.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/struct.ParamEnv.html)
2020
`ParseSess` | struct | This struct contains information about a parsing session | [The parser] | [src/libsyntax/parse/mod.rs](https://doc.rust-lang.org/nightly/nightly-rustc/syntax/parse/struct.ParseSess.html)
21+
`Query` | struct | Represents the result of query to the `Compiler` interface and allows stealing, borrowing, and returning the results of compiler passes. | [The Rustc Driver and Interface] | [src/librustc_interface/queries.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/queries/struct.Query.html)
2122
`Rib` | struct | Represents a single scope of names | [Name resolution] | [src/librustc_resolve/lib.rs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_resolve/struct.Rib.html)
22-
`Session` | struct | The data associated with a compilation session | [The parser], [The Rustc Driver] | [src/librustc/session/mod.html](https://doc.rust-lang.org/nightly/nightly-rustc/rustc/session/struct.Session.html)
23+
`Session` | struct | The data associated with a compilation session | [The parser], [The Rustc Driver and Interface] | [src/librustc/session/mod.html](https://doc.rust-lang.org/nightly/nightly-rustc/rustc/session/struct.Session.html)
2324
`SourceFile` | struct | Part of the `SourceMap`. Maps AST nodes to their source code for a single source file. Was previously called FileMap | [The parser] | [src/libsyntax_pos/lib.rs](https://doc.rust-lang.org/nightly/nightly-rustc/syntax/source_map/struct.SourceFile.html)
2425
`SourceMap` | struct | Maps AST nodes to their source code. It is composed of `SourceFile`s. Was previously called CodeMap | [The parser] | [src/libsyntax/source_map.rs](https://doc.rust-lang.org/nightly/nightly-rustc/syntax/source_map/struct.SourceMap.html)
2526
`Span` | struct | A location in the user's source code, used for error reporting primarily | [Emitting Diagnostics] | [src/libsyntax_pos/span_encoding.rs](https://doc.rust-lang.org/nightly/nightly-rustc/syntax_pos/struct.Span.html)
@@ -33,7 +34,7 @@ Item | Kind | Short description | Chapter |
3334
[The HIR]: ../hir.html
3435
[Identifiers in the HIR]: ../hir.html#hir-id
3536
[The parser]: ../the-parser.html
36-
[The Rustc Driver]: ../rustc-driver.html
37+
[The Rustc Driver and Interface]: ../rustc-driver.html
3738
[Type checking]: ../type-checking.html
3839
[The `ty` modules]: ../ty.html
3940
[Rustdoc]: ../rustdoc.html

src/appendix/stupid-stats.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# Appendix A: A tutorial on creating a drop-in replacement for rustc
22

33
> **Note:** This is a copy of `@nrc`'s amazing [stupid-stats]. You should find
4-
> a copy of the code on the GitHub repository although due to the compiler's
5-
> constantly evolving nature, there is no guarantee it'll compile on the first
6-
> go.
4+
> a copy of the code on the GitHub repository.
5+
>
6+
> Due to the compiler's constantly evolving nature, the `rustc_driver`
7+
> mechanisms described in this chapter have been replaced by a new
8+
> [`rustc_interface`] crate. See [The Rustc Driver and Interface] for more
9+
> information.
710
811
Many tools benefit from being a drop-in replacement for a compiler. By this, I
912
mean that any user of the tool can use `mytool` in all the ways they would
@@ -92,7 +95,7 @@ translation).
9295
> and [`librustc_codegen_utils`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_utils/index.html).
9396
9497
All these phases are coordinated by the driver. To see the exact sequence, look
95-
at [the `compile_input` function in `librustc_driver`][compile-input].
98+
at the `compile_input` function in `librustc_driver`.
9699
The driver handles all the highest level coordination of compilation -
97100
1. handling command-line arguments
98101
2. maintaining compilation state (primarily in the `Session`)
@@ -101,9 +104,6 @@ The driver handles all the highest level coordination of compilation -
101104
To create a drop-in compiler replacement or a compiler replacement,
102105
we leave most of compilation alone and customise the driver using its APIs.
103106

104-
[compile-input]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/driver/fn.compile_input.html
105-
106-
107107
## The driver customisation APIs
108108

109109
There are two primary ways to customise compilation - high level control of the
@@ -410,3 +410,5 @@ internally (I already changed save-analysis to use `CompilerController`). I've
410410
been experimenting with a prototype rustfmt which also uses these APIs.
411411

412412
[stupid-stats]: https://github.com/nrc/stupid-stats
413+
[`rustc_interface`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/index.html
414+
[The Rustc Driver and Interface]: ../rustc-driver.html

src/rustc-driver.md

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,25 @@
1-
# The Rustc Driver
1+
# The Rustc Driver and Interface
22

33
The [`rustc_driver`] is essentially `rustc`'s `main()` function. It acts as
44
the glue for running the various phases of the compiler in the correct order,
5-
managing state such as the [`SourceMap`] \(maps AST nodes to source code),
6-
[`Session`] \(general build context and error messaging) and the [`TyCtxt`]
7-
\(the "typing context", allowing you to query the type system and other cool
8-
stuff). The `rustc_driver` crate also provides external users with a method
5+
using the interface defined in the [`rustc_interface`] crate.
6+
7+
The `rustc_interface` crate provides external users with an (unstable) API
98
for running code at particular times during the compilation process, allowing
109
third parties to effectively use `rustc`'s internals as a library for
11-
analysing a crate or emulating the compiler in-process (e.g. the RLS).
12-
13-
For those using `rustc` as a library, the `run_compiler()` function is the main
14-
entrypoint to the compiler. Its main parameters are a list of command-line
15-
arguments and a reference to something which implements the `CompilerCalls`
16-
trait. A `CompilerCalls` creates the overall `CompileController`, letting it
17-
govern which compiler passes are run and attach callbacks to be fired at the end
18-
of each phase.
19-
20-
From `rustc_driver`'s perspective, the main phases of the compiler are:
21-
22-
1. *Parse Input:* Initial crate parsing
23-
2. *Configure and Expand:* Resolve `#[cfg]` attributes, name resolution, and
24-
expand macros
25-
3. *Run Analysis Passes:* Run trait resolution, typechecking, region checking
26-
and other miscellaneous analysis passes on the crate
27-
4. *Translate to LLVM:* Translate to the in-memory form of LLVM IR and turn it
28-
into an executable/object files
10+
analysing a crate or emulating the compiler in-process (e.g. the RLS or rustdoc).
2911

30-
The `CompileController` then gives users the ability to inspect the ongoing
31-
compilation process
12+
For those using `rustc` as a library, the `interface::run_compiler()` function is the main
13+
entrypoint to the compiler. It takes a configuration for the compiler and a closure that
14+
takes a [`Compiler`]. `run_compiler` creates a `Compiler` from the configuration and passes
15+
it to the closure. Inside the closure, you can use the `Compiler` to drive queries to compile
16+
a crate and get the results. This is what the `rustc_driver` does too.
3217

33-
- after parsing
34-
- after AST expansion
35-
- after HIR lowering
36-
- after analysis, and
37-
- when compilation is done
38-
39-
The `CompileState`'s various `state_after_*()` constructors can be inspected to
40-
determine what bits of information are available to which callback.
41-
42-
For a more detailed explanation on using `rustc_driver`, check out the
43-
[stupid-stats] guide by `@nrc` (attached as [Appendix A]).
18+
You can see what queries are currently available through the rustdocs for [`Compiler`].
19+
You can see an example of how to use them by looking at the `rustc_driver` implementation,
20+
specifically the [`rustc_driver::run_compiler` function][rd_rc] (not to be confused with
21+
`interface::run_compiler`). The `rustc_driver::run_compiler` function takes a bunch of
22+
command-line args and some other configurations and drives the compilation to completion.
4423

4524
> **Warning:** By its very nature, the internal compiler APIs are always going
4625
> to be unstable. That said, we do try not to break things unnecessarily.
@@ -54,21 +33,15 @@ manifests itself in the way people can plug into the compiler, preferring a
5433
"push"-style API (callbacks) instead of the more Rust-ic "pull" style (think
5534
the `Iterator` trait).
5635

57-
For example the [`CompileState`], the state passed to callbacks after each
58-
phase, is essentially just a box of optional references to pieces inside the
59-
compiler. The lifetime bound on the `CompilerCalls` trait then helps to ensure
60-
compiler internals don't "escape" the compiler (e.g. if you tried to keep a
61-
reference to the AST after the compiler is finished), while still letting users
62-
record *some* state for use after the `run_compiler()` function finishes.
63-
6436
Thread-local storage and interning are used a lot through the compiler to reduce
6537
duplication while also preventing a lot of the ergonomic issues due to many
6638
pervasive lifetimes. The `rustc::ty::tls` module is used to access these
6739
thread-locals, although you should rarely need to touch it.
6840

69-
41+
[rd_rc]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/fn.run_compiler.html
42+
[`rustc_interface`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/index.html
7043
[`rustc_driver`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/
71-
[`CompileState`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/driver/struct.CompileState.html
44+
[`Compiler`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/interface/struct.Compiler.html
7245
[`Session`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/session/struct.Session.html
7346
[`TyCtxt`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/struct.TyCtxt.html
7447
[`SourceMap`]: https://doc.rust-lang.org/nightly/nightly-rustc/syntax/source_map/struct.SourceMap.html

0 commit comments

Comments
 (0)