You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/ty-module/instantiating-binders.md
+40-19Lines changed: 40 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
# Instantiating `Binder`s
2
2
3
-
Much like [`EarlyBinder`], when accessing the inside of a [`Binder`] we must first discharge it by replacing the bound vars with some other value. This is for much the same reason as with `EarlyBinder`, types referencing parameters introduced by the `Binder` do not make any sense outside of that binder. See the following erroring example:
3
+
Much like [`EarlyBinder`], when accessing the inside of a [`Binder`] we must first discharge it by replacing the bound vars with some other value.
4
+
This is for much the same reason as with `EarlyBinder`, types referencing parameters introduced by the `Binder` do not make any sense outside of that binder.
5
+
See the following erroring example:
4
6
```rust,ignore
5
7
fn foo<'a>(a: &'a u32) -> &'a u32 {
6
8
a
@@ -15,7 +17,8 @@ fn main() {
15
17
let references_bound_vars = bar(higher_ranked_fn_ptr);
16
18
}
17
19
```
18
-
In this example we are providing an argument of type `for<'a> fn(&'^0 u32) -> &'^0 u32` to `bar`, we do not want to allow `T` to be inferred to the type `&'^0 u32` as it would be rather nonsensical (and likely unsound if we did not happen to ICE). `main` doesn't know about `'a` so the borrow checker would not be able to handle a borrow with lifetime `'a`.
20
+
In this example we are providing an argument of type `for<'a> fn(&'^0 u32) -> &'^0 u32` to `bar`, we do not want to allow `T` to be inferred to the type `&'^0 u32` as it would be rather nonsensical (and likely unsound if we did not happen to ICE).
21
+
`main` doesn't know about `'a` so the borrow checker would not be able to handle a borrow with lifetime `'a`.
19
22
20
23
Unlike `EarlyBinder` we typically do not instantiate `Binder` with some concrete set of arguments from the user, i.e. `['b, 'static]` as arguments to a `for<'a1, 'a2> fn(&'a1 u32, &'a2 u32)`. Instead we usually instantiate the binder with inference variables or placeholders.
21
24
@@ -31,27 +34,35 @@ As another example of instantiating with infer vars, given some `for<'a> T: Trai
31
34
- Equate the goal of `T: Trait<'static>` with the instantiated where clause, inferring `'?0 = 'static`
32
35
- The goal holds because we were successfully able to unify `T: Trait<'static>` with `T: Trait<'?0>`
33
36
34
-
Instantiating binders with inference variables can be accomplished by using the [`instantiate_binder_with_fresh_vars`] method on [`InferCtxt`]. Binders should be instantiated with infer vars when we only care about one specific instantiation of the binder, if instead we wish to reason about all possible instantiations of the binder then placeholders should be used instead.
37
+
Instantiating binders with inference variables can be accomplished by using the [`instantiate_binder_with_fresh_vars`] method on [`InferCtxt`].
38
+
Binders should be instantiated with infer vars when we only care about one specific instantiation of the binder, if instead we wish to reason about all possible instantiations of the binder then placeholders should be used instead.
35
39
36
40
## Instantiating with placeholders
37
41
38
-
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`].
42
+
Placeholders are very similar to `Ty/ConstKind::Param`/`ReEarlyParam`, they represent some unknown type that is only equal to itself.
43
+
`Ty`/`Const` and `Region` all have a [`Placeholder`] variant that is comprised of a [`Universe`] and a [`BoundVar`].
39
44
40
-
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.
45
+
The `Universe` tracks which binder the placeholder originated from, and the `BoundVar` tracks which parameter on said binder that this placeholder corresponds to.
46
+
Equality of placeholders is determined solely by whether the universes are equal and the `BoundVar`s are equal.
47
+
See the [chapter on Placeholders and Universes][ch_placeholders_universes] for more information.
41
48
42
-
When talking with other rustc devs or seeing `Debug` formatted `Ty`/`Const`/`Region`s, `Placeholder` will often be written as `'!UNIVERSE_BOUNDVARS`. For example given some type `for<'a> fn(&'a u32, for<'b> fn(&'b &'a u32))`, after instantiating both binders (assuming the `Universe` in the current `InferCtxt` was `U0` beforehand), the type of `&'b &'a u32` would be represented as `&'!2_0 &!1_0 u32`.
49
+
When talking with other rustc devs or seeing `Debug` formatted `Ty`/`Const`/`Region`s, `Placeholder` will often be written as `'!UNIVERSE_BOUNDVARS`.
50
+
For example given some type `for<'a> fn(&'a u32, for<'b> fn(&'b &'a u32))`, after instantiating both binders (assuming the `Universe` in the current `InferCtxt` was `U0` beforehand), the type of `&'b &'a u32` would be represented as `&'!2_0 &!1_0 u32`.
43
51
44
52
When the universe of the placeholder is `0`, it will be entirely omitted from the debug output, i.e. `!0_2` would be printed as `!2`. This rarely happens in practice though as we increase the universe in the `InferCtxt` when instantiating a binder with placeholders so usually the lowest universe placeholders encounterable are ones in `U1`.
45
53
46
-
`Binder`s can be instantiated with placeholders via the [`enter_forall`] method on `InferCtxt`. It should be used whenever the compiler should care about any possible instantiation of the binder instead of one concrete instantiation.
54
+
`Binder`s can be instantiated with placeholders via the [`enter_forall`] method on `InferCtxt`.
55
+
It should be used whenever the compiler should care about any possible instantiation of the binder instead of one concrete instantiation.
47
56
48
-
Note: in the original example of this chapter it was mentioned that we should not infer a local variable to have type `&'^0 u32`. This code is prevented from compiling via universes (as explained in the linked chapter)
57
+
Note: in the original example of this chapter it was mentioned that we should not infer a local variable to have type `&'^0 u32`.
58
+
This code is prevented from compiling via universes (as explained in the linked chapter)
49
59
50
60
### Why have both `RePlaceholder` and `ReBound`?
51
61
52
62
You may be wondering why we have both of these variants, afterall the data stored in `Placeholder` is effectively equivalent to that of `ReBound`: something to track which binder, and an index to track which parameter the `Binder` introduced.
53
63
54
-
The main reason for this is that `Bound` is a more syntactic representation of bound variables whereas `Placeholder` is a more semantic representation. As a concrete example:
64
+
The main reason for this is that `Bound` is a more syntactic representation of bound variables whereas `Placeholder` is a more semantic representation.
65
+
As a concrete example:
55
66
```rust
56
67
impl<'a> Other<'a> for&'au32 { }
57
68
@@ -66,7 +77,10 @@ where
66
77
{ ... }
67
78
```
68
79
69
-
Given these trait implementations `u32: Bar` should _not_ hold. `&'a u32` only implements `Other<'a>` when the lifetime of the borrow and the lifetime on the trait are equal. However if we only used `ReBound` and did not have placeholders it may be easy to accidentally believe that trait bound does hold. To explain this let's walk through an example of trying to prove `u32: Bar` in a world where rustc did not have placeholders:
80
+
Given these trait implementations `u32: Bar` should _not_ hold.
81
+
`&'a u32` only implements `Other<'a>` when the lifetime of the borrow and the lifetime on the trait are equal.
82
+
However if we only used `ReBound` and did not have placeholders it may be easy to accidentally believe that trait bound does hold.
83
+
To explain this let's walk through an example of trying to prove `u32: Bar` in a world where rustc did not have placeholders:
70
84
- We start by trying to prove `u32: Bar`
71
85
- We find the `impl<T> Bar for T` impl, we would wind up instantiating the `EarlyBinder` with `u32` (note: this is not _quite_ accurate as we first instantiate the binder with an inference variable that we then infer to be `u32` but that distinction is not super important here)
72
86
- There is a where clause `for<'a> &'^0 T: Trait` on the impl, as we instantiated the early binder with `u32` we actually have to prove `for<'a> &'^0 u32: Trait`
@@ -83,20 +97,21 @@ While in theory we could make this work it would be quite involved and more comp
83
97
- When resolving inference variables rewrite any bound variables according to the current binder depth of the infcx
84
98
- Maybe more (while writing this list items kept getting added so it seems naive to think this is exhaustive)
85
99
86
-
Fundamentally all of this complexity is because `Bound` ty/const/regions have a different representation for a given parameter on a `Binder` depending on how many other `Binder`s there are between the binder introducing the parameter, and its usage. For example given the following code:
100
+
Fundamentally all of this complexity is because `Bound` ty/const/regions have a different representation for a given parameter on a `Binder` depending on how many other `Binder`s there are between the binder introducing the parameter, and its usage.
That where clause would be written as: `for<'a> T: Trait<'^0, for<'b> fn(&'^0 T, &'^1_0 u32)>`
95
109
Despite there being two references to the `'a` parameter they are both represented differently: `^0` and `^1_0`, due to the fact that the latter usage is nested under a second `Binder` for the inner function pointer type.
96
110
97
111
This is in contrast to `Placeholder` ty/const/regions which do not have this limitation due to the fact that `Universe`s are specific to the current `InferCtxt` not the usage site of the parameter.
98
112
99
-
It is trivially possible to instantiate `EarlyBinder`s and unify inference variables with existing `Placeholder`s as no matter what context the `Placeholder` is in, it will have the same representation. As an example if we were to instantiate the binder on the higher ranked where clause from above, it would be represented like so:
113
+
It is trivially possible to instantiate `EarlyBinder`s and unify inference variables with existing `Placeholder`s as no matter what context the `Placeholder` is in, it will have the same representation.
114
+
As an example if we were to instantiate the binder on the higher ranked where clause from above, it would be represented like so:
100
115
`T: Trait<'!1_0, for<'b> fn(&'^0 T, &'!1_0 u32)>`
101
116
the `RePlaceholder` representation for both usages of `'a` are the same despite one being underneath another `Binder`.
102
117
@@ -107,9 +122,12 @@ the `RePlaceholder` for the `'b` parameter is in a higher universe to track the
107
122
## Instantiating with `ReLateParam`
108
123
109
124
As discussed in [the chapter about representing types][representing-types], `RegionKind` has two variants for representing generic parameters, `ReLateParam` and `ReEarlyParam`.
110
-
`ReLateParam` is conceptually a `Placeholder` that is always in the root universe (`U0`). It is used when instantiating late bound parameters of functions/closures while inside of them. Its actual representation is relatively different from both `ReEarlyParam` and `RePlaceholder`:
125
+
`ReLateParam` is conceptually a `Placeholder` that is always in the root universe (`U0`).
126
+
It is used when instantiating late bound parameters of functions/closures while inside of them.
127
+
Its actual representation is relatively different from both `ReEarlyParam` and `RePlaceholder`:
111
128
- A `DefId` for the item that introduced the late bound generic parameter
112
-
- A [`BoundRegionKind`] which either specifies the `DefId` of the generic parameter and its name (via a `Symbol`), or that this placeholder is representing the anonymous lifetime of a `Fn`/`FnMut` closure's self borrow. There is also a variant for `BrAnon` but this is not used for `ReLateParam`.
129
+
- A [`BoundRegionKind`] which either specifies the `DefId` of the generic parameter and its name (via a `Symbol`), or that this placeholder is representing the anonymous lifetime of a `Fn`/`FnMut` closure's self borrow.
130
+
There is also a variant for `BrAnon` but this is not used for `ReLateParam`.
113
131
114
132
For example, given the following code:
115
133
```rust,ignore
@@ -128,11 +146,14 @@ ReLateParam(
128
146
)
129
147
```
130
148
131
-
In this specific case of referencing late bound generic parameters of a function from inside the body this is done implicitly during `hir_ty_lowering` rather than explicitly when instantiating a `Binder` somewhere. In some cases however, we do explicitly instantiate a `Binder` with `ReLateParam`s.
149
+
In this specific case of referencing late bound generic parameters of a function from inside the body this is done implicitly during `hir_ty_lowering` rather than explicitly when instantiating a `Binder` somewhere.
150
+
In some cases however, we do explicitly instantiate a `Binder` with `ReLateParam`s.
132
151
133
-
Generally whenever we have a `Binder` for late bound parameters on a function/closure and we are conceptually inside of the binder already, we use [`liberate_late_bound_regions`] to instantiate it with `ReLateParam`s. That makes this operation the `Binder` equivalent to `EarlyBinder`'s `instantiate_identity`.
152
+
Generally whenever we have a `Binder` for late bound parameters on a function/closure and we are conceptually inside of the binder already, we use [`liberate_late_bound_regions`] to instantiate it with `ReLateParam`s.
153
+
That makes this operation the `Binder` equivalent to `EarlyBinder`'s `instantiate_identity`.
134
154
135
-
As a concrete example, accessing the signature of a function we are type checking will be represented as `EarlyBinder<Binder<FnSig>>`. As we are already "inside" of these binders, we would call `instantiate_identity` followed by `liberate_late_bound_regions`.
155
+
As a concrete example, accessing the signature of a function we are type checking will be represented as `EarlyBinder<Binder<FnSig>>`.
156
+
As we are already "inside" of these binders, we would call `instantiate_identity` followed by `liberate_late_bound_regions`.
0 commit comments