Skip to content
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

Document lifetime of temporaries in conditions of if and while-expr #94

Merged
merged 3 commits into from
Aug 14, 2017
Merged
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
22 changes: 19 additions & 3 deletions src/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@ The following expressions can create mutable lvalues:
### Temporary lifetimes

When using an rvalue in most lvalue contexts, a temporary unnamed lvalue is
created and used instead. The lifetime of temporary values is typically the
innermost enclosing statement; the tail expression of a block is considered
part of the statement that encloses the block.
created and used instead. The lifetime of temporary values is typically

- the innermost enclosing statement; the tail expression of a block is
considered part of the statement that encloses the block, or
- the condition expression or the loop conditional expression if the
temporary is created in the condition expression of an `if` or an `if`/`else`
or in the loop conditional expression of a `while` expression.

When a temporary rvalue is being created that is assigned into a `let`
declaration, however, the temporary is created with the lifetime of the
Expand All @@ -107,6 +111,18 @@ Here are some examples:
method-call. Here we are assuming that `foo()` is an `&self` method
defined in some trait, say `Foo`. In other words, the expression
`temp().foo()` is equivalent to `Foo::foo(&temp())`.
- `let x = if foo(&temp()) {bar()} else {baz()};`. The expression `temp()` is
an rvalue. As the temporary is created in the condition expression
of an `if`/`else`, it will be freed at the end of the condition expression
(in this example before the call to `bar` or `baz` is made).
- `let x = if temp().must_run_bar {bar()} else {baz()};`.
Here we assume the type of `temp()` is a struct with a boolean field
`must_run_bar`. As the previous example, the temporary corresponding to
`temp()` will be freed at the end of the condition expression.
- `while foo(&temp()) {bar();}`. The temporary containing the return value from
the call to `temp()` is created in the loop conditional expression. Hence it
will be freed at the end of the loop conditional expression (in this example
before the call to `bar` if the loop body is executed).
- `let x = &temp()`. Here, the same temporary is being assigned into
`x`, rather than being passed as a parameter, and hence the
temporary's lifetime is considered to be the enclosing block.
Expand Down