Skip to content

Add new temporary lifetime extension rule #1813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/destructors.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,11 @@ expression which is one of the following:
* The operand(s) of an extending [array][array expression], [cast][cast
expression], [braced struct][struct expression], or [tuple][tuple expression]
expression.
* The arguments to an extending [tuple struct] or [tuple variant] constructor expression.
* The final expression of any extending [block expression].

So the borrow expressions in `&mut 0`, `(&1, &mut 2)`, and `Some { 0: &mut 3 }`
are all extending expressions. The borrows in `&0 + &1` and `Some(&mut 0)` are
not: the latter is syntactically a function call expression.
So the borrow expressions in `&mut 0`, `(&1, &mut 2)`, and `Some(&mut 3)`
are all extending expressions. The borrows in `&0 + &1` and `f(&mut 0)` are not.

The operand of any extending borrow expression has its temporary scope
extended.
Expand All @@ -404,7 +404,7 @@ Here are some examples where expressions have extended temporary scopes:
let x = &temp();
let x = &temp() as &dyn Send;
let x = (&*&temp(),);
let x = { [Some { 0: &temp(), }] };
let x = { [Some(&temp()) ] };
let ref x = temp();
let ref x = *&temp();
# x;
Expand All @@ -419,7 +419,7 @@ Here are some examples where expressions don't have extended temporary scopes:
// The temporary that stores the result of `temp()` only lives until the
// end of the let statement in these cases.

let x = Some(&temp()); // ERROR
let x = std::convert::identity(&temp()); // ERROR
let x = (&temp()).use_temp(); // ERROR
# x;
```
Expand Down Expand Up @@ -476,6 +476,8 @@ There is one additional case to be aware of: when a panic reaches a [non-unwindi
[struct pattern]: patterns.md#struct-patterns
[tuple pattern]: patterns.md#tuple-patterns
[tuple struct pattern]: patterns.md#tuple-struct-patterns
[tuple struct]: type.struct.tuple
[tuple variant]: type.enum.declaration

[array expression]: expressions/array-expr.md#array-expressions
[block expression]: expressions/block-expr.md
Expand Down
Loading