Skip to content

Commit b620d4e

Browse files
committed
Re-organize source code repr chapter
1 parent ffd9a44 commit b620d4e

File tree

5 files changed

+65
-133
lines changed

5 files changed

+65
-133
lines changed

src/SUMMARY.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@
9696
# Source Code Representation
9797

9898
- [Prologue](./part-3-intro.md)
99-
- [Command-line arguments](./cli.md)
100-
- [rustc_driver and rustc_interface](./rustc-driver/intro.md)
101-
- [Example: Type checking](./rustc-driver/interacting-with-the-ast.md)
102-
- [Example: Getting diagnostics](./rustc-driver/getting-diagnostics.md)
10399
- [Syntax and the AST](./syntax-intro.md)
104100
- [Lexing and Parsing](./the-parser.md)
105101
- [Macro expansion](./macro-expansion.md)
@@ -118,10 +114,22 @@
118114
- [MIR construction](./mir/construction.md)
119115
- [MIR visitor and traversal](./mir/visitor.md)
120116
- [MIR queries and passes: getting the MIR](./mir/passes.md)
121-
- [Identifiers in the Compiler](./identifiers.md)
122-
- [Closure expansion](./closure.md)
123117
- [Inline assembly](./asm.md)
124118

119+
# Supporting Infrastructure
120+
121+
- [Command-line arguments](./cli.md)
122+
- [rustc_driver and rustc_interface](./rustc-driver/intro.md)
123+
- [Example: Type checking](./rustc-driver/interacting-with-the-ast.md)
124+
- [Example: Getting diagnostics](./rustc-driver/getting-diagnostics.md)
125+
- [Errors and Lints](diagnostics.md)
126+
- [Diagnostic and subdiagnostic structs](./diagnostics/diagnostic-structs.md)
127+
- [Translation](./diagnostics/translation.md)
128+
- [`LintStore`](./diagnostics/lintstore.md)
129+
- [Error codes](./diagnostics/error-codes.md)
130+
- [Diagnostic items](./diagnostics/diagnostic-items.md)
131+
- [`ErrorGuaranteed`](./diagnostics/error-guaranteed.md)
132+
125133
# Analysis
126134

127135
- [Prologue](./part-4-intro.md)
@@ -190,13 +198,7 @@
190198
- [Closure constraints](./borrow_check/region_inference/closure_constraints.md)
191199
- [Error reporting](./borrow_check/region_inference/error_reporting.md)
192200
- [Two-phase-borrows](./borrow_check/two_phase_borrows.md)
193-
- [Errors and Lints](diagnostics.md)
194-
- [Diagnostic and subdiagnostic structs](./diagnostics/diagnostic-structs.md)
195-
- [Translation](./diagnostics/translation.md)
196-
- [`LintStore`](./diagnostics/lintstore.md)
197-
- [Error codes](./diagnostics/error-codes.md)
198-
- [Diagnostic items](./diagnostics/diagnostic-items.md)
199-
- [`ErrorGuaranteed`](./diagnostics/error-guaranteed.md)
201+
- [Closure capture inference](./closure.md)
200202
- [Async closures/"coroutine-closures"](coroutine-closures.md)
201203

202204
# MIR to Binaries

src/closure.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Closure Expansion in rustc
1+
# Closure Capture Inference
22

