Skip to content

Commit 885fd79

Browse files
BoxyUwUKobzol
authored andcommittedJan 5, 2025·
Split stuff out of representing types, and rewrite early/late bound chapter (#2192)
1 parent bb71c99 commit 885fd79

6 files changed

+433
-370
lines changed
 

‎src/SUMMARY.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,13 @@
134134

135135
- [Prologue](./part-4-intro.md)
136136
- [Generic parameter definitions](./generic_parameters_summary.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)
137+
- [`EarlyBinder` and instantiating parameters](./ty_module/early_binder.md)
138+
- [Binders and Higher ranked regions](./ty_module/binders.md)
139+
- [Instantiating binders](./ty_module/instantiating_binders.md)
140+
- [Early vs Late bound parameters](./early_late_parameters.md)
139141
- [The `ty` module: representing types](./ty.md)
140142
- [ADTs and Generic Arguments](./ty_module/generic_arguments.md)
141143
- [Parameter types/consts/regions](./ty_module/param_ty_const_regions.md)
142-
- [`EarlyBinder` and instantiating parameters](./ty_module/early_binder.md)
143-
- [`Binder` and Higher ranked regions](./ty_module/binders.md)
144-
- [Instantiating binders](./ty_module/instantiating_binders.md)
145144
- [`TypeFolder` and `TypeFoldable`](./ty-fold.md)
146145
- [Parameter Environments](./param_env/param_env_summary.md)
147146
- [What is it?](./param_env/param_env_what_is_it.md)

‎src/early-late-bound-params/early-late-bound-implementation-nuances.md

-197
This file was deleted.

‎src/early-late-bound-params/turbofishing-and-early-late-bound.md

-125
This file was deleted.

‎src/early_late_parameters.md

+424
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,424 @@
1+
2+
# Early vs Late bound parameters
3+
4+
<!-- toc -->
5+
6+
> **NOTE**: This chapter largely talks about early/late bound as being solely relevant when discussing function item types/function definitions. This is potentially not completely true, async blocks and closures should likely be discussed somewhat in this chapter.
7+
8+
## What does it mean to be "early" bound or "late" bound
9+
10+
Every function definition has a corresponding ZST that implements the `Fn*` traits known as a [function item type][function_item_type]. This part of the chapter will talk a little bit about the "desugaring" of function item types as it is useful context for explaining the difference between early bound and late bound generic parameters.
11+
12+
Let's start with a very trivial example involving no generic parameters:
13+
14+
```rust
15+
fn foo(a: String) -> u8 {
16+
# 1
17+
/* snip */
18+
}
19+
```
20+
21+
If we explicitly wrote out the definitions for the function item type corresponding to `foo` and its associated `Fn` impl it would look something like this:
22+
```rust,ignore
23+
struct FooFnItem;
24+
25+
impl Fn<(String,)> for FooFnItem {
26+
type Output = u8;
27+
/* fn call(&self, ...) -> ... { ... } */
28+
}
29+
```
30+
31+
The builtin impls for the `FnMut`/`FnOnce` traits as well as the impls for `Copy` and `Clone` were omitted for brevity reasons (although these traits *are* implemented for function item types).
32+
33+
A slightly more complicated example would involve introducing generic parameters to the function:
34+
```rust
35+
fn foo<T: Sized>(a: T) -> T {
36+
# a
37+
/* snip */
38+
}
39+
```
40+
Writing out the definitions would look something like this:
41+
```rust,ignore
42+
struct FooFnItem<T: Sized>(PhantomData<fn(T) -> T>);
43+
44+
impl<T: Sized> Fn<(T,)> for FooFnItem<T> {
45+
type Output = T;
46+
/* fn call(&self, ...) -> ... { ... } */
47+
}
48+
```
49+
50+
Note that the function item type `FooFnItem` is generic over some type parameter `T` as defined on the function `foo`. However, not all generic parameters defined on functions are also defined on the function item type as demonstrated here:
51+
```rust
52+
fn foo<'a, T: Sized>(a: &'a T) -> &'a T {
53+
# a
54+
/* snip */
55+
}
56+
```
57+
With its "desugared" form looking like so:
58+
```rust,ignore
59+
struct FooFnItem<T: Sized>(PhantomData<for<'a> fn(&'a T) -> &'a T>);
60+
61+
impl<'a, T: Sized> Fn<(&'a T,)> for FooFnItem<T> {
62+
type Output = &'a T;
63+
/* fn call(&self, ...) -> ... { ... } */
64+
}
65+
```
66+
67+
The lifetime parameter `'a` from the function `foo` is not present on the function item type `FooFnItem` and is instead introduced on the builtin impl solely for use in representing the argument types.
68+
69+
Generic parameters not all being defined on the function item type means that there are two steps where generic arguments are provided when calling a function.
70+
1. Naming the function (e.g. `let a = foo;`) the arguments for `FooFnItem` are provided.
71+
2. Calling the function (e.g. `a(&10);`) any parameters defined on the builtin impl are provided.
72+
73+
This two-step system is where the early vs late naming scheme comes from, early bound parameters are provided in the *earliest* step (naming the function), whereas late bound parameters are provided in the *latest* step (calling the function).
74+
75+
Looking at the desugaring from the previous example we can tell that `T` is an early bound type parameter and `'a` is a late bound lifetime parameter as `T` is present on the function item type but `'a` is not. See this example of calling `foo` annotated with where each generic parameter has an argument provided:
76+
```rust
77+
fn foo<'a, T: Sized>(a: &'a T) -> &'a T {
78+
# a
79+
/* snip */
80+
}
81+
82+
// Here we provide a type argument `String` to the
83+
// type parameter `T` on the function item type
84+
let my_func = foo::<String>;
85+
86+
// Here (implicitly) a lifetime argument is provided
87+
// to the lifetime parameter `'a` on the builtin impl.
88+
my_func(&String::new());
89+
```
90+
91+
[function_item_type]: https://doc.rust-lang.org/reference/types/function-item.html
92+
93+
## Differences between early and late bound parameters
94+
95+
### Higher ranked function pointers and trait bounds
96+
97+
A generic parameter being late bound allows for more flexible usage of the function item. For example if we have some function `foo` with an early bound lifetime parameter and some function `bar` with a late bound lifetime parameter `'a` we would have the following builtin `Fn` impls:
98+
```rust,ignore
99+
impl<'a> Fn<(&'a String,)> for FooFnItem<'a> { /* ... */ }
100+
impl<'a> Fn<(&'a String,)> for BarFnItem { /* ... */ }
101+
```
102+
103+
The `bar` function has a strictly more flexible signature as the function item type can be called with a borrow with *any* lifetime, whereas the `foo` function item type would only be callable with a borrow with the same lifetime on the function item type. We can show this by simply trying to call `foo`'s function item type multiple times with different lifetimes:
104+
105+
```rust
106+
// The `'a: 'a` bound forces this lifetime to be early bound.
107+
fn foo<'a: 'a>(b: &'a String) -> &'a String { b }
108+
fn bar<'a>(b: &'a String) -> &'a String { b }
109+
110+
// Early bound generic parameters are instantiated here when naming
111+
// the function `foo`. As `'a` is early bound an argument is provided.
112+
let f = foo::<'_>;
113+
114+
// Both function arguments are required to have the same lifetime as
115+
// the lifetime parameter being early bound means that `f` is only
116+
// callable for one specific lifetime.
117+
//
118+
// As we call this with borrows of different lifetimes, the borrow checker
119+
// will error here.
120+
f(&String::new());
121+
f(&String::new());
122+
```
123+
124+
In this example we call `foo`'s function item type twice, each time with a borrow of a temporary. These two borrows could not possible have lifetimes that overlap as the temporaries are only alive during the function call, not after. The lifetime parameter on `foo` being early bound requires all callers of `f` to provide a borrow with the same lifetime, as this is not possible the borrow checker errors.
125+
126+
If the lifetime parameter on `foo` was late bound this would be able to compile as each caller could provide a different lifetime argument for its borrow. See the following example which demonstrates this using the `bar` function defined above:
127+
128+
```rust
129+
#fn foo<'a: 'a>(b: &'a String) -> &'a String { b }
130+
#fn bar<'a>(b: &'a String) -> &'a String { b }
131+
132+
// Early bound parameters are instantiated here, however as `'a` is
133+
// late bound it is not provided here.
134+
let b = bar;
135+
136+
// Late bound parameters are instantiated separately at each call site
137+
// allowing different lifetimes to be used by each caller.
138+
b(&String::new());
139+
b(&String::new());
140+
```
141+
142+
This is reflected in the ability to coerce function item types to higher ranked function pointers and prove higher ranked `Fn` trait bounds. We can demonstrate this with the following example:
143+
```rust
144+
// The `'a: 'a` bound forces this lifetime to be early bound.
145+
fn foo<'a: 'a>(b: &'a String) -> &'a String { b }
146+
fn bar<'a>(b: &'a String) -> &'a String { b }
147+
148+
fn accepts_hr_fn(_: impl for<'a> Fn(&'a String) -> &'a String) {}
149+
150+
fn higher_ranked_trait_bound() {
151+
let bar_fn_item = bar;
152+
accepts_hr_fn(bar_fn_item);
153+
154+
let foo_fn_item = foo::<'_>;
155+
// errors
156+
accepts_hr_fn(foo_fn_item);
157+
}
158+
159+
fn higher_ranked_fn_ptr() {
160+
let bar_fn_item = bar;
161+
let fn_ptr: for<'a> fn(&'a String) -> &'a String = bar_fn_item;
162+
163+
let foo_fn_item = foo::<'_>;
164+
// errors
165+
let fn_ptr: for<'a> fn(&'a String) -> &'a String = foo_fn_item;
166+
}
167+
```
168+
169+
In both of these cases the borrow checker errors as it does not consider `foo_fn_item` to be callable with a borrow of any lifetime. This is due to the fact that the lifetime parameter on `foo` is early bound, causing `foo_fn_item` to have a type of `FooFnItem<'_>` which (as demonstrated by the desugared `Fn` impl) is only callable with a borrow of the same lifetime `'_`.
170+
171+
### Turbofishing in the presence of late bound parameters
172+
173+
As mentioned previously, the distinction between early and late bound parameters means that there are two places where generic parameters are instantiated:
174+
- When naming a function (early)
175+
- When calling a function (late)
176+
177+
There currently is no syntax for explicitly specifying generic arguments for late bound parameters as part of the call step, only specifying generic arguments when naming a function. The syntax `foo::<'static>();`, despite being part of a function call, behaves as `(foo::<'static>)();` and instantiates the early bound generic parameters on the function item type.
178+
179+
See the following example:
180+
```rust
181+
fn foo<'a>(b: &'a u32) -> &'a u32 { b }
182+
183+
let f /* : FooFnItem<????> */ = foo::<'static>;
184+
```
185+
186+
The above example errors as the lifetime parameter `'a` is late bound and so cannot be instantiated as part of the "naming a function" step. If we make the lifetime parameter early bound we will see this code start to compile:
187+
```rust
188+
fn foo<'a: 'a>(b: &'a u32) -> &'a u32 { b }
189+
190+
let f /* : FooFnItem<'static> */ = foo::<'static>;
191+
```
192+
193+
What the current implementation of the compiler aims to do is error when specifying lifetime arguments to a function that has both early *and* late bound lifetime parameters. In practice, due to excessive breakage, some cases are actually only future compatibility warnings ([#42868](https://github.com/rust-lang/rust/issues/42868)):
194+
- When the amount of lifetime arguments is the same as the number of early bound lifetime parameters a FCW is emitted instead of an error
195+
- An error is always downgraded to a FCW when using method call syntax
196+
197+
To demonstrate this we can write out the different kinds of functions and give them both a late and early bound lifetime:
198+
```rust,ignore
199+
fn free_function<'a: 'a, 'b>(_: &'a (), _: &'b ()) {}
200+
201+
struct Foo;
202+
203+
trait Trait: Sized {
204+
fn trait_method<'a: 'a, 'b>(self, _: &'a (), _: &'b ());
205+
fn trait_function<'a: 'a, 'b>(_: &'a (), _: &'b ());
206+
}
207+
208+
impl Trait for Foo {
209+
fn trait_method<'a: 'a, 'b>(self, _: &'a (), _: &'b ()) {}
210+
fn trait_function<'a: 'a, 'b>(_: &'a (), _: &'b ()) {}
211+
}
212+
213+
impl Foo {
214+
fn inherent_method<'a: 'a, 'b>(self, _: &'a (), _: &'b ()) {}
215+
fn inherent_function<'a: 'a, 'b>(_: &'a (), _: &'b ()) {}
216+
}
217+
```
218+
219+
Then, for the first case, we can call each function with a single lifetime argument (corresponding to the one early bound lifetime parameter) and note that it only results in a FCW rather than a hard error.
220+
```rust
221+
#![deny(late_bound_lifetime_arguments)]
222+
223+
#fn free_function<'a: 'a, 'b>(_: &'a (), _: &'b ()) {}
224+
#
225+
#struct Foo;
226+
#
227+
#trait Trait: Sized {
228+
# fn trait_method<'a: 'a, 'b>(self, _: &'a (), _: &'b ());
229+
# fn trait_function<'a: 'a, 'b>(_: &'a (), _: &'b ());
230+
#}
231+
#
232+
#impl Trait for Foo {
233+
# fn trait_method<'a: 'a, 'b>(self, _: &'a (), _: &'b ()) {}
234+
# fn trait_function<'a: 'a, 'b>(_: &'a (), _: &'b ()) {}
235+
#}
236+
#
237+
#impl Foo {
238+
# fn inherent_method<'a: 'a, 'b>(self, _: &'a (), _: &'b ()) {}
239+
# fn inherent_function<'a: 'a, 'b>(_: &'a (), _: &'b ()) {}
240+
#}
241+
#
242+
// Specifying as many arguments as there are early
243+
// bound parameters is always a future compat warning
244+
Foo.trait_method::<'static>(&(), &());
245+
Foo::trait_method::<'static>(Foo, &(), &());
246+
Foo::trait_function::<'static>(&(), &());
247+
Foo.inherent_method::<'static>(&(), &());
248+
Foo::inherent_function::<'static>(&(), &());
249+
free_function::<'static>(&(), &());
250+
```
251+
252+
For the second case we call each function with more lifetime arguments than there are lifetime parameters (be it early or late bound) and note that method calls result in a FCW as opposed to the free/associated functions which result in a hard error:
253+
```rust
254+
#fn free_function<'a: 'a, 'b>(_: &'a (), _: &'b ()) {}
255+
#
256+
#struct Foo;
257+
#
258+
#trait Trait: Sized {
259+
# fn trait_method<'a: 'a, 'b>(self, _: &'a (), _: &'b ());
260+
# fn trait_function<'a: 'a, 'b>(_: &'a (), _: &'b ());
261+
#}
262+
#
263+
#impl Trait for Foo {
264+
# fn trait_method<'a: 'a, 'b>(self, _: &'a (), _: &'b ()) {}
265+
# fn trait_function<'a: 'a, 'b>(_: &'a (), _: &'b ()) {}
266+
#}
267+
#
268+
#impl Foo {
269+
# fn inherent_method<'a: 'a, 'b>(self, _: &'a (), _: &'b ()) {}
270+
# fn inherent_function<'a: 'a, 'b>(_: &'a (), _: &'b ()) {}
271+
#}
272+
#
273+
// Specifying more arguments than there are early
274+
// bound parameters is a future compat warning when
275+
// using method call syntax.
276+
Foo.trait_method::<'static, 'static, 'static>(&(), &());
277+
Foo.inherent_method::<'static, 'static, 'static>(&(), &());
278+
// However, it is a hard error when not using method call syntax.
279+
Foo::trait_method::<'static, 'static, 'static>(Foo, &(), &());
280+
Foo::trait_function::<'static, 'static, 'static>(&(), &());
281+
Foo::inherent_function::<'static, 'static, 'static>(&(), &());
282+
free_function::<'static, 'static, 'static>(&(), &());
283+
```
284+
285+
Even when specifying enough lifetime arguments for both the late and early bound lifetime parameter, these arguments are not actually used to annotate the lifetime provided to late bound parameters. We can demonstrate this by turbofishing `'static` to a function while providing a non-static borrow:
286+
```rust
287+
struct Foo;
288+
289+
impl Foo {
290+
fn inherent_method<'a: 'a, 'b>(self, _: &'a (), _: &'b String ) {}
291+
}
292+
293+
Foo.inherent_method::<'static, 'static>(&(), &String::new());
294+
```
295+
296+
This compiles even though the `&String::new()` function argument does not have a `'static` lifetime, this is because "extra" lifetime arguments are discarded rather than taken into account for late bound parameters when actually calling the function.
297+
298+
### Liveness of types with late bound parameters
299+
300+
When checking type outlives bounds involving function item types we take into account early bound parameters. For example:
301+
302+
```rust
303+
fn foo<T>(_: T) {}
304+
305+
fn requires_static<T: 'static>(_: T) {}
306+
307+
fn bar<T>() {
308+
let f /* : FooFnItem<T> */ = foo::<T>;
309+
requires_static(f);
310+
}
311+
```
312+
313+
As the type parameter `T` is early bound, the desugaring of the function item type for `foo` would look something like `struct FooFnItem<T>`. Then in order for `FooFnItem<T>: 'static` to hold we must also require `T: 'static` to hold as otherwise we would wind up with soundness bugs.
314+
315+
Unfortunately, due to bugs in the compiler, we do not take into account early bound *lifetimes*, which is the cause of the open soundness bug [#84366](https://github.com/rust-lang/rust/issues/84366). This means that it's impossible to demonstrate a "difference" between early/late bound parameters for liveness/type outlives bounds as the only kind of generic parameters that are able to be late bound are lifetimes which are handled incorrectly.
316+
317+
Regardless, in theory the code example below *should* demonstrate such a difference once [#84366](https://github.com/rust-lang/rust/issues/84366) is fixed:
318+
```rust
319+
fn early_bound<'a: 'a>(_: &'a String) {}
320+
fn late_bound<'a>(_: &'a String) {}
321+
322+
fn requires_static<T: 'static>(_: T) {}
323+
324+
fn bar<'b>() {
325+
let e = early_bound::<'b>;
326+
// this *should* error but does not
327+
requires_static(e);
328+
329+
let l = late_bound;
330+
// this correctly does not error
331+
requires_static(l);
332+
}
333+
```
334+
335+
## Requirements for a parameter to be late bound
336+
337+
### Must be a lifetime parameter
338+
339+
Type and Const parameters are not able to be late bound as we do not have a way to support types such as `dyn for<T> Fn(Box<T>)` or `for<T> fn(Box<T>)`. Calling such types requires being able to monomorphize the underlying function which is not possible with indirection through dynamic dispatch.
340+
341+
### Must not be used in a where clause
342+
343+
Currently when a generic parameter is used in a where clause it must be early bound. For example:
344+
```rust
345+
# trait Trait<'a> {}
346+
fn foo<'a, T: Trait<'a>>(_: &'a String, _: T) {}
347+
```
348+
349+
In this example the lifetime parameter `'a` is considered to be early bound as it appears in the where clause `T: Trait<'a>`. This is true even for "trivial" where clauses such as `'a: 'a` or those implied by wellformedness of function arguments, for example:
350+
```rust
351+
fn foo<'a: 'a>(_: &'a String) {}
352+
fn bar<'a, T: 'a>(_: &'a T) {}
353+
```
354+
355+
In both of these functions the lifetime parameter `'a` would be considered to be early bound even though the where clauses they are used in arguably do not actually impose any constraints on the caller.
356+
357+
The reason for this restriction is a combination of two things:
358+
- We cannot prove bounds on late bound parameters until they have been instantiated
359+
- Function pointers and trait objects do not have a way to represent yet to be proven where clauses from the underlying function
360+
361+
Take the following example:
362+
```rust
363+
trait Trait<'a> {}
364+
fn foo<'a, T: Trait<'a>>(_: &'a T) {}
365+
366+
let f = foo::<String>;
367+
let f = f as for<'a> fn(&'a String);
368+
f(&String::new());
369+
```
370+
371+
At *some point* during type checking an error should be emitted for this code as `String` does not implement `Trait` for any lifetime.
372+
373+
If the lifetime `'a` were late bound then this becomes difficult to check. When naming `foo` we do not know what lifetime should be used as part of the `T: Trait<'a>` trait bound as it has not yet been instantiated. When coercing the function item type to a function pointer we have no way of tracking the `String: Trait<'a>` trait bound that must be proven when calling the function.
374+
375+
If the lifetime `'a` is early bound (which it is in the current implementation in rustc), then the trait bound can be checked when naming the function `foo`. Requiring parameters used in where clauses to be early bound gives a natural place to check where clauses defined on the function.
376+
377+
Finally, we do not require lifetimes to be early bound if they are used in *implied bounds*, for example:
378+
```rust
379+
fn foo<'a, T>(_: &'a T) {}
380+
381+
let f = foo;
382+
f(&String::new());
383+
f(&String::new());
384+
```
385+
386+
This code compiles, demonstrating that the lifetime parameter is late bound, even though `'a` is used in the type `&'a T` which implicitly requires `T: 'a` to hold. Implied bounds can be treated specially as any types introducing implied bounds are in the signature of the function pointer type, which means that when calling the function we know to prove `T: 'a`.
387+
388+
### Must be constrained by argument types
389+
390+
It is important that builtin impls on function item types do not wind up with unconstrained generic parameters as this can lead to unsoundness. This is the same kind of restriction as applies to user written impls, for example the following code results in an error:
391+
```rust
392+
trait Trait {
393+
type Assoc;
394+
}
395+
396+
impl<'a> Trait for u8 {
397+
type Assoc = &'a String;
398+
}
399+
```
400+
401+
The analogous example for builtin impls on function items would be the following:
402+
```rust,ignore
403+
fn foo<'a>() -> &'a String { /* ... */ }
404+
```
405+
If the lifetime parameter `'a` were to be late bound we would wind up with a builtin impl with an unconstrained lifetime, we can manually write out the desugaring for the function item type and its impls with `'a` being late bound to demonstrate this:
406+
```rust,ignore
407+
// NOTE: this is just for demonstration, in practice `'a` is early bound
408+
struct FooFnItem;
409+
410+
impl<'a> Fn<()> for FooFnItem {
411+
type Output = &'a String;
412+
/* fn call(...) -> ... { ... } */
413+
}
414+
```
415+
416+
In order to avoid such a situation we consider `'a` to be early bound which causes the lifetime on the impl to be constrained by the self type:
417+
```rust,ignore
418+
struct FooFnItem<'a>(PhantomData<fn() -> &'a String>);
419+
420+
impl<'a> Fn<()> for FooFnItem<'a> {
421+
type Output = &'a String;
422+
/* fn call(...) -> ... { ... } */
423+
}
424+
```

‎src/generic_parameters_summary.md

+1-39
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,4 @@ Interestingly, `ty::Generics` does not currently contain _every_ generic paramet
2525
[ch_representing_types]: ./ty.md
2626
[`ty::Generics`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Generics.html
2727
[`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
66-
[ch_binders]: ./ty_module/binders.md
28+
[ch_binders]: ./ty_module/binders.md

‎src/ty_module/early_binder.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ fn bar(foo: Foo<u32, f32>) {
4747

4848
In the compiler the `instantiate` call for this is done in [`FieldDef::ty`] ([src][field_def_ty_src]), at some point during type checking `bar` we will wind up calling `FieldDef::ty(x, &[u32, f32])` in order to obtain the type of `foo.x`.
4949

50-
**Note on indices:** It is possible for the indices in `Param` to not match with what the `EarlyBinder` binds. For
51-
example, the index could be out of bounds or it could be the index of a lifetime when we were expecting a type.
52-
These sorts of errors would be caught earlier in the compiler when translating from a `rustc_hir::Ty` to a `ty::Ty`.
53-
If they occur later, that is a compiler bug.
50+
**Note on indices:** It is a bug if the index of a `Param` does not match what the `EarlyBinder` binds. For
51+
example, if the index is out of bounds or the index index of a lifetime corresponds to a type parameter.
52+
These sorts of errors are caught earlier in the compiler during name resolution where we disallow references
53+
to generics parameters introduced by items that should not be nameable by the inner item.
5454

5555
[`FieldDef::ty`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.FieldDef.html#method.ty
5656
[field_def_ty_src]: https://github.com/rust-lang/rust/blob/44d679b9021f03a79133021b94e6d23e9b55b3ab/compiler/rustc_middle/src/ty/mod.rs#L1421-L1426

0 commit comments

Comments
 (0)
Please sign in to comment.