Skip to content

Commit 53de4ae

Browse files
committed
specify lifetime extension of pin! and format_args! arguments
1 parent 0a25dea commit 53de4ae

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/destructors.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ scope of the initializer expression is extended.
425425
r[destructors.scope.lifetime-extension.exprs]
426426
#### Extending based on expressions
427427

428+
r[destructors.scope.lifetime-extension.exprs.extending]
428429
For a let statement with an initializer, an *extending expression* is an
429430
expression which is one of the following:
430431

@@ -434,19 +435,26 @@ expression which is one of the following:
434435
expression], [braced struct][struct expression], or [tuple][tuple expression]
435436
expression.
436437
* The arguments to an extending [tuple struct] or [tuple variant] constructor expression.
438+
* The argument(s) to an extending [`pin!`] or [`format_args!`] [macro invocation] expression.
437439
* The final expression of any extending [block expression].
438440

439441
So the borrow expressions in `&mut 0`, `(&1, &mut 2)`, and `Some(&mut 3)`
440442
are all extending expressions. The borrows in `&0 + &1` and `f(&mut 0)` are not.
441443

444+
r[destructors.scope.lifetime-extension.exprs.borrow]
442445
The operand of any extending borrow expression has its temporary scope
443446
extended.
444447

448+
r[destructors.scope.lifetime-extension.exprs.macros]
449+
The built-in macros [`pin!`] and [`format_args!`] create temporaries.
450+
Any extending [`pin!`] or [`format_args!`] [macro invocation] expression has an extended temporary scope.
451+
445452
#### Examples
446453

447454
Here are some examples where expressions have extended temporary scopes:
448455

449456
```rust
457+
# use std::pin::pin;
450458
# fn temp() {}
451459
// The temporary that stores the result of `temp()` lives in the same scope
452460
// as x in these cases.
@@ -456,6 +464,8 @@ let x = &temp() as &dyn Send;
456464
# x;
457465
let x = (&*&temp(),);
458466
# x;
467+
let x = pin!(temp());
468+
# x;
459469
let x = { [Some(&temp()) ] };
460470
# x;
461471
let ref x = temp();
@@ -510,6 +520,7 @@ There is one additional case to be aware of: when a panic reaches a [non-unwindi
510520
[initialized]: glossary.md#initialized
511521
[interior mutability]: interior-mutability.md
512522
[lazy boolean expression]: expressions/operator-expr.md#lazy-boolean-operators
523+
[macro invocation]: macros.md#macro-invocation
513524
[non-unwinding ABI boundary]: items/functions.md#unwinding
514525
[panic]: panic.md
515526
[place context]: expressions.md#place-expressions-and-value-expressions
@@ -554,3 +565,6 @@ There is one additional case to be aware of: when a panic reaches a [non-unwindi
554565
[`match`]: expressions/match-expr.md
555566
[`while let`]: expressions/loop-expr.md#while-let-patterns
556567
[`while`]: expressions/loop-expr.md#predicate-loops
568+
569+
[`pin!`]: std::pin::pin
570+
[`format_args!`]: core::format_args

0 commit comments

Comments
 (0)