Skip to content

Commit ea35ee8

Browse files
committed
Introduce chapter for defining generic parameters
1 parent b71f723 commit ea35ee8

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Diff for: src/SUMMARY.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,11 @@
114114
# Analysis
115115

116116
- [Prologue](./part-4-intro.md)
117-
- [The `ty` module: representing types](./ty.md)
118-
- [Generics and substitutions](./generics.md)
117+
- [Generic parameter definitions](./generic_parameters_summary.md)
118+
- [What is `ty::Generics`](./what_is_ty_generics.md)
119+
- [Early vs Late bound parameters](./early-late-bound-params/early-late-bound-summary.md)
120+
- [Implementation nuances of early/late bound parameters](./early-late-bound-params/early-late-bound-implementation-nuances.md)
121+
- [Interactions with turbofishing](./early-late-bound-params/turbofishing-and-early-late-bound.md)
119122
- [`TypeFolder` and `TypeFoldable`](./ty-fold.md)
120123
- [Generic arguments](./generic_arguments.md)
121124
- [Constants in the type system](./constants.md)

Diff for: src/generic_parameters_summary.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Generic parameter definitions
2+
3+
This chapter will discuss how rustc tracks what generic parameters are introduced by an item. For example given some struct defined via `struct Foo<T>` how does rustc track that `Foo` defines some type parameter `T` and nothing else?
4+
5+
This will *not* cover how we track generic parameters introduced via `for<'a>` syntax (i.e. in where clauses or `fn` types), which is covered elsewhere in the [chapter on `Binder`s ][ch_binders].
6+
7+
[ch_binders]: ./ty_module/binders.md

Diff for: src/what_is_ty_generics.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# What is `ty::Generics`
2+
3+
The generic parameters introduced by an item are tracked the [`ty::Generics`] struct. Sometimes items allows usage of generics of parent items inside of them, this is accomplished via the `ty::Generics` struct having an optional field to specify a parent item to inherit generic parameters of. For example given the following code:
4+
5+
```rust,ignore
6+
trait Trait<T> {
7+
fn foo<U>(&self);
8+
}
9+
```
10+
11+
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`.
12+
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).
14+
15+
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.
16+
17+
[`ty::Generics`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Generics.html
18+
[`GenericParamDef`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/generics/struct.GenericParamDef.html

0 commit comments

Comments
 (0)