Skip to content

Commit fe632f2

Browse files
committed
apply mark-i-m's suggestions
1 parent 95bab3e commit fe632f2

9 files changed

+209
-202
lines changed

Diff for: src/SUMMARY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
- [Type checking](./type-checking.md)
2525
- [The MIR (Mid-level IR)](./mir.md)
2626
- [MIR construction](./mir-construction.md)
27-
- [MIR visitor](./mir-visitor.md)
27+
- [MIR visitor and traversal](./mir-visitor.md)
2828
- [MIR passes: getting the MIR for a function](./mir-passes.md)
2929
- [MIR borrowck](./mir-borrowck.md)
3030
- [MIR-based region checking (NLL)](./mir-regionck.md)

Diff for: src/background.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ A control-flow graph is structured as a set of **basic blocks**
1717
connected by edges. The key idea of a basic block is that it is a set
1818
of statements that execute "together" -- that is, whenever you branch
1919
to a basic block, you start at the first statement and then execute
20-
all the remainder. Only at the end of the is there the possibility of
21-
branching to more than one place (in MIR, we call that final statement
22-
the **terminator**):
20+
all the remainder. Only at the end of the block is there the
21+
possibility of branching to more than one place (in MIR, we call that
22+
final statement the **terminator**):
2323

2424
```
2525
bb0: {
@@ -88,7 +88,8 @@ cycle.
8888

8989
## What is co- and contra-variance?
9090

91-
*to be written*
91+
Check out the subtyping chapter from the
92+
[Rust Nomicon](https://doc.rust-lang.org/nomicon/subtyping.html).
9293

9394
<a name=free-vs-bound>
9495

@@ -97,18 +98,17 @@ cycle.
9798
Let's describe the concepts of free vs bound in terms of program
9899
variables, since that's the thing we're most familiar with.
99100

100-
- Consider this expression: `a + b`. In this expression, `a` and `b`
101-
refer to local variables that are defined *outside* of the
102-
expression. We say that those variables **appear free** in the
103-
expression. To see why this term makes sense, consider the next
104-
example.
105-
- In contrast, consider this expression, which creates a closure: `|a,
101+
- Consider this expression, which creates a closure: `|a,
106102
b| a + b`. Here, the `a` and `b` in `a + b` refer to the arguments
107103
that the closure will be given when it is called. We say that the
108104
`a` and `b` there are **bound** to the closure, and that the closure
109105
signature `|a, b|` is a **binder** for the names `a` and `b`
110106
(because any references to `a` or `b` within refer to the variables
111107
that it introduces).
108+
- Consider this expression: `a + b`. In this expression, `a` and `b`
109+
refer to local variables that are defined *outside* of the
110+
expression. We say that those variables **appear free** in the
111+
expression (i.e., they are **free**, not **bound** (tied up)).
112112

113113
So there you have it: a variable "appears free" in some
114114
expression/statement/whatever if it refers to something defined

