Skip to content

Commit 47d9e86

Browse files
committed
Flatten generic parameter defs section
1 parent 5a76e97 commit 47d9e86

7 files changed

+88
-68
lines changed

Diff for: src/SUMMARY.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,8 @@
134134

135135
- [Prologue](./part-4-intro.md)
136136
- [Generic parameter definitions](./generic_parameters_summary.md)
137-
- [What is `ty::Generics`](./what_is_ty_generics.md)
138-
- [Early vs Late bound parameters](./early-late-bound-params/early-late-bound-summary.md)
139-
- [Implementation nuances of early/late bound parameters](./early-late-bound-params/early-late-bound-implementation-nuances.md)
140-
- [Interactions with turbofishing](./early-late-bound-params/turbofishing-and-early-late-bound.md)
137+
- [Implementation nuances of early/late bound parameters](./early-late-bound-params/early-late-bound-implementation-nuances.md)
138+
- [Interactions with turbofishing](./early-late-bound-params/turbofishing-and-early-late-bound.md)
141139
- [The `ty` module: representing types](./ty.md)
142140
- [ADTs and Generic Arguments](./ty_module/generic_arguments.md)
143141
- [Parameter types/consts/regions](./ty_module/param_ty_const_regions.md)

Diff for: src/early-late-bound-params/early-late-bound-implementation-nuances.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Early and Late Bound Parameter Implementation Nuances
22

3+
> Note: this chapter makes reference to information discussed later on in the [representing types][ch_representing_types] chapter. Specifically, it uses concise notation to represent some more complex kinds of types that have not yet been discussed, such as inference variables.
4+
5+
[ch_representing_types]: ../ty.md
6+
37
Understanding this page likely requires a rudimentary understanding of higher ranked
48
trait bounds/`for<'a>`and also what types such as `dyn for<'a> Trait<'a>` and
59
`for<'a> fn(&'a u32)` mean. Reading [the nomincon chapter](https://doc.rust-lang.org/nomicon/hrtb.html)
@@ -41,13 +45,13 @@ fn foo_late<'a, T>(_: &'a u32, _: T) {}
4145
fn accepts_hr_func<F: for<'a> Fn(&'a u32, u32)>(_: F) {}
4246

4347
fn main() {
44-
// doesn't work, the substituted bound is `for<'a> FnDef<'?0>: Fn(&'a u32, u32)`
48+
// doesn't work, the instantiated bound is `for<'a> FnDef<'?0>: Fn(&'a u32, u32)`
4549
// `foo_early` only implements `for<'a> FnDef<'a>: Fn(&'a u32, u32)`- the lifetime
4650
// of the borrow in the function argument must be the same as the lifetime
4751
// on the `FnDef`.
4852
accepts_hr_func(foo_early);
4953

50-
// works, the substituted bound is `for<'a> FnDef: Fn(&'a u32, u32)`
54+
// works, the instantiated bound is `for<'a> FnDef: Fn(&'a u32, u32)`
5155
accepts_hr_func(foo_late);
5256
}
5357

Diff for: src/early-late-bound-params/early-late-bound-summary.md

-35
This file was deleted.

Diff for: src/early-late-bound-params/turbofishing-and-early-late-bound.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Turbofishing's interactions with early/late bound parameters
22

