Skip to content

Commit 832291f

Browse files
committed
Add normative example on for<..> capturing
Under the rules of this RFC, we automatically capture all lifetime parameters in scope of an RPIT-like `impl Trait` opaque type. This includes lifetimes that are in scope due to `for<..>` binders for higher ranked trait bounds (HRTBs). However, we did not describe this explicitly and we did not have an example of this. Let's describe this and add an example. (Thanks to @aliemjay for pointing this out.)
1 parent 220b5f8 commit 832291f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

text/3498-lifetime-capture-rules-2024.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,33 @@ impl<T> Foo<T> {
279279
}
280280
```
281281

282+
### Capturing lifetimes from `for<..>` binders
283+
284+
Once higher kinded lifetime bounds on nested opaque types are supported in Rust (see [#104288][]), the following code will become legal:
285+
286+
```rust
287+
trait Trait<'a> {
288+
type Assoc;
289+
}
290+
291+
impl<'a, F: Fn(&'a ()) -> &'a ()> Trait<'a> for F {
292+
type Assoc = &'a ();
293+
}
294+
295+
fn foo() -> impl for<'a> Trait<'a, Assoc = impl Sized> {
296+
// ^^^^^^^^^^
297+
// Captures `'a`. ^
298+
fn f(x: &()) -> &() { x }
299+
f
300+
}
301+
```
302+
303+
That is, the `'a` lifetime parameter from the higher ranked trait bounds (HRTBs) `for<..>` binder is in scope for the `impl Sized` opaque type, so it is captured under the rules of this RFC.
304+
305+
Note that support for higher kinded lifetime bounds is not required by this RFC and is not a blocker to stabilizing the rules specified in this RFC.
306+
307+
[#104288]: https://github.com/rust-lang/rust/issues/104288
308+
282309
## TAIT as the solution to overcapturing
283310

284311
[TAIT as the solution to overcapturing]: #tait-as-the-solution-to-overcapturing

0 commit comments

Comments
 (0)