Skip to content

Commit fde9968

Browse files
Alexander Regueiromark-i-m
authored andcommitted
Cleaned up section on type inference.
1 parent 61d67c5 commit fde9968

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

src/type-inference.md

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,21 @@ the following:
2323

2424
```rust
2525
tcx.infer_ctxt().enter(|infcx| {
26-
// use the inference context `infcx` in here
26+
// Use the inference context `infcx` here.
2727
})
2828
```
2929

3030
Each inference context creates a short-lived type arena to store the
31-
fresh types and things that it will create, as described in
32-
[the README in the ty module][ty-readme]. This arena is created by the `enter`
33-
function and disposed after it returns.
31+
fresh types and things that it will create, as described in the
32+
[README in the `ty` module][ty-readme]. This arena is created by the `enter`
33+
function and disposed of after it returns.
3434

3535
[ty-readme]: ty.html
3636

37-
Within the closure, the infcx will have the type `InferCtxt<'cx, 'gcx,
38-
'tcx>` for some fresh `'cx` and `'tcx` – the latter corresponds to
39-
the lifetime of this temporary arena, and the `'cx` is the lifetime of
40-
the `InferCtxt` itself. (Again, see [that ty README][ty-readme] for
41-
more details on this setup.)
37+
Within the closure, `infcx` has the type `InferCtxt<'cx, 'gcx, 'tcx>`
38+
for some fresh `'cx` and `'tcx` – the latter corresponds to the lifetime of
39+
this temporary arena, and the `'cx` is the lifetime of the `InferCtxt` itself.
40+
(Again, see the [`ty` README][ty-readme] for more details on this setup.)
4241

4342
The `tcx.infer_ctxt` method actually returns a build, which means
4443
there are some kinds of configuration you can do before the `infcx` is
@@ -58,7 +57,7 @@ inference works, or perhaps this blog post on
5857

5958
[Unification in the Chalk project]: http://smallcultfollowing.com/babysteps/blog/2017/03/25/unification-in-chalk-part-1/
6059

61-
All told, the inference context stores four kinds of inference variables as of this
60+
All said, the inference context stores four kinds of inference variables as of
6261
writing:
6362

6463
- Type variables, which come in three varieties:
@@ -67,7 +66,7 @@ writing:
6766
arise from an integer literal expression like `22`.
6867
- Float type variables, which can only be unified with a float type, and
6968
arise from a float literal expression like `22.0`.
70-
- Region variables, which represent lifetimes, and arise all over the dang place.
69+
- Region variables, which represent lifetimes, and arise all over the place.
7170

7271
All the type variables work in much the same way: you can create a new
7372
type variable, and what you get is `Ty<'tcx>` representing an
@@ -86,7 +85,7 @@ The most basic operations you can perform in the type inferencer is
8685
recommended way to add an equality constraint is using the `at`
8786
method, roughly like so:
8887

89-
```
88+
```rust
9089
infcx.at(...).eq(t, u);
9190
```
9291

@@ -95,7 +94,7 @@ doing this unification, and in what environment, and the `eq` method
9594
performs the actual equality constraint.
9695

9796
When you equate things, you force them to be precisely equal. Equating
98-
returns a `InferResult` – if it returns `Err(err)`, then equating
97+
returns an `InferResult` – if it returns `Err(err)`, then equating
9998
failed, and the enclosing `TypeError` will tell you what went wrong.
10099

101100
The success case is perhaps more interesting. The "primary" return
@@ -105,12 +104,12 @@ side-effects of constraining type variables and so forth. However, the
105104
actual return type is not `()`, but rather `InferOk<()>`. The
106105
`InferOk` type is used to carry extra trait obligations – your job is
107106
to ensure that these are fulfilled (typically by enrolling them in a
108-
fulfillment context). See the [trait README] for more background here.
107+
fulfillment context). See the [trait README] for more background on that.
109108

110109
[trait README]: trait-resolution.html
111110

112-
You can also enforce subtyping through `infcx.at(..).sub(..)`. The same
113-
basic concepts apply as above.
111+
You can similarly enforce subtyping through `infcx.at(..).sub(..)`. The same
112+
basic concepts as above apply.
114113

115114
## "Trying" equality
116115

@@ -119,7 +118,7 @@ types without error. You can test that with `infcx.can_eq` (or
119118
`infcx.can_sub` for subtyping). If this returns `Ok`, then equality
120119
is possible – but in all cases, any side-effects are reversed.
121120

122-
Be aware though that the success or failure of these methods is always
121+
Be aware, though, that the success or failure of these methods is always
123122
**modulo regions**. That is, two types `&'a u32` and `&'b u32` will
124123
return `Ok` for `can_eq`, even if `'a != 'b`. This falls out from the
125124
"two-phase" nature of how we solve region constraints.
@@ -146,7 +145,7 @@ patterns.
146145

147146
## Subtyping obligations
148147

149-
One thing worth discussing are subtyping obligations. When you force
148+
One thing worth discussing is subtyping obligations. When you force
150149
two types to be a subtype, like `?T <: i32`, we can often convert those
151150
into equality constraints. This follows from Rust's rather limited notion
152151
of subtyping: so, in the above case, `?T <: i32` is equivalent to `?T = i32`.
@@ -172,11 +171,11 @@ mechanism. You'll have to try again when more details about `?T` or
172171
Regions are inferred somewhat differently from types. Rather than
173172
eagerly unifying things, we simply collect constraints as we go, but
174173
make (almost) no attempt to solve regions. These constraints have the
175-
form of an outlives constraint:
174+
form of an "outlives" constraint:
176175

177176
'a: 'b
178177

179-
Actually the code tends to view them as a subregion relation, but it's the same
178+
Actually, the code tends to view them as a subregion relation, but it's the same
180179
idea:
181180

182181
'b <= 'a
@@ -202,7 +201,7 @@ ways to solve region constraints right now: lexical and
202201
non-lexical. Eventually there will only be one.
203202

204203
To solve **lexical** region constraints, you invoke
205-
`resolve_regions_and_report_errors`. This will "close" the region
204+
`resolve_regions_and_report_errors`. This "closes" the region
206205
constraint process and invoke the `lexical_region_resolve` code. Once
207206
this is done, any further attempt to equate or create a subtyping
208207
relationship will yield an ICE.
@@ -224,4 +223,4 @@ Lexical region resolution is done by initially assigning each region
224223
variable to an empty value. We then process each outlives constraint
225224
repeatedly, growing region variables until a fixed-point is reached.
226225
Region variables can be grown using a least-upper-bound relation on
227-
the region lattice in a fairly straight-forward fashion.
226+
the region lattice in a fairly straightforward fashion.

0 commit comments

Comments
 (0)