|
| 1 | +# Turbofishing's interactions with early/late bound parameters |
| 2 | + |
| 3 | +The early/late bound parameter distinction on functions introduces some complications when providing generic arguments to functions. This document discusses what those are and how they might interact with future changes to make more things late bound. |
| 4 | + |
| 5 | +## Can't turbofish generic arguments on functions sometimes |
| 6 | + |
| 7 | +When a function has any late bound lifetime parameters (be they explicitly defined or implicitly introduced via lifetime elision) we disallow specifying any lifetime arguments on the function. Sometimes this is a hard error other times it is a future compat lint ([`late_bound_lifetime_arguments`](https://github.com/rust-lang/rust/issues/42868)). |
| 8 | + |
| 9 | +```rust |
| 10 | +fn early<'a: 'a>(a: &'a ()) -> &'a () { a } |
| 11 | +fn late<'a>(a: &'a ()) -> &'a () { a } |
| 12 | + |
| 13 | +fn mixed<'a, 'b: 'b>(a: &'a (), b: &'b ()) -> &'a () { a } |
| 14 | + |
| 15 | +struct Foo; |
| 16 | +impl Foo { |
| 17 | + fn late<'a>(self, a: &'a ()) -> &'a () { a } |
| 18 | +} |
| 19 | + |
| 20 | +fn main() { |
| 21 | + // fine |
| 22 | + let f = early::<'static>; |
| 23 | + |
| 24 | + // some variation of hard errors and future compat lints |
| 25 | + Foo.late::<'static>(&()); |
| 26 | + let f = late::<'static>; |
| 27 | + let f = mixed::<'static, 'static>; |
| 28 | + let f = mixed::<'static>; |
| 29 | + late::<'static>(&()); |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +The justification for this is that late bound parameters are not present on the `FnDef` so the arguments to late bound parameters can't be present in the substs for the type. i.e. the `late` function in the above code snippet would not have any generic parameters on the `FnDef` zst: |
| 34 | +```rust |
| 35 | +// example desugaring of the `late` function and its zst + builtin Fn impl |
| 36 | +struct LateFnDef; |
| 37 | +impl<'a> Fn<(&'a ())> for LateFnDef { |
| 38 | + type Output = &'a (); |
| 39 | + ... |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +The cause for some situations giving future compat lints and others giving hard errors is a little arbitrary but explainable: |
| 44 | +- It's always a hard error for method calls |
| 45 | +- It's only a hard error on paths to free functions if there is no unambiguous way to create the substs for the fndef from the lifetime arguments. (i.e. the amount of lifetimes provided must be exactly equal to the amount of early bound lifetimes or else it's a hard error) |
| 46 | + |
| 47 | +## Back compat issues from turning early bound to late bound |
| 48 | + |
| 49 | +Because of the previously mentioned restriction on turbofishing generic arguments, it is a breaking change to upgrade a lifetime from early bound to late bound as it can cause existing turbofishies to become hard errors/future compat lints. |
| 50 | + |
| 51 | +Many t-types members have expressed interest in wanting more parameters to be late bound. We cannot do so if making something late bound is going to break code that many would expect to work (judging by the future compat lint issue many people do expect to be able to turbofish late bound parameters). |
| 52 | + |
| 53 | +## Interactions with late bound type/const parameters |
| 54 | + |
| 55 | +If we were to make some type/const parameters late bound we would definitely not want to disallow turbofishing them as it presumably(?) would break a Tonne of code. |
| 56 | + |
| 57 | +While lifetimes do differ from type/consts in some ways I(BoxyUwU) do not believe there is any justification for why it would make sense to allow turbofishing late bound type/const parameters but not late bound lifetimes. |
| 58 | + |
| 59 | +## Removing the hard error/fcw |
| 60 | + |
| 61 | +From reasons above it seems reasonable that we may want to remove the hard error and fcw (removing the errors/fcw is definitely a blocker for making more things late bound). |
| 62 | + |
| 63 | +example behaviour: |
| 64 | +```rust |
| 65 | +fn late<'a>(a: &'a ()) -> &'a () { a } |
| 66 | + |
| 67 | +fn accepts_fn(_: impl for<'a> Fn(&'a ()) -> &'a ()) {} |
| 68 | +fn accepts_fn_2(_: impl Fn(&'static ()) -> &'static ()) {} |
| 69 | + |
| 70 | +fn main() { |
| 71 | + let f = late::<'static>; |
| 72 | + |
| 73 | + accepts_fn(f); //~ error: `f` doesnt implement `for<'a> Fn(&'a ()) -> &'a ()` |
| 74 | + accepts_fn_2(f) // works |
| 75 | + |
| 76 | + accepts_fn(late) // works |
| 77 | +} |
| 78 | +```` |
| 79 | + |
| 80 | +one potential complication is that we would want a way to specify a generic argument to a function without having to specify arguments for all previous parameters. i.e. ideally you could write the following code somehow. |
| 81 | +```rust |
| 82 | +fn late<'a, 'b>(_: &'a (), _: &'b ()) {} |
| 83 | + |
| 84 | +fn accepts_fn(_: impl for<'a> Fn(&'a (), &'static ())) {} |
| 85 | + |
| 86 | +fn main() { |
| 87 | + // a naive implementation would have a `ReInfer` as the subst for `'a` parameter |
| 88 | + // no longer allowing the FnDef to satisfy the `for<'a> Fn(&'a ()` bound |
| 89 | + let f = late::<'_, 'static>; |
| 90 | + accepts_fn(f); |
| 91 | +} |
| 92 | +``` |
| 93 | +Maybe we can just special case astconv for `_`/`'_` arguments for late bound parameters somehow and have it not mean the same thing as `_` for early bound parameters. Regardless I think we would need a solution that would allow writing the above code even if it was done by some new syntax such as havign to write `late::<k#no_argument, 'static>` (naturally `k#no_argument` would only make sense as an argument to late bound parameters). |
| 94 | + |
| 95 | + |
| 96 | +## Conclusion |
| 97 | + |
| 98 | +Late bound params make turbofishing complicated. We currently have a hard error and a future compat lint that we might or might not want. |
| 99 | + |
| 100 | +We don't have to decide on anything in this meeting when it comes to the error/fcw or whether we even want to make more things late bound. The primary purpose is to spread knowledge. I would still like to see what people think about removing the error and fcw though (but any decision about it will have a proper FCP). |
0 commit comments