You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rather than discussing the built-in macros directly in the context of
extending expressions, and inspired by our section on implicit
borrows, let's instead add a concept of "borrow macro call
expressions". We'll then treat extending borrow macro call
expressions as extending expressions and extend the temporary scope of
operands to extending borrow macro call expression.
This seems a bit cleaner and better represents the notion that these
macros are only special in that they are a kind of borrow expression.
If a [borrow][borrow expression], [dereference][dereference expression],
402
-
[field][field expression], or [tuple indexing expression] has an extended
403
-
temporary scope then so does its operand. If an [indexing expression] has an
404
-
extended temporary scope then the indexed expression also has an extended
405
-
temporary scope.
401
+
If a [borrow], [dereference][dereference expression], [field][field expression], or [tuple indexing expression] has an extended temporary scope then so does its operand. If an [indexing expression] has an extended temporary scope then the indexed expression also has an extended temporary scope.
406
402
407
403
r[destructors.scope.lifetime-extension.patterns]
408
404
#### Extending based on patterns
@@ -479,26 +475,20 @@ For a let statement with an initializer, an *extending expression* is an
479
475
expression which is one of the following:
480
476
481
477
* The initializer expression.
482
-
* The operand of an extending [borrow expression].
478
+
* The operand of an extending [borrow operator] or [borrow macro] call expression.
483
479
* The operand(s) of an extending [array][array expression], [cast][cast
484
480
expression], [braced struct][struct expression], or [tuple][tuple expression]
485
481
expression.
486
482
* The arguments to an extending [tuple struct] or [tuple variant] constructor expression.
487
483
* The final expression of an extending [block expression] except for an [async block expression].
488
484
* The final expression of an extending [`if`] expression's consequent, `else if`, or `else` block.
489
485
* An arm expression of an extending [`match`] expression.
490
-
* The argument(s) to an extending [`pin!`] or [`format_args!`][macro invocation] expression.
491
486
492
487
So the borrow expressions in `&mut 0`, `(&1, &mut 2)`, and `Some(&mut 3)`
493
488
are all extending expressions. The borrows in `&0 + &1` and `f(&mut 0)` are not.
Copy file name to clipboardExpand all lines: src/expressions.md
+29Lines changed: 29 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -175,6 +175,7 @@ The following contexts are *place expression* contexts:
175
175
* The operand of a field expression.
176
176
* The indexed operand of an array indexing expression.
177
177
* The operand of any [implicit borrow].
178
+
* The operand of any [macro borrow expression].
178
179
* The initializer of a [let statement].
179
180
* The [scrutinee] of an [`if let`], [`match`][match], or [`while let`]
180
181
expression.
@@ -286,6 +287,24 @@ Implicit borrows may be taken in the following expressions:
286
287
* Operands of [comparison].
287
288
* Left operands of the [compound assignment].
288
289
290
+
r[expr.borrow-macros]
291
+
### Borrow macros
292
+
293
+
r[expr.borrow-macros.intro]
294
+
Calls to certain built-in macros act as [borrow expressions] with respect to some or all of the arguments. These [macro invocations] are *borrow macro call expressions*. The arguments borrowed are the *operands* of these borrow macro call expressions. As with other borrow expressions, these operands are [place expression contexts] and their [temporary scopes] are [extended][destructors.scope.lifetime-extension.exprs].
295
+
296
+
r[expr.borrow-macros.exprs]
297
+
The following are borrow macro call expressions:
298
+
299
+
- Calls to [`addr_of!`], [`addr_of_mut!`], or [`pin!`].
300
+
- Calls to [`format_args!`] with at least one argument following the format string.
301
+
302
+
r[expr.borrow-macros.operands]
303
+
The following are operands of borrow macro call expressions:
304
+
305
+
- The argument to [`addr_of!`], [`addr_of_mut!`], or [`pin!`].
306
+
- Any arguments following the format string to [`format_args!`].
307
+
289
308
r[expr.overload]
290
309
## Overloading Traits
291
310
@@ -310,14 +329,19 @@ They are never allowed before:
0 commit comments