Skip to content

Commit b02f1aa

Browse files
committed
Improve example on avoiding HR lifetime capture
We have an example showing how to avoid capturing a higher ranked lifetime in a nested opaque type so that the code can be migrated to Rust 2024. However, because we didn't parameterize the trait in the example with a lifetime, the code could be migrated by just removing the `for<..>` binder. This makes the example weaker than it could be, so let's strengthen it by parameterizing the trait. (Thanks to aliemjay for raising this point.)
1 parent f012228 commit b02f1aa

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

text/3617-precise-capturing.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,14 @@ mod _0 {
257257

258258
## Avoiding capture of higher ranked lifetimes in nested opaques
259259

260-
For implementation reasons, Rust does not yet support higher ranked lifetime bounds on nested opaque types (see [#104288][]). However, according to the Lifetime Capture Rules 2024, a nested `impl Trait` opaque type *must* capture all generic parameters in scope, including higher ranked ones. Therefore, in Rust 2024, this code fails to compile:
260+
According to the Lifetime Capture Rules 2024, a nested `impl Trait` opaque type *must* capture all generic parameters in scope, including higher ranked ones. However, for implementation reasons, Rust does not yet support higher ranked lifetime bounds on nested opaque types (see [#104288][]). Therefore, in Rust 2024, this code, which is valid in Rust 2021, fails to compile:
261261

262262
```rust
263-
trait Trait { type Ty; }
264-
impl<F> Trait for F { type Ty = (); }
263+
//@ edition: 2024
264+
trait Trait<'a> { type Ty; }
265+
impl<F> Trait<'_> for F { type Ty = (); }
265266

266-
fn foo() -> impl for<'a> Trait<Ty = impl Sized> {
267+
fn foo() -> impl for<'a> Trait<'a, Ty = impl Sized> {
267268
//~^ ERROR `impl Trait` cannot capture higher-ranked lifetime
268269
//~| from outer `impl Trait`
269270
fn f(_: &()) -> &'static () { &() }
@@ -274,9 +275,9 @@ fn foo() -> impl for<'a> Trait<Ty = impl Sized> {
274275
With `use<..>`, we can avoid capturing this higher ranked lifetime, allowing compilation:
275276

276277
```rust
277-
fn foo() -> impl for<'a> Trait<Ty = impl use<> Sized> {
278-
// ^^^^^^^^^^^^^^^^
279-
// ^ Captures nothing.
278+
fn foo() -> impl for<'a> Trait<'a, Ty = impl use<> Sized> {
279+
// ^^^^^^^^^^^^^^^^
280+
// ^ Captures nothing.
280281
fn f(_: &()) -> &'static () { &() }
281282
f
282283
}

0 commit comments

Comments
 (0)