3+
> Note: this chapter makes reference to information discussed later on in the [representing types][ch_representing_types] chapter. Specifically, it uses concise notation to represent some more complex kinds of types that have not yet been discussed, such as inference variables.
4+
5+
[ch_representing_types]: ../ty.md
6+
37
The early/late bound parameter distinction on functions introduces some complications
48
when providing generic arguments to functions. This document discusses what those are
59
and how they might interact with future changes to make more things late bound.
@@ -36,7 +40,7 @@ fn main() {
3640
```
3741

3842
The justification for this is that late bound parameters are not present on the
39-
`FnDef` so the arguments to late bound parameters can't be present in the substs
43+
`FnDef` so the arguments to late bound parameters can't be present in the generic arguments
4044
for the type. i.e. the `late` function in the above code snippet would not have
4145
any generic parameters on the `FnDef` zst:
4246
```rust
@@ -52,7 +56,7 @@ The cause for some situations giving future compat lints and others giving hard
5256
is a little arbitrary but explainable:
5357
- It's always a hard error for method calls
5458
- It's only a hard error on paths to free functions if there is no unambiguous way to
55-
create the substs for the fndef from the lifetime arguments. (i.e. the amount of
59+
create the generic arguments for the fndef from the lifetime arguments. (i.e. the amount of
5660
lifetimes provided must be exactly equal to the amount of early bound lifetimes or
5761
else it's a hard error)
5862

@@ -107,8 +111,9 @@ fn late<'a, 'b>(_: &'a (), _: &'b ()) {}
107111
fn accepts_fn(_: impl for<'a> Fn(&'a (), &'static ())) {}
108112

109113
fn main() {
110-
// a naive implementation would have a `ReInfer` as the subst for `'a` parameter
111-
// no longer allowing the FnDef to satisfy the `for<'a> Fn(&'a ()` bound
114+
// a naive implementation would have an inference variable as
115+
// the argument to the `'a` parameter no longer allowing the `FnDef`
116+
// to satisfy the bound `for<'a> Fn(&'a ())`
112117
let f = late::<'_, 'static>;
113118
accepts_fn(f);
114119
}

Diff for: src/generic_parameters_summary.md

+61-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,66 @@
11
# Generic parameter definitions
22

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?
3+
This chapter will discuss how rustc tracks what generic parameters are introduced. For example given some `struct Foo<T>` how does rustc track that `Foo` defines some type parameter `T` (and no other generic parameters).
44

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].
5+
This will *not* cover how we track generic parameters introduced via `for<'a>` syntax (e.g. in where clauses or `fn` types), which is covered elsewhere in the [chapter on `Binder`s ][ch_binders].
66

7+
# `ty::Generics`
8+
9+
The generic parameters introduced by an item are tracked by the [`ty::Generics`] struct. Sometimes items allow usage of generics defined on parent items, 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:
10+
11+
```rust,ignore
12+
trait Trait<T> {
13+
fn foo<U>(&self);
14+
}
15+
```
16+
17+
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`.
18+
19+
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).
20+
21+
`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]).
22+
23+
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_ parameters.
24+
25+
[ch_representing_types]: ./ty.md
26+
[`ty::Generics`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Generics.html
27+
[`GenericParamDef`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/generics/struct.GenericParamDef.html
28+
29+
# Early vs Late bound parameters
30+
31+
32+
```rust
33+
fn foo<'a, T>(b: &'a T) -> &'a T { b }
34+
// ^^ ^early bound
35+
// ^^
36+
// ^^late bound
37+
```
38+
39+
Generally when referring to an item with generic parameters you must specify a list of generic arguments corresponding to the item's generic parameters. In some cases it is permitted to elide these arguments but still, implicitly, a set of arguments are provided (e.g. `Vec::default()` desugars to `Vec::<_>::default()`).
40+
41+
For functions this is not necessarily the case, for example if we take the function `foo` from the example above and write the following code:
42+
```rust
43+
fn main() {
44+
let f = foo::<_>;
45+
46+
let b = String::new();
47+
let c = String::new();
48+
49+
f(&b);
50+
drop(b);
51+
f(&c);
52+
}
53+
```
54+
55+
This code compiles perfectly fine even though there is no single lifetime that could possibly be specified in `foo::<_>` that would allow for both
56+
the `&b` and `&c` borrows to be used as arguments (note: the `drop(b)` line forces the `&b` borrow to be shorter than the `&c` borrow). This works because the `'a` lifetime is _late bound_.
57+
58+
A generic parameter being late bound means that when we write `foo::<_>` we do not actually provide an argument for that parameter, instead we wait until _calling_ the function to provide the generic argument. In the above example this means that we are doing something like `f::<'_>(&b);` and `f::<'_>(&c);` (although in practice we do not actually support turbofishing late bound parameters in this manner)
59+
60+
It may be helpful to think of "early bound parameter" or "late bound parameter" as meaning "early provided parameter" and "late provided parameter", i.e. we provide the argument to the parameter either early (when naming the function) or late (when calling it).
61+
62+
Late bound parameters on functions are tracked with a [`Binder`] when accessing the signature of the function, this can be done with the [`fn_sig`] query. For more information of binders see the [chapter on `Binder`s ][ch_binders].
63+
64+
[`Binder`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_type_ir/binder/struct.Binder.html
65+
[`fn_sig`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.fn_sig
766
[ch_binders]: ./ty_module/binders.md

Diff for: src/ty.md

+10
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,13 @@ a redundant delayed bug.
314314

315315
[terr]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Ty.html#method.new_error
316316
[terrmsg]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Ty.html#method.new_error_with_message
317+
318+
319+
## `TyKind` variant shorthand syntax
320+
321+
When looking at the debug output of `Ty` or simply talking about different types in the compiler, you may encounter syntax that is not valid rust but is used to concisely represent internal information about types. Below is a quick reference cheat sheet to tell what the various syntax actually means, these should be covered in more depth in later chapters.
322+
323+
- Generic parameters: `{name}/#{index}` e.g. `T/#0`, where `index` corresponds to its position in the list of generic parameters
324+
- Inference variables: `?{id}` e.g. `?x`/`?0`, where `id` identifies the inference variable
325+
- Variables from binders: `^{binder}_{index}` e.g. `^0_x`/`^0_2`, where `binder` and `index` identify which variable from which binder is being referred to
326+
- Placeholders: `!{id}` or `!{id}_{universe}` e.g. `!x`/`!0`/`!x_2`/`!0_2`, representing some unique type in the specified universe. The universe is often elided when it is `0`

Diff for: src/what_is_ty_generics.md

-21
This file was deleted.

0 commit comments

Comments
 (0)