Skip to content

Commit 513f357

Browse files
authored
Merge pull request #2 from nikomatsakis/rfc2229-info
updates to rfc2229 info Pr
2 parents fb73551 + 6a236ad commit 513f357

File tree

1 file changed

+83
-35
lines changed

1 file changed

+83
-35
lines changed

src/rust-2021/disjoint-capture-in-closures.md

Lines changed: 83 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,92 @@
33
## Summary
44

55
- `|| a.x + 1` now captures only `a.x` instead of `a`.
6-
- This can subtly change the drop order of things and whether auto traits are applied to closures or not.
6+
- This can cause things to be dropped at different times or affect whether closures implement traits like `Send` or `Clone`.
7+
- If possible changes are detected, `cargo fix` will insert statements like `let _ = &a` to force a closure to capture the entire variable.
78

89
## Details
910

1011
[Closures](https://doc.rust-lang.org/book/ch13-01-closures.html)
1112
automatically capture anything that you refer to from within their body.
1213
For example, `|| a + 1` automatically captures a reference to `a` from the surrounding context.
1314

14-
Currently, this applies to whole structs, even when only using one field.
15+
In Rust 2018 and before, closures capture entire variables, even if the closure only uses one field.
1516
For example, `|| a.x + 1` captures a reference to `a` and not just `a.x`.
16-
In some situations, this is a problem.
17-
When a field of the struct is already borrowed (mutably) or moved out of,
18-
the other fields can no longer be used in a closure,
19-
since that would capture the whole struct, which is no longer available.
17+
Capturing `a` in its entirety prevents mutation or moves from other fields of `a`, so that code like this does not compile:
2018

2119
```rust,ignore
2220
let a = SomeStruct::new();
23-
2421
drop(a.x); // Move out of one field of the struct
25-
2622
println!("{}", a.y); // Ok: Still use another field of the struct
27-
2823
let c = || println!("{}", a.y); // Error: Tries to capture all of `a`
2924
c();
3025
```
3126

32-
Starting in Rust 2021, closures will only capture the fields that they use, eliminating common borrow check errors.
33-
So, the above example will compile fine in Rust 2021.
27+
Starting in Rust 2021, closures captures are more precise. Typically they will only capture the fields they use (in some cases, they might capture more than just what they use, see the Rust reference for full details). Therefore, the above example will compile fine in Rust 2021.
28+
29+
Disjoint capture was proposed as part of [RFC 2229](https://github.com/rust-lang/rfcs/blob/master/text/2229-capture-disjoint-fields.md) and the RFC contains details about the motivation.
30+
31+
## Migrating to Rust 2021
3432

35-
Disjoint capture was proposed as part of [RFC 2229](https://github.com/rust-lang/rfcs/blob/master/text/2229-capture-disjoint-fields.md), and we suggest reading the RFC to better understand motivations behind the feature.
33+
Changing the variables captured by a closure can cause programs to change behavior or to stop compiling in two cases:
3634

37-
## Changes to semantics because of disjoint capture
38-
Disjoint captures introduces a minor breaking change to the language. This means that there might be observable changes or valid Rust 2018 code that fails to compile once you move to Rust Edition 2021.
35+
- changes to drop order, or when destructors run ([details](#drop-order));
36+
- changes to which traits a closure implements ([details](#trait-implementations)).
3937

4038
When running `cargo fix --edition`, Cargo will update the closures in your code to help you migrate to Rust 2021, as described below in [Migrations](#migrations).
39+
40+
Whenever any of the scenarios below are detected, `cargo fix` will insert a "dummy let" into your closure to force it to capture the entire variable:
41+
42+
```rust
43+
let x = (vec![], vec![]);
44+
let c = move || {
45+
// "Dummy let" that forces `x` to be captured in its entirety
46+
let _ = &x;
47+
48+
// Otherwise, only `x.0` would be captured here
49+
println!("{:?}", x.0);
50+
};
51+
```
52+
53+
This is a conservative analysis: in many cases, these dummy lets can be safely removed and your program will work fine.
54+
4155
### Wild Card Patterns
42-
Closures now only capture data that needs to be read, which means the following closures will not capture `x`
56+
57+
Closures now only capture data that needs to be read, which means the following closures will not capture `x`:
4358

4459
```rust
4560
let x = 10;
4661
let c = || {
47-
let _ = x;
62+
let _ = x; // no-op
4863
};
4964

5065
let c = || match x {
5166
_ => println!("Hello World!")
5267
};
5368
```
5469

70+
The `let _ = x` statement here is a no-op, since the `_` pattern completely ignores the right-hand side, and `x` is a reference to a place in memory (in this case, a variable).
71+
72+
This change by itself (capturing fewer values) doesn't trigger any suggestions, but it may do so in conjunction with the "drop order" change below.
73+
74+
**Subtle:** There are other similar expressions, such as the "dummy lets" `let _ = &x` that we insert, which are not no-ops. This is because the right-hand side (`&x`) is not a reference to a place in memory, but rather an expression that must first be evaluated (and whose result is then discarded).
75+
5576
### Drop Order
56-
Since only a part of a variable might be captured instead of the entire variable, when different fields or elements (in case of tuple) get drop, the drop order might be affected.
77+
78+
When a closure takes ownership of a value from a variable `t`, that value is then dropped when the closure is dropped, and not when the variable `t` goes out of scope:
79+
80+
```rust
81+
# fn move_value<T>(_: T){}
82+
{
83+
let t = (vec![0], vec![0]);
84+
85+
{
86+
let c = || move_value(t); // t is moved here
87+
} // c is dropped, which drops the tuple `t` as well
88+
} // t goes out of scope here
89+
```
90+
91+
The above code will run the same in both Rust 2018 and Rust 2021. However, in cases where the closure only takes ownership of _part_ of a variable, there can be differences:
5792

5893
```rust
5994
# fn move_value<T>(_: T){}
@@ -62,22 +97,47 @@ Since only a part of a variable might be captured instead of the entire variable
6297

6398
{
6499
let c = || {
65-
move_value(t.0); // t.0 is moved here
100+
// In Rust 2018, captures all of `t`.
101+
// In Rust 2021, captures only `t.0`
102+
move_value(t.0);
66103
};
67-
} // c and t.0 dropped here
68-
} // t.1 dropped here
69104

105+
// In Rust 2018, `c` (and `t`) are both dropped when we
106+
// exit this block.
107+
//
108+
// In Rust 2021, `c` and `t.0` are both dropped when we
109+
// exit this block.
110+
}
111+
112+
// In Rust 2018, the value from `t` has been moved and is
113+
// not dropped.
114+
//
115+
// In Rust 2021, the value from `t.0` has been moved, but `t.1`
116+
// remains, so it will be dropped here.
117+
}
70118
```
71119

120+
In most cases, dropping values at different times just affects when memory is freed and is not important. However, some `Drop` impls (aka, destructors) have side-effects, and changing the drop order in those cases can alter the semantics of your program. In such cases, the compiler will suggest inserting a dummy `let` to force the entire variable to be captured.
72121

73-
### Auto Traits
74-
Structs or tuples that implement auto traits and are passed along in a closure may no longer guarantee that the closure can also implement those auto traits.
122+
### Trait implementations
123+
124+
Closures automatically implement the following traits based on what values they capture:
125+
126+
- [`Clone`]: if all captured values are [`Clone`].
127+
- [Auto traits] like [`Send`], [`Sync`], and [`UnwindSafe`]: if all captured values implement the given trait.
128+
129+
[auto traits]: https://doc.rust-lang.org/nightly/reference/special-types-and-traits.html#auto-traits
130+
[`clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html
131+
[`send`]: https://doc.rust-lang.org/std/marker/trait.Send.html
132+
[`sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html
133+
[`unwindsafe`]: https://doc.rust-lang.org/std/marker/trait.UnwindSafe.html
134+
135+
In Rust 2021, since different values are being captured, this can affect what traits a closure will implement. The migration lints test each closure to see whether it would have implemented a given trait before and whether it still implements it now; if they find that a trait used to be implemented but no longer is, then "dummy lets" are inserted.
75136

76137
For instance, a common way to allow passing around raw pointers between threads is to wrap them in a struct and then implement `Send`/`Sync` auto trait for the wrapper. The closure that is passed to `thread::spawn` uses the specific fields within the wrapper but the entire wrapper is captured regardless. Since the wrapper is `Send`/`Sync`, the code is considered safe and therefore compiles successfully.
77138

78139
With disjoint captures, only the specific field mentioned in the closure gets captured, which wasn't originally `Send`/`Sync` defeating the purpose of the wrapper.
79140

80-
81141
```rust
82142
use std::thread;
83143

@@ -94,15 +154,3 @@ let c = thread::spawn(move || {
94154
}
95155
}); // Closure captured px.0 which is not Send
96156
```
97-
98-
## Migrations
99-
100-
This new behavior is only activated starting in the 2021 edition,
101-
since it can change the order in which fields are dropped and can impact auto trait usage with closures.
102-
103-
When running `cargo fix --edition`, the closures in your code may be updated so that they will retain the old behavior on both the 2018 and 2021 editions.
104-
It does this by enabling the `disjoint_capture_migration` lint which adds statements like `let _ = &a;` inside the closure to force the entire variable to be captured as before.
105-
106-
After migrating, it is recommended to inspect the changes and see if they are necessary.
107-
After changing the edition to 2021, you can try to remove the new statements and test that the closure works as expected.
108-
You should review these closures, and ensure that the changes described above will not cause any problems.

0 commit comments

Comments
 (0)