Skip to content

Commit 7cc2afa

Browse files
varkormark-i-m
authored andcommitted
Add preliminary chapter on kinds
1 parent de2ab38 commit 7cc2afa

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
- [The HIR (High-level IR)](./hir.md)
3939
- [Lowering AST to HIR](./lowering.md)
4040
- [The `ty` module: representing types](./ty.md)
41+
- [Kinds](./kinds.md)
4142
- [Type inference](./type-inference.md)
4243
- [Trait solving (old-style)](./traits/resolution.md)
4344
- [Higher-ranked trait bounds](./traits/hrtb.md)

src/kinds.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Kinds
2+
A `ty::subst::Kind<'tcx>` represents some entity in the type system: currently
3+
either a type (`Ty<'tcx>`) or a lifetime (`ty::Region<'tcx>`), though in the
4+
future this will also include constants (`ty::Const<'tcx>`) to facilitate the
5+
use of const generics. `Kind` is used for type and lifetime substitution (from
6+
abstract type and lifetime parameters to concrete types and lifetimes).
7+
8+
## `UnpackedKind`
9+
As `Kind` itself is not type-safe (see [`Kind`](#kind)), the `UnpackedKind` enum
10+
provides a more convenient and safe interface for dealing with kinds. To
11+
convert from an `UnpackedKind` to a `Kind`, you can call `Kind::from` (or
12+
`.into`). It should not be necessary to convert a `Kind` to an `UnpackedKind`:
13+
instead, you should prefer to deal with `UnpackedKind`, converting it only when
14+
passing it to `Subst` methods.
15+
16+
## `Kind`
17+
The actual `Kind` struct is optimised for space, storing the type or lifetime
18+
as an interned pointer containing a mask identifying its kind (in the lowest
19+
2 bits).
20+
21+
## `Subst`
22+
`ty::subst::Subst<'tcx>` is simply defined as a slice of `Kind<'tcx>`s
23+
and acts as an ordered list of substitutions from kind parameters (i.e.
24+
type and lifetime parameters) to kinds.
25+
26+
For example, given a `HashMap<K, V>` with two type parameters, `K` and `V`, an
27+
instantiation of the parameters, for example `HashMap<i32, u32>`, would be
28+
represented by the substitution `&'tcx [tcx.types.i32, tcx.types.u32]`.
29+
30+
`Subst` provides various convenience methods to instantiant substitutions
31+
given item definitions.

src/ty.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ In addition to types, there are a number of other arena-allocated data
141141
structures that you can allocate, and which are found in this
142142
module. Here are a few examples:
143143

144-
- `Substs`, allocated with `mk_substs` – this will intern a slice of types,
145-
often used to specify the values to be substituted for generics
144+
- [`Substs`][subst], allocated with `mk_substs` – this will intern a slice of
145+
types, often used to specify the values to be substituted for generics
146146
(e.g. `HashMap<i32, u32>` would be represented as a slice
147147
`&'tcx [tcx.types.i32, tcx.types.u32]`).
148148
- `TraitRef`, typically passed by value – a **trait reference**
@@ -153,6 +153,8 @@ module. Here are a few examples:
153153
- `Predicate` defines something the trait system has to prove (see `traits`
154154
module).
155155

156+
[subst]: ./kinds.html#subst
157+
156158
### Import conventions
157159

158160
Although there is no hard and fast rule, the `ty` module tends to be used like

0 commit comments

Comments
 (0)