Diff for: src/glossary.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@ The compiler uses a number of...idiosyncratic abbreviations and things. This glo
66
Term | Meaning
77
------------------------|--------
88
AST | the abstract syntax tree produced by the syntax crate; reflects user syntax very closely.
9+
binder | a "binder" is a place where a variable or type is declared; for example, the `<T>` is a binder for the generic type parameter `T` in `fn foo<T>(..)`, and `|a| ...` is a binder for the parameter `a`. See [the background chapter for more](./background.html#free-vs-bound)
10+
bound variable | a "bound variable" is one that is declared within an expression/term. For example, the variable `a` is bound within the closure expession `|a| a * 2`. See [the background chapter for more](./background.html#free-vs-bound)
911
codegen unit | when we produce LLVM IR, we group the Rust code into a number of codegen units. Each of these units is processed by LLVM independently from one another, enabling parallelism. They are also the unit of incremental re-use.
1012
completeness | completeness is a technical term in type theory. Completeness means that every type-safe program also type-checks. Having both soundness and completeness is very hard, and usually soundness is more important. (see "soundness").
13+
control-flow graph | a representation of the control-flow of a program; see [the background chapter for more](./background.html#cfg)
1114
cx | we tend to use "cx" as an abbrevation for context. See also `tcx`, `infcx`, etc.
1215
DAG | a directed acyclic graph is used during compilation to keep track of dependencies between queries. ([see more](incremental-compilation.html))
16+
data-flow analysis | a static analysis that figures out what properties are true at each point in the control-flow of a program; see [the background chapter for more](./background.html#dataflow)
1317
DefId | an index identifying a definition (see `librustc/hir/def_id.rs`). Uniquely identifies a `DefPath`.
18+
free variable | a "free variable" is one that is not bound within an expression or term; see [the background chapter for more](./background.html#free-vs-bound)
1419
'gcx | the lifetime of the global arena ([see more](ty.html))
1520
generics | the set of generic type parameters defined on a type or item
1621
HIR | the High-level IR, created by lowering and desugaring the AST ([see more](hir.html))
1722
HirId | identifies a particular node in the HIR by combining a def-id with an "intra-definition offset".
1823
HIR Map | The HIR map, accessible via tcx.hir, allows you to quickly navigate the HIR and convert between various forms of identifiers.
1924
ICE | internal compiler error. When the compiler crashes.
2025
ICH | incremental compilation hash. ICHs are used as fingerprints for things such as HIR and crate metadata, to check if changes have been made. This is useful in incremental compilation to see if part of a crate has changed and should be recompiled.
21-
inference variable | when doing type or region inference, an "inference variable" is a kind of special type/region that represents value you are trying to find. Think of `X` in algebra.
26+
inference variable | when doing type or region inference, an "inference variable" is a kind of special type/region that represents what you are trying to infer. Think of X in algebra. For example, if we are trying to infer the type of a variable in a program, we create an inference variable to represent that unknown type.
2227
infcx | the inference context (see `librustc/infer`)
2328
IR | Intermediate Representation. A general term in compilers. During compilation, the code is transformed from raw source (ASCII text) to various IRs. In Rust, these are primarily HIR, MIR, and LLVM IR. Each IR is well-suited for some set of computations. For example, MIR is well-suited for the borrow checker, and LLVM IR is well-suited for codegen because LLVM accepts it.
2429
local crate | the crate currently being compiled.
@@ -27,14 +32,18 @@ LTO | Link-Time Optimizations. A set of optimizations offer
2732
MIR | the Mid-level IR that is created after type-checking for use by borrowck and trans ([see more](./mir.html))
2833
miri | an interpreter for MIR used for constant evaluation ([see more](./miri.html))
2934
newtype | a "newtype" is a wrapper around some other type (e.g., `struct Foo(T)` is a "newtype" for `T`). This is commonly used in Rust to give a stronger type for indices.
35+
NLL | [non-lexical lifetimes](./mir-regionck.html), an extension to Rust's borrowing system to make it be based on the control-flow graph.
3036
node-id or NodeId | an index identifying a particular node in the AST or HIR; gradually being phased out and replaced with `HirId`.
3137
obligation | something that must be proven by the trait system ([see more](trait-resolution.html))
38+
promoted constants | constants extracted from a function and lifted to static scope; see [this section](./mir.html#promoted) for more details.
3239
provider | the function that executes a query ([see more](query.html))
40+
quantified | in math or logic, existential and universal quantification are used to ask questions like "is there any type T for which is true?" or "is this true for all types T?"; see [the background chapter for more](./background.html#quantified)
3341
query | perhaps some sub-computation during compilation ([see more](query.html))
3442
region | another term for "lifetime" often used in the literature and in the borrow checker.
3543
sess | the compiler session, which stores global data used throughout compilation
3644
side tables | because the AST and HIR are immutable once created, we often carry extra information about them in the form of hashtables, indexed by the id of a particular node.
3745
sigil | like a keyword but composed entirely of non-alphanumeric tokens. For example, `&` is a sigil for references.
46+
skolemization | a way of handling subtyping around "for-all" types (e.g., `for<'a> fn(&'a u32)` as well as solving higher-ranked trait bounds (e.g., `for<'a> T: Trait<'a>`). See [the chapter on skolemization and universes](./mir-regionck.html#skol) for more details.
3847
soundness | soundness is a technical term in type theory. Roughly, if a type system is sound, then if a program type-checks, it is type-safe; i.e. I can never (in safe rust) force a value into a variable of the wrong type. (see "completeness").
3948
span | a location in the user's source code, used for error reporting primarily. These are like a file-name/line-number/column tuple on steroids: they carry a start/end point, and also track macro expansions and compiler desugaring. All while being packed into a few bytes (really, it's an index into a table). See the Span datatype for more.
4049
substs | the substitutions for a given generic type or item (e.g. the `i32`, `u32` in `HashMap<i32, u32>`)
@@ -45,6 +54,7 @@ token | the smallest unit of parsing. Tokens are produced aft
4554
trans | the code to translate MIR into LLVM IR.
4655
trait reference | a trait and values for its type parameters ([see more](ty.html)).
4756
ty | the internal representation of a type ([see more](ty.html)).
57+
variance | variance determines how changes to a generic type/lifetime parameter affect subtyping; for example, if `T` is a subtype of `U`, then `Vec<T>` is a subtype `Vec<U>` because `Vec` is *covariant* in its generic parameter. See [the background chapter for more](./background.html#variance).
4858

4959
[LLVM]: https://llvm.org/
5060
[lto]: https://llvm.org/docs/LinkTimeOptimization.html

Diff for: src/mir-background.md

-122
This file was deleted.

Diff for: src/mir-borrowck.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ in several modes, but this text will describe only the mode when NLL is enabled
3737

3838
The overall flow of the borrow checker is as follows:
3939

40-
- We first create a **local copy** C of the MIR. We will be modifying
41-
this copy in place to modify the types and things to include
42-
references to the new regions that we are computing.
40+
- We first create a **local copy** C of the MIR. In the coming steps,
41+
we will modify this copy in place to modify the types and things to
42+
include references to the new regions that we are computing.
4343
- We then invoke `nll::replace_regions_in_mir` to modify this copy C.
4444
Among other things, this function will replace all of the regions in
4545
the MIR with fresh [inference variables](glossary.html).
@@ -51,6 +51,6 @@ The overall flow of the borrow checker is as follows:
5151
- (More details can be found in [the NLL section](./mir-regionck.html).)
5252
- Finally, the borrow checker itself runs, taking as input (a) the
5353
results of move analysis and (b) the regions computed by the region
54-
checker. This allows is to figure out which loans are still in scope
54+
checker. This allows us to figure out which loans are still in scope
5555
at any particular point.
5656

Diff for: src/mir-passes.md

+20-16
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ If you would like to get the MIR for a function (or constant, etc),
44
you can use the `optimized_mir(def_id)` query. This will give you back
55
the final, optimized MIR. For foreign def-ids, we simply read the MIR
66
from the other crate's metadata. But for local def-ids, the query will
7-
construct the MIR and then iteratively optimize it by putting it
8-
through various pipeline stages. This section describes those pipeline
9-
stages and how you can extend them.
7+
construct the MIR and then iteratively optimize it by applying a
8+
series of passes. This section describes how those passes work and how
9+
you can extend them.
1010

1111
To produce the `optimized_mir(D)` for a given def-id `D`, the MIR
1212
passes through several suites of optimizations, each represented by a
@@ -97,18 +97,19 @@ that appeared within the `main` function.)
9797
### Implementing and registering a pass
9898

9999
A `MirPass` is some bit of code that processes the MIR, typically --
100-
but not always -- transforming it along the way in some way. For
101-
example, it might perform an optimization. The `MirPass` trait itself
102-
is found in in [the `rustc_mir::transform` module][mirtransform], and
103-
it basically consists of one method, `run_pass`, that simply gets an
100+
but not always -- transforming it along the way somehow. For example,
101+
it might perform an optimization. The `MirPass` trait itself is found
102+
in in [the `rustc_mir::transform` module][mirtransform], and it
103+
basically consists of one method, `run_pass`, that simply gets an
104104
`&mut Mir` (along with the tcx and some information about where it
105-
came from).
105+
came from). The MIR is therefore modified in place (which helps to
106+
keep things efficient).
106107

107-
A good example of a basic MIR pass is [`NoLandingPads`], which walks the
108-
MIR and removes all edges that are due to unwinding -- this is used
109-
with when configured with `panic=abort`, which never unwinds. As you can see
110-
from its source, a MIR pass is defined by first defining a dummy type, a struct
111-
with no fields, something like:
108+
A good example of a basic MIR pass is [`NoLandingPads`], which walks
109+
the MIR and removes all edges that are due to unwinding -- this is
110+
used when configured with `panic=abort`, which never unwinds. As you
111+
can see from its source, a MIR pass is defined by first defining a
112+
dummy type, a struct with no fields, something like:
112113

113114
```rust
114115
struct MyPass;
@@ -120,8 +121,9 @@ this pass into the appropriate list of passes found in a query like
120121
should go into the `optimized_mir` list.)
121122

122123
If you are writing a pass, there's a good chance that you are going to
123-
want to use a [MIR visitor] too -- those are a handy visitor that
124-
walks the MIR for you and lets you make small edits here and there.
124+
want to use a [MIR visitor]. MIR visitors are a handy way to walk all
125+
the parts of the MIR, either to search for something or to make small
126+
edits.
125127

126128
### Stealing
127129

@@ -149,7 +151,9 @@ be **stolen** by the `mir_validated()` suite. If nothing was done,
149151
then `mir_const_qualif(D)` would succeed if it came before
150152
`mir_validated(D)`, but fail otherwise. Therefore, `mir_validated(D)`
151153
will **force** `mir_const_qualif` before it actually steals, thus
152-
ensuring that the reads have already happened:
154+
ensuring that the reads have already happened (remember that
155+
[queries are memoized](./query.html), so executing a query twice
156+
simply loads from a cache the second time):
153157

154158
```
155159
mir_const(D) --read-by--> mir_const_qualif(D)

0 commit comments

Comments
 (0)