Skip to content

Commit bf30610

Browse files
committed
Reviews
1 parent 5d808a5 commit bf30610

7 files changed

+17
-10
lines changed

src/ty.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ The `ty` module defines how the Rust compiler represents types internally. It al
1010
When we talk about how rustc represents types, we usually refer to a type called `Ty` . There are
1111
quite a few modules and types for `Ty` in the compiler ([Ty documentation][ty]).
1212

13-
[ty]: https://doc.rust-lang.org/nightly/nightly-rustc/ru
14-
]stc_middle/ty/index.html
13+
[ty]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/index.html
1514

1615
The specific `Ty` we are referring to is [`rustc_middle::ty::Ty`][ty_ty] (and not
1716
[`rustc_hir::Ty`][hir_ty]). The distinction is important, so we will discuss it first before going

src/ty_module/binders.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Binder(
4444
&[BoundVariarbleKind::Region(...)],
4545
)
4646
```
47-
This would cause all kinds of issues as the region `'^1_0` refers to a binder at a higher level than the outtermost binder i.e. it is an escaping bound var. The `'^1` region (also writeable as `'^0_1`) is also ill formed as the binder it refers to does not introduce a second parameter. Modern day rustc will ICE when constructing this binder due to both of those regions, in the past we would have simply allowed this to work and then ran into issues in other parts of the codebase.
47+
This would cause all kinds of issues as the region `'^1_0` refers to a binder at a higher level than the outermost binder i.e. it is an escaping bound var. The `'^1` region (also writeable as `'^0_1`) is also ill formed as the binder it refers to does not introduce a second parameter. Modern day rustc will ICE when constructing this binder due to both of those regions, in the past we would have simply allowed this to work and then ran into issues in other parts of the codebase.
4848

