Skip to content

Commit feed479

Browse files
Pieter PenninckxPieter Penninckx
Pieter Penninckx
authored and
Pieter Penninckx
committed
Make dropping of temporaries in conditionals part of the rule
Reformulate text so that dropping of temporaries created in condition expression of an `if` or `if`/`else` or in the loop conditional expression of a `while` is now part of the rule, not an exception. Revert the paragraph concerning the `let` expression to its original form. Move examples. Include an example of a temporary without borrowing.
1 parent 9b340a1 commit feed479

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

Diff for: src/expressions.md

+21-22
Original file line numberDiff line numberDiff line change
@@ -84,30 +84,17 @@ The following expressions can create mutable lvalues:
8484
### Temporary lifetimes
8585

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

91-
A first exception is when a temporary value is created in the
92-
condition expression of an `if` or an `if`/`else` expression or in the
93-
loop conditional expression of a `while` expression.
94-
In this case, the lifetime ends right after the condition expression or the
95-
loop conditional expression.
89+
- the innermost enclosing statement; the tail expression of a block is
90+
considered part of the statement that encloses the block, or
91+
- the condition expression or the loop conditional expression if the
92+
temporary is created in the condition expression of an `if` or an `if`/`else`
93+
or in the loop conditional expression of a `while` expression.
9694

97-
Here are some examples:
98-
99-
- `let x = if foo(&temp()) {bar()} else {baz()}`. The expression `temp()` is
100-
an rvalue. As the temporary is created in the condition expression
101-
of an `if`/`else`, it will be freed at the end of the condition expression
102-
(in this example before the call to `bar` or `baz` is made).
103-
- `while foo(&temp()) {bar();}`. The temporary containing the return value from
104-
the call to `temp()` is created in the loop conditional expression. Hence it
105-
will be freed at the end of the loop conditional expression (in this example
106-
before the call to `bar` if the loop body is executed).
107-
108-
Another exception is when a temporary rvalue is being created that is assigned
109-
into a `let` declaration. In this case the temporary is created with the
110-
lifetime of the enclosing block, as using the enclosing statement (the `let`
95+
When a temporary rvalue is being created that is assigned into a `let`
96+
declaration, however, the temporary is created with the lifetime of the
97+
enclosing block instead, as using the enclosing statement (the `let`
11198
declaration) would be a guaranteed error (since a pointer to the temporary
11299
would be stored into a variable, but the temporary would be freed before the
113100
variable could be used). The compiler uses simple syntactic rules to decide
@@ -124,6 +111,18 @@ Here are some examples:
124111
method-call. Here we are assuming that `foo()` is an `&self` method
125112
defined in some trait, say `Foo`. In other words, the expression
126113
`temp().foo()` is equivalent to `Foo::foo(&temp())`.
114+
- `let x = if foo(&temp()) {bar()} else {baz()};`. The expression `temp()` is
115+
an rvalue. As the temporary is created in the condition expression
116+
of an `if`/`else`, it will be freed at the end of the condition expression
117+
(in this example before the call to `bar` or `baz` is made).
118+
- `let x = if temp().must_run_bar {bar()} else {baz()};`.
119+
Here we assume the type of `temp()` is a struct with a boolean field
120+
`must_run_bar`. As the previous example, the temporary corresponding to
121+
`temp()` will be freed at the end of the condition expression.
122+
- `while foo(&temp()) {bar();}`. The temporary containing the return value from
123+
the call to `temp()` is created in the loop conditional expression. Hence it
124+
will be freed at the end of the loop conditional expression (in this example
125+
before the call to `bar` if the loop body is executed).
127126
- `let x = &temp()`. Here, the same temporary is being assigned into
128127
`x`, rather than being passed as a parameter, and hence the
129128
temporary's lifetime is considered to be the enclosing block.

0 commit comments

Comments
 (0)