Skip to content

Commit f37072d

Browse files
committed
Address review feedback
1 parent 663b670 commit f37072d

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
@@ -67,7 +67,8 @@ Given a function, or closure, there are drop scopes for:
6767
* Each [statement]
6868
* Each [expression]
6969
* Each block, including the function body
70-
* [Block expressions] the expression and block scopes are the same scope.
70+
* In the case of a [block expression], the scope for the block and the
71+
expression are the same scope.
7172
* Each arm of a `match` expression
7273

7374
Drop scopes are nested within one another as follows. When multiple scopes are
@@ -78,13 +79,14 @@ from the inside outwards.
7879
* The function body block is contained within the scope of the entire function.
7980
* The parent of the expression in an expression statement is the scope of the
8081
statement.
81-
* The parent of the initializer of a `let` statement is the `let`
82-
statement's scope.
82+
* The parent of the initializer of a [`let` statement] is the `let` statement's
83+
scope.
8384
* The parent of a statement scope is the scope of the block that contains the
8485
statement.
8586
* The parent of the expression for a `match` guard is the scope of the arm that
8687
the guard is for.
87-
* The parent of the expression for a given `match` arm is that arm's scope.
88+
* The parent of the expression after the `=>` in a `match` expression is the
89+
scope of the arm that it's in.
8890
* The parent of the arm scope is the scope of the `match` expression that it
8991
belongs to.
9092
* The parent of all other scopes is the scope of the immediately enclosing
@@ -93,8 +95,8 @@ from the inside outwards.
9395
### Scopes of function parameters
9496

9597
All function parameters are in the scope of the entire function body, so are
96-
dropped last when evaluating the function. Actual function parameters are
97-
dropped after any named parameters that are bound to parts of it.
98+
dropped last when evaluating the function. Each actual function parameter is
99+
dropped after any bindings introduced in that parameter's pattern.
98100

99101
```rust
100102
# struct PrintOnDrop(&'static str);
@@ -120,8 +122,8 @@ patterns_in_parameters(
120122

121123
Local variables declared in a `let` statement are associated to the scope of
122124
the block that contains the `let` statement. Local variables declared in a
123-
`match` expression are associated to the arm scope of the `match` arm that they are declared
124-
in.
125+
`match` expression are associated to the arm scope of the `match` arm that they
126+
are declared in.
125127

126128
```rust
127129
# struct PrintOnDrop(&'static str);
@@ -137,14 +139,14 @@ let declared_first = PrintOnDrop("Dropped last in outer scope");
137139
let declared_last = PrintOnDrop("Dropped first in outer scope");
138140
```
139141

140-
If multiple patterns are used in the same arm for a `match` expression, then an unspecified
141-
pattern will be used to determine the drop order.
142+
If multiple patterns are used in the same arm for a `match` expression, then an
143+
unspecified pattern will be used to determine the drop order.
142144

143145
### Temporary scopes
144146

145147
The *temporary scope* of an expression is the scope that is used for the
146148
temporary variable that holds the result of that expression when used in a
147-
[place context], unless it is promoted to a `static`.
149+
[place context], unless it is [promoted].
148150

149151
Apart from lifetime extension, the temporary scope of an expression is the
150152
smallest scope that contains the expression and is for one of the following:
@@ -158,9 +160,16 @@ smallest scope that contains the expression and is for one of the following:
158160
* The expression for a match arm.
159161
* The second operand of a [lazy boolean expression].
160162

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

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

242251
### Temporary lifetime extension
243252

253+
> **Note**: The exact rules for temporary lifetime extension are subject to
254+
> change. This is describes the current behavior only.
255+
244256
The temporary scopes for expressions in `let` statements are sometimes
245257
*extended* to the scope of the block containing the `let` statement. This is
246258
done when the usual temporary scope would be too small, based on certain
@@ -283,11 +295,11 @@ expression which is one of the following:
283295
* The operand(s) of an extending [array][array expression], [cast][cast
284296
expression], [braced struct][struct expression], or [tuple][tuple expression]
285297
expression.
286-
* The final expression of any extending [block expression][block expressions].
298+
* The final expression of any extending [block expression].
287299

288300
So the borrow expressions in `&mut 0`, `(&1, &mut 2)`, and `Some { 0: &mut 3 }`
289-
are all extending expressions, while the borrows in `&0 + &1` and
290-
`Some(&mut 0)` are not.
301+
are all extending expressions. The borrows in `&0 + &1` and `Some(&mut 0)` are
302+
not: the latter is syntactically a function call expression.
291303

292304
The operand of any extending borrow expression has its temporary scope
293305
extended.
@@ -341,6 +353,8 @@ variable or field from being dropped automatically.
341353
[interior mutability]: interior-mutability.md
342354
[lazy boolean expression]: expressions/operator-expr.md#lazy-boolean-operators
343355
[place context]: expressions.md#place-expressions-and-value-expressions
356+
[promoted]: destructors.md#constant-promotion
357+
[scrutinee]: glossary.md#scrutinee
344358
[statement]: statements.md
345359
[temporary]: expressions.md#temporaries
346360
[variable]: variables.md
@@ -358,7 +372,7 @@ variable or field from being dropped automatically.
358372
[tuple struct pattern]: patterns.md#tuple-struct-patterns
359373

360374
[array expression]: expressions/array-expr.md#array-expressions
361-
[block expressions]: expressions/block-expr.md
375+
[block expression]: expressions/block-expr.md
362376
[borrow expression]: expressions/operator-expr.md#borrow-operators
363377
[cast expression]: expressions/operator-expr.md#type-cast-expressions
364378
[struct expression]: expressions/struct-expr.md

0 commit comments

Comments
 (0)