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
Copy file name to clipboardExpand all lines: src/types/closure.md
+12-7Lines changed: 12 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -331,7 +331,7 @@ If a closure captures a field of a composite types such as structs, tuples, and
331
331
* Input:
332
332
* Analyzing the closure C yields a mapping of `Place -> Mode` that are accessed
333
333
* Access mode is `ref`, `ref uniq`, `ref mut`, or `by-value` (ordered least to max)
334
-
* For a `Place` that is used in two different acess modes within the same closure, the mode reported from closure analysis is the maximum access mode.
334
+
* For a `Place` that is used in two different access modes within the same closure, the mode reported from closure analysis is the maximum access mode.
335
335
* Note: `ByValue` use of a `Copy` type is seen as a `ref` access mode.
336
336
* Closure mode is `ref` or `move`
337
337
* Output:
@@ -397,6 +397,8 @@ If a closure captures a field of a composite types such as structs, tuples, and
397
397
398
398
### box-mut
399
399
400
+
This test shows how a `move` closure can sometimes capture values by mutable reference, if they are reached via a `&mut` reference.
401
+
400
402
```rust
401
403
structFoo { x:i32 }
402
404
@@ -419,10 +421,10 @@ fn box_mut() {
419
421
<!-- ignore: Omit error about unterminated string literal when representing c_prime -->
420
422
```ignore
421
423
Closure mode = move
422
-
C = {
424
+
C_in = {
423
425
(ref mut, (*(*bx)).x)
424
426
}
425
-
C' = C
427
+
C_out = C_in
426
428
```
427
429
428
430
Output is the same: `C' = C`
@@ -453,13 +455,16 @@ fn main() {
453
455
<!-- ignore: Omit error about unterminated string literal when representing c_prime -->
454
456
```ignore
455
457
Closure mode = ref
456
-
C = {
458
+
C_in = {
457
459
(ref mut, packed)
458
460
}
459
-
C' = C
461
+
C_out = C_in
460
462
```
461
463
462
464
### Optimization-Edge-Case
465
+
466
+
This test shows an interesting edge case. Normally, when we see a borrow of something behind a shared reference (`&T`), we truncate to capture the entire reference, because that is more efficient (and we can always use that reference to reach all the data it refers to). However, in the case where we are dereferencing two shared references, we have to be sure to preserve the full path, since otherwise the resulting closure could have a shorter lifetime than is necessary.
0 commit comments