4949
[`Binder`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Binder.html
5050
[`BoundVar]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.BoundVar.html

src/ty_module/early_binder.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn main() {
1010
}
1111
```
1212

13-
When type checking `main` we cannot just naively look at the return type of `foo` and assign the type `T` to the variable `a`, after all the function `main` does not define any generic parameters, `T` is completely meaningless in this context. More generally whenever an item introduces (binds) generic parameters, when accessing types inside the item from outside, the generic parameters must be instantiated with values from the outer item.
13+
When type checking `main` we cannot just naively look at the return type of `foo` and assign the type `T` to the variable `c`, after all the function `main` does not define any generic parameters, `T` is completely meaningless in this context. More generally whenever an item introduces (binds) generic parameters, when accessing types inside the item from outside, the generic parameters must be instantiated with values from the outer item.
1414

1515
In rustc we track this via the [`EarlyBinder`] type, the return type of `foo` is represented as an `EarlyBinder<Ty>` with the only way to acess `Ty` being to provide arguments for any generic parameters `Ty` might be using. This is implemented via the [`EarlyBinder::instantiate`] method which discharges the binder returning the inner value with all the generic parameters replaced by the provided arguments.
1616

src/ty_module/generic_arguments.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ADTs and Generic Arguments
22

3+
The term `ADT` stands for "Algebraic data type", in rust this refers to a struct, enum, or union.
4+
35
## ADTs Representation
46

57
Let's consider the example of a type like `MyStruct<u32>`, where `MyStruct` is defined like so:
@@ -123,4 +125,4 @@ For the `MyStruct<U>` written in the `Foo` type alias, we would represent it in
123125

124126
- There would be an `AdtDef` (and corresponding `DefId`) for `MyStruct`.
125127
- There would be a `GenericArgs` containing the list `[GenericArgKind::Type(Ty(u32))]`
126-
- This is one `TyKind::Adt` containing the `AdtDef` of `MyStruct` with the `GenericArgs` above.
128+
- And finally a `TyKind::Adt` with the `AdtDef` and `GenericArgs` listed above.

src/ty_module/instantiating_binders.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Instantiating binders with inference variables can be accomplished by using the
3434

3535
## Instantiating with placeholders
3636

37-
Placeholders are very similar to `Ty/ConstKind::Param`/`ReEarlyParam`, they represent some unknown type that is only equal to itself. `Ty`/`Const` and `Region` all have a `Placeholder` variant that is comprised of a `Universe` and a `BoundVar`.
37+
Placeholders are very similar to `Ty/ConstKind::Param`/`ReEarlyParam`, they represent some unknown type that is only equal to itself. `Ty`/`Const` and `Region` all have a [`Placeholder`] variant that is comprised of a [`Universe`] and a [`BoundVar`].
3838

3939
The `Universe` tracks which binder the placeholder originated from, and the `BoundVar` tracks which parameter on said binder that this placeholder corresponds to. Equality of placeholders is determined solely by whether the universes are equal and the `BoundVar`s are equal. See the [chapter on Placeholders and Universes][ch_placeholders_universes] for more information.
4040

@@ -139,4 +139,7 @@ As a concrete example, accessing the signature of a function we are type checkin
139139
[`instantiate_binder_with_fresh_vars`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_trait_selection/infer/struct.InferCtxt.html#method.instantiate_binder_with_fresh_vars
140140
[`InferCtxt`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_trait_selection/infer/struct.InferCtxt.html
141141
[`EarlyBinder`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.EarlyBinder.html
142-
[`Binder`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/type.Binder.html
142+
[`Binder`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/type.Binder.html
143+
[`Placeholder`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Placeholder.html
144+
[`Universe`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.UniverseIndex.html
145+
[`BoundVar`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.BoundVar.html

src/ty_module/param_ty_const_regions.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct Foo<T>(Vec<T>);
3131
The `Vec<T>` type is represented as `TyKind::Adt(Vec, &[GenericArgKind::Type(Param("T", 0))])`.
3232

3333
The name is somewhat self explanatory, it's the name of the type parameter. The index of the type parameter is an integer indicating
34-
its order in the list of generic parameters in scope (note: this includes parameters defined on items on outter scopes than the item the parameter is defined on). Consider the following examples:
34+
its order in the list of generic parameters in scope (note: this includes parameters defined on items on outer scopes than the item the parameter is defined on). Consider the following examples:
3535

3636
```rust,ignore
3737
struct Foo<A, B> {
@@ -50,7 +50,7 @@ impl<X, Y> Foo<X, Y> {
5050
}
5151
```
5252

53-
Concretely given the `ty::Generics` for the item the parameter is defined on, if the index is `10` then starting from the root `parent`, it will be the eleventh parameter to be introduced.
53+
Concretely given the `ty::Generics` for the item the parameter is defined on, if the index is `2` then starting from the root `parent`, it will be the third parameter to be introduced. For example in the above example, `Z` has index `2` and is the third generic parameter to be introduced, starting from the `impl` block.
5454

5555
The index fully defines the `Ty` and is the only part of `TyKind::Param` that matters for reasoning about the code we are compiling.
5656

src/what_is_ty_generics.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ trait Trait<T> {
1010

1111
The `ty::Generics` used for `foo` would contain `[U]` and a parent of `Some(Trait)`. `Trait` would have a `ty::Generics` containing `[Self, T]` with a parent of `None`.
1212

13-
The [`GenericParamDef`] struct is used to represent each individual generic parameter in a `ty::Generics` listing. The `GenericParamDef` struct contains information about the generic parameter, for example its name, defid, what kind of parameter it is (i.e. type, const, lifetime). It also contains a `u32` index representing what position the parameter is (starting from the outtermost parent).
13+
The [`GenericParamDef`] struct is used to represent each individual generic parameter in a `ty::Generics` listing. The `GenericParamDef` struct contains information about the generic parameter, for example its name, defid, what kind of parameter it is (i.e. type, const, lifetime).
14+
15+
`GenericParamDef` also contains a `u32` index representing what position the parameter is (starting from the outermost parent), this is the value used to represent usages of generic parameters (more on this in the [chapter on representing types][ch_representing_types]).
1416

1517
Interestingly, `ty::Generics` does not currently contain _every_ generic parameter defined on an item. In the case of functions it only contains the _early bound_ lifetime parameters. See the next chapter for information on what "early bound" and "late bound" parameters are.
1618

19+
[ch_representing_types]: ./ty.md
1720
[`ty::Generics`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Generics.html
1821
[`GenericParamDef`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/generics/struct.GenericParamDef.html

0 commit comments

Comments
 (0)