|
| 1 | +# Early and Late Bound Parameter Implementation Nuances |
| 2 | + |
| 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 | + |
| 7 | +Understanding this page likely requires a rudimentary understanding of higher ranked |
| 8 | +trait bounds/`for<'a>`and also what types such as `dyn for<'a> Trait<'a>` and |
| 9 | + `for<'a> fn(&'a u32)` mean. Reading [the nomincon chapter](https://doc.rust-lang.org/nomicon/hrtb.html) |
| 10 | +on HRTB may be useful for understanding this syntax. The meaning of `for<'a> fn(&'a u32)` |
| 11 | +is incredibly similar to the meaning of `T: for<'a> Trait<'a>`. |
| 12 | + |
| 13 | +## What does it mean for parameters to be early or late bound |
| 14 | + |
| 15 | +All function definitions conceptually have a ZST (this is represented by `TyKind::FnDef` in rustc). |
| 16 | +The only generics on this ZST are the early bound parameters of the function definition. e.g. |
| 17 | +```rust |
| 18 | +fn foo<'a>(_: &'a u32) {} |
| 19 | + |
| 20 | +fn main() { |
| 21 | + let b = foo; |
| 22 | + // ^ `b` has type `FnDef(foo, [])` (no args because `'a` is late bound) |
| 23 | + assert!(std::mem::size_of_val(&b) == 0); |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +In order to call `b` the late bound parameters do need to be provided, these are inferred at the |
| 28 | +call site instead of when we refer to `foo`. |
| 29 | +```rust |
| 30 | +fn main() { |
| 31 | + let b = foo; |
| 32 | + let a: &'static u32 = &10; |
| 33 | + foo(a); |
| 34 | + // the lifetime argument for `'a` on `foo` is inferred at the callsite |
| 35 | + // the generic parameter `'a` on `foo` is inferred to `'static` here |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +Because late bound parameters are not part of the `FnDef`'s args this allows us to prove trait |
| 40 | +bounds such as `F: for<'a> Fn(&'a u32)` where `F` is `foo`'s `FnDef`. e.g. |
| 41 | +```rust |
| 42 | +fn foo_early<'a, T: Trait<'a>>(_: &'a u32, _: T) {} |
| 43 | +fn foo_late<'a, T>(_: &'a u32, _: T) {} |
| 44 | + |
| 45 | +fn accepts_hr_func<F: for<'a> Fn(&'a u32, u32)>(_: F) {} |
| 46 | + |
| 47 | +fn main() { |
| 48 | + // doesn't work, the instantiated bound is `for<'a> FnDef<'?0>: Fn(&'a u32, u32)` |
| 49 | + // `foo_early` only implements `for<'a> FnDef<'a>: Fn(&'a u32, u32)`- the lifetime |
| 50 | + // of the borrow in the function argument must be the same as the lifetime |
| 51 | + // on the `FnDef`. |
| 52 | + accepts_hr_func(foo_early); |
| 53 | + |
| 54 | + // works, the instantiated bound is `for<'a> FnDef: Fn(&'a u32, u32)` |
| 55 | + accepts_hr_func(foo_late); |
| 56 | +} |
| 57 | + |
| 58 | +// the builtin `Fn` impls for `foo_early` and `foo_late` look something like: |
| 59 | +// `foo_early` |
| 60 | +impl<'a, T: Trait<'a>> Fn(&'a u32, T) for FooEarlyFnDef<'a, T> { ... } |
| 61 | +// `foo_late` |
| 62 | +impl<'a, T> Fn(&'a u32, T) for FooLateFnDef<T> { ... } |
| 63 | + |
| 64 | +``` |
| 65 | + |
| 66 | +Early bound parameters are present on the `FnDef`. Late bound generic parameters are not present |
| 67 | +on the `FnDef` but are instead constrained by the builtin `Fn*` impl. |
| 68 | + |
| 69 | +The same distinction applies to closures. Instead of `FnDef` we are talking about the anonymous |
| 70 | +closure type. Closures are [currently unsound](https://github.com/rust-lang/rust/issues/84366) in |
| 71 | +ways that are closely related to the distinction between early/late bound |
| 72 | +parameters (more on this later) |
| 73 | + |
| 74 | +The early/late boundness of generic parameters is only relevant for the desugaring of |
| 75 | +functions/closures into types with builtin `Fn*` impls. It does not make sense to talk about |
| 76 | +in other contexts. |
| 77 | + |
| 78 | +The `generics_of` query in rustc only contains early bound parameters. In this way it acts more |
| 79 | +like `generics_of(my_func)` is the generics for the FnDef than the generics provided to the function |
| 80 | +body although it's not clear to the author of this section if this was the actual justification for |
| 81 | +making `generics_of` behave this way. |
| 82 | + |
| 83 | +## What parameters are currently late bound |
| 84 | + |
| 85 | +Below are the current requirements for determining if a generic parameter is late bound. It is worth |
| 86 | +keeping in mind that these are not necessarily set in stone and it is almost certainly possible to |
| 87 | +be more flexible. |
| 88 | + |
| 89 | +### Must be a lifetime parameter |
| 90 | + |
| 91 | +Rust can't support types such as `for<T> dyn Trait<T>` or `for<T> fn(T)`, this is a |
| 92 | +fundamental limitation of the language as we are required to monomorphize type/const |
| 93 | +parameters and cannot do so behind dynamic dispatch. (technically we could probably |
| 94 | +support `for<T> dyn MarkerTrait<T>` as there is nothing to monomorphize) |
| 95 | + |
| 96 | +Not being able to support `for<T> dyn Trait<T>` resulted in making all type and const |
| 97 | +parameters early bound. Only lifetime parameters can be late bound. |
| 98 | + |
| 99 | +### Must not appear in the where clauses |
| 100 | + |
| 101 | +In order for a generic parameter to be late bound it must not appear in any where clauses. |
| 102 | +This is currently an incredibly simplistic check that causes lifetimes to be early bound even |
| 103 | +if the where clause they appear in are always true, or implied by well formedness of function |
| 104 | +arguments. e.g. |
| 105 | +```rust |
| 106 | +fn foo1<'a: 'a>(_: &'a u32) {} |
| 107 | +// ^^ early bound parameter because it's in a `'a: 'a` clause |
| 108 | +// even though the bound obviously holds all the time |
| 109 | +fn foo2<'a, T: Trait<'a>(a: T, b: &'a u32) {} |
| 110 | +// ^^ early bound parameter because it's used in the `T: Trait<'a>` clause |
| 111 | +fn foo3<'a, T: 'a>(_: &'a T) {} |
| 112 | +// ^^ early bound parameter because it's used in the `T: 'a` clause |
| 113 | +// even though that bound is implied by wellformedness of `&'a T` |
| 114 | +fn foo4<'a, 'b: 'a>(_: Inv<&'a ()>, _: Inv<&'b ()>) {} |
| 115 | +// ^^ ^^ ^^^ note: |
| 116 | +// ^^ ^^ `Inv` stands for `Invariant` and is used to |
| 117 | +// ^^ ^^ make the type parameter invariant. This |
| 118 | +// ^^ ^^ is necessary for demonstration purposes as |
| 119 | +// ^^ ^^ `for<'a, 'b> fn(&'a (), &'b ())` and |
| 120 | +// ^^ ^^ `for<'a> fn(&'a u32, &'a u32)` are subtypes- |
| 121 | +// ^^ ^^ of each other which makes the bound trivially |
| 122 | +// ^^ ^^ satisfiable when making the fnptr. `Inv` |
| 123 | +// ^^ ^^ disables this subtyping. |
| 124 | +// ^^ ^^ |
| 125 | +// ^^^^^^ both early bound parameters because they are present in the |
| 126 | +// `'b: 'a` clause |
| 127 | +``` |
| 128 | + |
| 129 | +The reason for this requirement is that we cannot represent the `T: Trait<'a>` or `'a: 'b` clauses |
| 130 | +on a function pointer. `for<'a, 'b> fn(Inv<&'a ()>, Inv<&'b ()>)` is not a valid function pointer to |
| 131 | +represent`foo4` as it would allow calling the function without `'b: 'a` holding. |
| 132 | + |
| 133 | +### Must be constrained by where clauses or function argument types |
| 134 | + |
| 135 | +The builtin impls of the `Fn*` traits for closures and `FnDef`s cannot not have any unconstrained |
| 136 | +parameters. For example the following impl is illegal: |
| 137 | +```rust |
| 138 | +impl<'a> Trait for u32 { type Assoc = &'a u32; } |
| 139 | +``` |
| 140 | +We must not end up with a similar impl for the `Fn*` traits e.g. |
| 141 | +```rust |
| 142 | +impl<'a> Fn<()> for FnDef { type Assoc = &'a u32 } |
| 143 | +``` |
| 144 | + |
| 145 | +Violating this rule can trivially lead to unsoundness as seen in [#84366](https://github.com/rust-lang/rust/issues/84366). |
| 146 | +Additionally if we ever support late bound type params then an impl like: |
| 147 | +```rust |
| 148 | +impl<T> Fn<()> for FnDef { type Assoc = T; } |
| 149 | +``` |
| 150 | +would break the compiler in various ways. |
| 151 | + |
| 152 | +In order to ensure that everything functions correctly, we do not allow generic parameters to |
| 153 | +be late bound if it would result in a builtin impl that does not constrain all of the generic |
| 154 | +parameters on the builtin impl. Making a generic parameter be early bound trivially makes it be |
| 155 | +constrained by the builtin impl as it ends up on the self type. |
| 156 | + |
| 157 | +Because of the requirement that late bound parameters must not appear in where clauses, checking |
| 158 | +this is simpler than the rules for checking impl headers constrain all the parameters on the impl. |
| 159 | +We only have to ensure that all late bound parameters appear at least once in the function argument |
| 160 | +types outside of an alias (e.g. an associated type). |
| 161 | + |
| 162 | +The requirement that they not indirectly be in the args of an alias for it to count is the |
| 163 | +same as why the follow code is forbidden: |
| 164 | +```rust |
| 165 | +impl<T: Trait> OtherTrait for <T as Trait>::Assoc { type Assoc = T } |
| 166 | +``` |
| 167 | +There is no guarantee that `<T as Trait>::Assoc` will normalize to different types for every |
| 168 | +instantiation of `T`. If we were to allow this impl we could get overlapping impls and the |
| 169 | +same is true of the builtin `Fn*` impls. |
| 170 | + |
| 171 | +## Making more generic parameters late bound |
| 172 | + |
| 173 | +It is generally considered desirable for more parameters to be late bound as it makes |
| 174 | +the builtin `Fn*` impls more flexible. Right now many of the requirements for making |
| 175 | +a parameter late bound are overly restrictive as they are tied to what we can currently |
| 176 | +(or can ever) do with fn ptrs. |
| 177 | + |
| 178 | +It would be theoretically possible to support late bound params in `where`-clauses in the |
| 179 | +language by introducing implication types which would allow us to express types such as: |
| 180 | +`for<'a, 'b: 'a> fn(Inv<&'a u32>, Inv<&'b u32>)` which would ensure `'b: 'a` is upheld when |
| 181 | +calling the function pointer. |
| 182 | + |
| 183 | +It would also be theoretically possible to support it by making the coercion to a fn ptr |
| 184 | +instantiate the parameter with an infer var while still allowing the FnDef to not have the |
| 185 | +generic parameter present as trait impls are perfectly capable of representing the where clauses |
| 186 | +on the function on the impl itself. This would also allow us to support late bound type/const |
| 187 | +vars allowing bounds like `F: for<T> Fn(T)` to hold. |
| 188 | + |
| 189 | +It is almost somewhat unclear if we can change the `Fn` traits to be structured differently |
| 190 | +so that we never have to make a parameter early bound just to make the builtin impl have all |
| 191 | +generics be constrained. Of all the possible causes of a generic parameter being early bound |
| 192 | +this seems the most difficult to remove. |
| 193 | + |
| 194 | +Whether these would be good ideas to implement is a separate question- they are only brought |
| 195 | +up to illustrate that the current rules are not necessarily set in stone and a result of |
| 196 | +"its the only way of doing this". |
| 197 | + |
0 commit comments