Skip to content

Commit 9b0ca4c

Browse files
matthewjasperehuss
authored andcommitted
Address review feedback
1 parent 99da7c0 commit 9b0ca4c

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

src/destructors.md

+32-18
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ Given a function, or closure, there are drop scopes for:
7171
* Each [statement]
7272
* Each [expression]
7373
* Each block, including the function body
74-
* [Block expressions] the expression and block scopes are the same scope.
74+
* In the case of a [block expression], the scope for the block and the
75+
expression are the same scope.
7576
* Each arm of a `match` expression
7677

7778
Drop scopes are nested within one another as follows. When multiple scopes are
@@ -82,13 +83,14 @@ from the inside outwards.
8283
* The function body block is contained within the scope of the entire function.
8384
* The parent of the expression in an expression statement is the scope of the
8485
statement.
85-
* The parent of the initializer of a `let` statement is the `let`
86-
statement's scope.
86+
* The parent of the initializer of a [`let` statement] is the `let` statement's
87+
scope.
8788
* The parent of a statement scope is the scope of the block that contains the
8889
statement.
8990
* The parent of the expression for a `match` guard is the scope of the arm that
9091
the guard is for.
91-
* The parent of the expression for a given `match` arm is that arm's scope.
92+
* The parent of the expression after the `=>` in a `match` expression is the
93+
scope of the arm that it's in.
9294
* The parent of the arm scope is the scope of the `match` expression that it
9395
belongs to.
9496
* The parent of all other scopes is the scope of the immediately enclosing
@@ -97,8 +99,8 @@ from the inside outwards.
9799
### Scopes of function parameters
98100

99101
All function parameters are in the scope of the entire function body, so are
100-
dropped last when evaluating the function. Actual function parameters are
101-
dropped after any named parameters that are bound to parts of it.
102+
dropped last when evaluating the function. Each actual function parameter is
103+
dropped after any bindings introduced in that parameter's pattern.
102104

103105
```rust
104106
# struct PrintOnDrop(&'static str);
@@ -124,8 +126,8 @@ patterns_in_parameters(
124126

125127
Local variables declared in a `let` statement are associated to the scope of
126128
the block that contains the `let` statement. Local variables declared in a
127-
`match` expression are associated to the arm scope of the `match` arm that they are declared
128-
in.
129+
`match` expression are associated to the arm scope of the `match` arm that they
130+
are declared in.
129131

130132
```rust
131133
# struct PrintOnDrop(&'static str);
@@ -141,14 +143,14 @@ let declared_first = PrintOnDrop("Dropped last in outer scope");
141143
let declared_last = PrintOnDrop("Dropped first in outer scope");
142144
```
143145

144-
If multiple patterns are used in the same arm for a `match` expression, then an unspecified
145-
pattern will be used to determine the drop order.
146+
If multiple patterns are used in the same arm for a `match` expression, then an
147+
unspecified pattern will be used to determine the drop order.
146148

147149
### Temporary scopes
148150

149151
The *temporary scope* of an expression is the scope that is used for the
150152
temporary variable that holds the result of that expression when used in a
151-
[place context], unless it is promoted to a `static`.
153+
[place context], unless it is [promoted].
152154

153155
Apart from lifetime extension, the temporary scope of an expression is the
154156
smallest scope that contains the expression and is for one of the following:
@@ -162,9 +164,16 @@ smallest scope that contains the expression and is for one of the following:
162164
* The expression for a match arm.
163165
* The second operand of a [lazy boolean expression].
164166

165-
> Note: Temporaries that are created in the final expression of a function body
166-
> are dropped *after* any named variables bound in the function body, as there
167-
> is no smaller enclosing temporary scope.
167+
> **Notes**:
168+
>
169+
> Temporaries that are created in the final expression of a function
170+
> body are dropped *after* any named variables bound in the function body, as
171+
> there is no smaller enclosing temporary scope.
172+
>
173+
> The [scrutinee] of a `match` expression is not a temporary scope, so
174+
> temporaries in the scrutinee can be dropped after the `match` expression. For
175+
> example, the temporary for `1` in `match 1 { ref mut z => z };` lives until
176+
> the end of the statement.
168177
169178
Some examples:
170179

@@ -245,6 +254,9 @@ always has the type `&'static Option<_>`, as it contains nothing disallowed).
245254

246255
### Temporary lifetime extension
247256

257+
> **Note**: The exact rules for temporary lifetime extension are subject to
258+
> change. This is describes the current behavior only.
259+
248260
The temporary scopes for expressions in `let` statements are sometimes
249261
*extended* to the scope of the block containing the `let` statement. This is
250262
done when the usual temporary scope would be too small, based on certain
@@ -287,11 +299,11 @@ expression which is one of the following:
287299
* The operand(s) of an extending [array][array expression], [cast][cast
288300
expression], [braced struct][struct expression], or [tuple][tuple expression]
289301
expression.
290-
* The final expression of any extending [block expression][block expressions].
302+
* The final expression of any extending [block expression].
291303

292304
So the borrow expressions in `&mut 0`, `(&1, &mut 2)`, and `Some { 0: &mut 3 }`
293-
are all extending expressions, while the borrows in `&0 + &1` and
294-
`Some(&mut 0)` are not.
305+
are all extending expressions. The borrows in `&0 + &1` and `Some(&mut 0)` are
306+
not: the latter is syntactically a function call expression.
295307

296308
The operand of any extending borrow expression has its temporary scope
297309
extended.
@@ -345,6 +357,8 @@ variable or field from being dropped automatically.
345357
[interior mutability]: interior-mutability.md
346358
[lazy boolean expression]: expressions/operator-expr.md#lazy-boolean-operators
347359
[place context]: expressions.md#place-expressions-and-value-expressions
360+
[promoted]: destructors.md#constant-promotion
361+
[scrutinee]: glossary.md#scrutinee
348362
[statement]: statements.md
349363
[temporary]: expressions.md#temporaries
350364
[variable]: variables.md
@@ -362,7 +376,7 @@ variable or field from being dropped automatically.
362376
[tuple struct pattern]: patterns.md#tuple-struct-patterns
363377

364378
[array expression]: expressions/array-expr.md#array-expressions
365-
[block expressions]: expressions/block-expr.md
379+
[block expression]: expressions/block-expr.md
366380
[borrow expression]: expressions/operator-expr.md#borrow-operators
367381
[cast expression]: expressions/operator-expr.md#type-cast-expressions
368382
[struct expression]: expressions/struct-expr.md

0 commit comments

Comments
 (0)