Skip to content

Commit 8d1e853

Browse files
committed
improve ty-module/param-ty-const-regions.md
1 parent 2c7d051 commit 8d1e853

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/ty-module/param-ty-const-regions.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Parameter `Ty`/`Const`/`Region`s
22

33
When inside of generic items, types can be written that use in scope generic parameters, for example `fn foo<'a, T>(_: &'a Vec<T>)`.
4-
In this specific case the `&'a Vec<T>` type would be represented internally as:
4+
In this specific case, the `&'a Vec<T>` type would be represented internally as:
55
```
66
TyKind::Ref(
77
RegionKind::LateParam(DefId(foo), DefId(foo::'a), "'a"),
@@ -29,9 +29,10 @@ struct Foo<T>(Vec<T>);
2929
```
3030
The `Vec<T>` type is represented as `TyKind::Adt(Vec, &[GenericArgKind::Type(Param("T", 0))])`.
3131

32-
The name is somewhat self explanatory, it's the name of the type parameter.
32+
The name is somewhat self explanatory; it's the name of the type parameter.
3333
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 outer scopes than the item the parameter is defined on).
34+
its order in the list of generic parameters in scope.
35+
Note that this includes parameters defined on items on outer scopes than the item the parameter is defined on.
3536
Consider the following examples:
3637

3738
```rust,ignore
@@ -51,17 +52,19 @@ impl<X, Y> Foo<X, Y> {
5152
}
5253
```
5354

54-
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.
55+
Concretely, given the `ty::Generics` for the item the parameter is defined on,
56+
if the index is `2` and if we start from the root `parent`, it will be the third parameter to be introduced.
5557
For example in the above example, `Z` has index `2` and is the third generic parameter to be introduced, starting from the `impl` block.
5658

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

59-
Generally we do not care what the name is and only use the index.
61+
Generally, we do not care what the name is, and we only use the index.
6062
The name is included for diagnostics and debug logs as otherwise it would be
6163
incredibly difficult to understand the output, i.e. `Vec<Param(0)>: Sized` vs `Vec<T>: Sized`. In debug output, parameter types are
6264
often printed out as `{name}/#{index}`, for example in the function `foo` if we were to debug print `Vec<T>` it would be written as `Vec<T/#0>`.
6365

64-
An alternative representation would be to only have the name, however using an index is more efficient as it means we can index into `GenericArgs` when instantiating generic parameters with some arguments.
66+
An alternative representation would be to only have the name.
67+
However, using an index is more efficient as it means we can index into `GenericArgs` when instantiating generic parameters with some arguments.
6568
We would otherwise have to store `GenericArgs` as a `HashMap<Symbol, GenericArg>` and do a hashmap lookup everytime we used a generic item.
6669

6770
In theory an index would also allow for having multiple distinct parameters that use the same name, e.g.
@@ -73,10 +76,12 @@ The rules against shadowing make this difficult but those language rules could c
7376
In contrast to `Ty`/`Const`'s `Param` singular `Param` variant, lifetimes have two variants for representing region parameters: [`RegionKind::EarlyParam`] and [`RegionKind::LateParam`].
7477
The reason for this is due to function's distinguishing between [early and late bound parameters][ch_early_late_bound] which is discussed in an earlier chapter (see link).
7578

76-
`RegionKind::EarlyParam` is structured identically to `Ty/Const`'s `Param` variant, it is simply a `u32` index and a `Symbol`.
77-
For lifetime parameters defined on non-function items we always use `ReEarlyParam`.
78-
For functions we use `ReEarlyParam` for any early bound parameters and `ReLateParam` for any late bound parameters.
79-
Note that just like `Ty` and `Const` params we often debug format them as `'SYMBOL/#INDEX`, see for example:
79+
`RegionKind::EarlyParam` is structured identically to `Ty/Const`'s `Param` variant; it is simply a `u32` index and a `Symbol`.
80+
For lifetime parameters defined on non-function items, we always use `ReEarlyParam`.
81+
For functions, we use `ReEarlyParam` for any early bound parameters and `ReLateParam` for any late bound parameters.
82+
Note that, just like `Ty` and `Const` params, we often debug format them as `'SYMBOL/#INDEX`.
83+
84+
An example:
8085

8186
```rust,ignore
8287
// This function would have its signature represented as:

0 commit comments

Comments
 (0)