33
This section describes how rustc handles closures. Closures in Rust are
44
effectively "desugared" into structs that contain the values they use (or

src/hir.md

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,40 @@ the compiler a chance to observe that you accessed the data for
7878

7979
## Identifiers in the HIR
8080

81-
There are a bunch of different identifiers to refer to other nodes or definitions
82-
in the HIR. In short:
83-
- A [`DefId`] refers to a *definition* in any crate.
84-
- A [`LocalDefId`] refers to a *definition* in the currently compiled crate.
85-
- A [`HirId`] refers to *any node* in the HIR.
81+
The HIR uses a bunch of different identifiers that coexist and serve different purposes.
8682

87-
For more detailed information, check out the [chapter on identifiers][ids].
83+
- A [`DefId`], as the name suggests, identifies a particular definition, or top-level
84+
item, in a given crate. It is composed of two parts: a [`CrateNum`] which identifies
85+
the crate the definition comes from, and a [`DefIndex`] which identifies the definition
86+
within the crate. Unlike [`HirId`]s, there isn't a [`DefId`] for every expression, which
87+
makes them more stable across compilations.
88+
89+
- A [`LocalDefId`] is basically a [`DefId`] that is known to come from the current crate.
90+
This allows us to drop the [`CrateNum`] part, and use the type system to ensure that
91+
only local definitions are passed to functions that expect a local definition.
92+
93+
- A [`HirId`] uniquely identifies a node in the HIR of the current crate. It is composed
94+
of two parts: an `owner` and a `local_id` that is unique within the `owner`. This
95+
combination makes for more stable values which are helpful for incremental compilation.
96+
Unlike [`DefId`]s, a [`HirId`] can refer to [fine-grained entities][Node] like expressions,
97+
but stays local to the current crate.
98+
99+
- A [`BodyId`] identifies a HIR [`Body`] in the current crate. It is currently only
100+
a wrapper around a [`HirId`]. For more info about HIR bodies, please refer to the
101+
[HIR chapter][hir-bodies].
102+
103+
These identifiers can be converted into one another through the [HIR map][map].
88104

89105
[`DefId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.DefId.html
90106
[`LocalDefId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.LocalDefId.html
91107
[`HirId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir_id/struct.HirId.html
92-
[ids]: ./identifiers.md#in-the-hir
108+
[`BodyId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/struct.BodyId.html
109+
[`CrateNum`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.CrateNum.html
110+
[`DefIndex`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.DefIndex.html
111+
[`Body`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/struct.Body.html
112+
[hir-map]: ./hir.md#the-hir-map
113+
[hir-bodies]: ./hir.md#hir-bodies
114+
[map]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html
93115

94116
## The HIR Map
95117

src/identifiers.md

Lines changed: 0 additions & 107 deletions
This file was deleted.

src/the-parser.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,39 @@ This happens in two stages: Lexing and Parsing.
66

77
1. _Lexing_ takes strings and turns them into streams of [tokens]. For
88
example, `foo.bar + buz` would be turned into the tokens `foo`, `.`, `bar`,
9-
`+`, and `buz`.
9+
`+`, and `buz`. This is implemented in [`rustc_lexer`][lexer].
1010

1111
[tokens]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/token/index.html
1212
[lexer]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lexer/index.html
1313

1414
2. _Parsing_ takes streams of tokens and turns them into a structured form
1515
which is easier for the compiler to work with, usually called an [*Abstract
1616
Syntax Tree* (AST)][ast] .
17-
18-
19-
An AST mirrors the structure of a Rust program in memory, using a `Span` to
17+
18+
## The AST
19+
20+
The AST mirrors the structure of a Rust program in memory, using a `Span` to
2021
link a particular AST node back to its source text. The AST is defined in
2122
[`rustc_ast`][rustc_ast], along with some definitions for tokens and token
2223
streams, data structures/traits for mutating ASTs, and shared definitions for
2324
other AST-related parts of the compiler (like the lexer and
2425
macro-expansion).
2526

26-
The lexer is developed in [`rustc_lexer`][lexer].
27+
Every node in the AST has its own [`NodeId`], including top-level items
28+
such as structs, but also individual statements and expressions. A [`NodeId`]
29+
is an identifier number that uniquely identifies an AST node within a crate.
30+
31+
However, because they are absolute within a crate, adding or removing a single
32+
node in the AST causes all the subsequent [`NodeId`]s to change. This renders
33+
[`NodeId`]s pretty much useless for incremental compilation, where you want as
34+
few things as possible to change.
35+
36+
[`NodeId`]s are used in all the `rustc` bits that operate directly on the AST,
37+
like macro expansion and name resolution (more on these over the next couple chapters).
38+
39+
[`NodeId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/node_id/struct.NodeId.html
40+
41+
## Parsing
2742

2843
The parser is defined in [`rustc_parse`][rustc_parse], along with a
2944
high-level interface to the lexer and some validation routines that run after

0 commit comments

Comments
 (0)