Skip to content

Commit 6a40f1b

Browse files
committed
Provide more information about the RFC2229 and migration information
1 parent 86cdd2b commit 6a40f1b

File tree

1 file changed

+68
-8
lines changed

1 file changed

+68
-8
lines changed

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

+68-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
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.
6+
- This can subtly change the drop order of things, and auto traits usage with closures.
77

88
## Details
99

@@ -29,12 +29,72 @@ let c = || println!("{}", a.y); // Error: Tries to capture all of `a`
2929
c();
3030
```
3131

32-
Starting in Rust 2021, closures will only capture the fields that they use.
32+
Starting in Rust 2021, closures will only capture the fields that they use, eliminating common borrow check errors.
3333
So, the above example will compile fine in Rust 2021.
3434

35-
This new behavior is only activated in the new edition,
36-
since it can change the order in which fields are dropped.
37-
As for all edition changes, an automatic migration is available,
38-
which will update your closures for which this matters.
39-
It can insert `let _ = &a;` inside the closure to force the entire
40-
struct to be captured as before.
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.
36+
37+
## Changes to semantics because of disjoint capture
38+
`#[feature(capture_disjoint_fields)]` introduces (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. You can use `cargo fix` with the `disjoint_capture_migrations` lint to migrate your existing code to Rust 2021.
39+
### Wild Card Patterns
40+
Closures now only capture data that needs to be read, which means the following closures will not capture `x`
41+
42+
```rust
43+
let x = 10;
44+
let c = || {
45+
let _ = x;
46+
};
47+
48+
let c = || match x {
49+
_ => prinln!("Hello World!")
50+
};
51+
```
52+
53+
### Drop Order
54+
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.
55+
56+
```rust
57+
{
58+
let t = (vec![0], vec![0]);
59+
60+
{
61+
let c = || {
62+
move(t.0);
63+
};
64+
} // c and t.0 dropped here
65+
} // t.1 dropped here
66+
67+
```
68+
69+
70+
### Auto Traits
71+
Structs or tuples that implement (an) auto trait(s) and are passed along in a closure may no longer guarantee that the closure can also implement that/those auto trait(s).
72+
73+
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.
74+
75+
With this feature only the specific field mentioned in the closure gets captured, which wasn't originally `Send`/`Sync` defeating the purpose of the wrapper.
76+
77+
78+
```rust
79+
struct Ptr(*mut i32);
80+
unsafe impl Send for Ptr;
81+
82+
83+
let mut x = 5;
84+
let px = (&mut x as *mut i32);
85+
86+
let c = thread::spawn(move || {
87+
*(px.0) += 10;
88+
}); // Closure captured px.0 which is not Send
89+
```
90+
91+
### Migrations
92+
93+
This new behavior is only activated in the new edition, or by specifically enabling it using `#[feature(capture_disjoint_fields)]`,
94+
since it can change the order in which fields are dropped and can impact auto trait usage with closures.
95+
96+
As for all edition changes, an automatic migration is available.
97+
If you would like to be warned of semantics change that may impact your code, you can [use the lint](https://doc.rust-lang.org/rustc/lints/levels.html) `disjoint_capture_migrations`. The lint is also supported with [cargo fix](https://doc.rust-lang.org/cargo/commands/cargo-fix.html) to automatically migrate your code.
98+
99+
The migration fix involves adding `let _ = &a;`, where `a` is the variable that needs to be fully captures, inside the closure to force the entire
100+
struct to be captured as before.

0 commit comments

Comments
 (0)