Skip to content

Commit fcdc090

Browse files
committed
Reframe lifetime extension to not be specific to let statements
1 parent 116eeb2 commit fcdc090

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

src/destructors.md

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,8 @@ r[destructors.scope.lifetime-extension]
375375
> [!NOTE]
376376
> The exact rules for temporary lifetime extension are subject to change. This is describing the current behavior only.
377377
378-
r[destructors.scope.lifetime-extension.let]
379-
The temporary scopes for expressions in `let` statements are sometimes
380-
*extended* to the scope of the block containing the `let` statement. This is
381-
done when the usual temporary scope would be too small, based on certain
382-
syntactic rules. For example:
378+
r[destructors.scope.lifetime-extension.intro]
379+
The temporary scopes for expressions are sometimes *extended*. This is done when the usual temporary scope would be too small, based on certain syntactic rules. For example:
383380

384381
```rust
385382
let x = &mut 0;
@@ -388,18 +385,6 @@ let x = &mut 0;
388385
println!("{}", x);
389386
```
390387

391-
r[destructors.scope.lifetime-extension.static]
392-
Lifetime extension also applies to `static` and `const` items, where it
393-
makes temporaries live until the end of the program. For example:
394-
395-
```rust
396-
const C: &Vec<i32> = &Vec::new();
397-
// Usually this would be a dangling reference as the `Vec` would only
398-
// exist inside the initializer expression of `C`, but instead the
399-
// borrow gets lifetime-extended so it effectively has `'static` lifetime.
400-
println!("{:?}", C);
401-
```
402-
403388
r[destructors.scope.lifetime-extension.sub-expressions]
404389
If a [borrow], [dereference][dereference expression], [field][field expression], or [tuple indexing expression] has an extended temporary scope, then so does its operand. If an [indexing expression] has an extended temporary scope, then the indexed expression also has an extended temporary scope.
405390

@@ -445,7 +430,7 @@ So `ref x`, `V(ref x)` and `[ref x, y]` are all extending patterns, but `x`, `&r
445430

446431
r[destructors.scope.lifetime-extension.patterns.let]
447432
If the pattern in a `let` statement is an extending pattern then the temporary
448-
scope of the initializer expression is extended.
433+
scope of the initializer expression is extended to the scope of the block containing the `let` statement.
449434

450435
```rust
451436
# fn temp() {}
@@ -508,10 +493,18 @@ r[destructors.scope.lifetime-extension.exprs.parent]
508493
If a temporary scope is extended through the scope of an extending expression, it is extended through that scope's [parent][destructors.scope.nesting].
509494

510495
r[destructors.scope.lifetime-extension.exprs.let]
511-
A temporary scope extended through a `let` statement scope is [extended] to the scope of the block containing the `let` statement ([destructors.scope.lifetime-extension.let]).
496+
A temporary scope extended through a `let` statement scope is [extended] to the scope of the block containing the `let` statement.
512497

513498
r[destructors.scope.lifetime-extension.exprs.static]
514-
A temporary scope extended through a [static][static item] or [constant item] scope or a [const block][const block expression] scope is [extended] to the end of the program ([destructors.scope.lifetime-extension.static]).
499+
A temporary scope extended through a [static][static item] or [constant item] scope or a [const block][const block expression] scope is [extended] to the end of the program.
500+
501+
```rust
502+
const C: &Vec<i32> = &Vec::new();
503+
// Usually this would be a dangling reference as the `Vec` would only
504+
// exist inside the initializer expression of `C`, but instead the
505+
// borrow gets lifetime-extended so it effectively has `'static` lifetime.
506+
println!("{:?}", C);
507+
```
515508

516509
r[destructors.scope.lifetime-extension.exprs.other]
517510
A temporary scope extended through the scope of a non-extending expression is [extended] to that expression's [temporary scope].

0 commit comments

Comments
 (0)