Skip to content

Commit 4b1cb73

Browse files
authored
Rollup merge of #90597 - nikomatsakis:issue-90465, r=wesleywiser
Warn for variables that are no longer captured r? `@wesleywiser` cc `@rust-lang/wg-rfc-2229` Fixes #90465
2 parents cd24ffb + 4154e8a commit 4b1cb73

13 files changed

+328
-147
lines changed

compiler/rustc_typeck/src/check/upvar.rs

+153-78
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// run-rustfix
2+
3+
#![deny(rust_2021_incompatible_closure_captures)]
4+
//~^ NOTE lint level is defined here
5+
6+
fn main() {
7+
struct Foo(u32);
8+
impl Drop for Foo {
9+
fn drop(&mut self) {
10+
println!("dropped {}", self.0);
11+
}
12+
}
13+
14+
let f0 = Foo(0);
15+
let f1 = Foo(1);
16+
17+
let c0 = move || {
18+
let _ = &f0;
19+
//~^ ERROR changes to closure capture in Rust 2021 will affect drop order
20+
//~| NOTE for more information
21+
let _ = f0;
22+
//~^ NOTE in Rust 2018, this causes the closure to capture `f0`, but in Rust 2021, it has no effect
23+
};
24+
25+
let c1 = move || {
26+
let _ = &f1;
27+
};
28+
29+
println!("dropping 0");
30+
drop(c0);
31+
println!("dropping 1");
32+
drop(c1);
33+
println!("dropped all");
34+
}
35+
//~^ NOTE in Rust 2018, `f0` is dropped here along with the closure, but in Rust 2021 `f0` is not part of the closure
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// run-rustfix
2+
3+
#![deny(rust_2021_incompatible_closure_captures)]
4+
//~^ NOTE lint level is defined here
5+
6+
fn main() {
7+
struct Foo(u32);
8+
impl Drop for Foo {
9+
fn drop(&mut self) {
10+
println!("dropped {}", self.0);
11+
}
12+
}
13+
14+
let f0 = Foo(0);
15+
let f1 = Foo(1);
16+
17+
let c0 = move || {
18+
//~^ ERROR changes to closure capture in Rust 2021 will affect drop order
19+
//~| NOTE for more information
20+
let _ = f0;
21+
//~^ NOTE in Rust 2018, this causes the closure to capture `f0`, but in Rust 2021, it has no effect
22+
};
23+
24+
let c1 = move || {
25+
let _ = &f1;
26+
};
27+
28+
println!("dropping 0");
29+
drop(c0);
30+
println!("dropping 1");
31+
drop(c1);
32+
println!("dropped all");
33+
}
34+
//~^ NOTE in Rust 2018, `f0` is dropped here along with the closure, but in Rust 2021 `f0` is not part of the closure
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: changes to closure capture in Rust 2021 will affect drop order
2+
--> $DIR/issue-90465.rs:17:14
3+
|
4+
LL | let c0 = move || {
5+
| ^^^^^^^
6+
...
7+
LL | let _ = f0;
8+
| -- in Rust 2018, this causes the closure to capture `f0`, but in Rust 2021, it has no effect
9+
...
10+
LL | }
11+
| - in Rust 2018, `f0` is dropped here along with the closure, but in Rust 2021 `f0` is not part of the closure
12+
|
13+
note: the lint level is defined here
14+
--> $DIR/issue-90465.rs:3:9
15+
|
16+
LL | #![deny(rust_2021_incompatible_closure_captures)]
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/disjoint-capture-in-closures.html>
19+
help: add a dummy let to cause `f0` to be fully captured
20+
|
21+
LL ~ let c0 = move || {
22+
LL + let _ = &f0;
23+
|
24+
25+
error: aborting due to previous error
26+

src/test/ui/closures/2229_closure_analysis/migrations/auto_traits.fixed

+7-6
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ fn test_send_trait() {
2020
let mut f = 10;
2121
let fptr = SendPointer(&mut f as *mut i32);
2222
thread::spawn(move || { let _ = &fptr; unsafe {
23-
//~^ ERROR: `Send` trait implementation for closure
24-
//~| NOTE: in Rust 2018, this closure implements `Send` as `fptr` implements `Send`, but in Rust 2021, this closure will no longer implement `Send` as `fptr.0` does not implement `Send`
23+
//~^ ERROR: changes to closure capture
24+
//~| NOTE: in Rust 2018, this closure implements `Send`
2525
//~| NOTE: for more information, see
2626
//~| HELP: add a dummy let to cause `fptr` to be fully captured
2727
*fptr.0 = 20;
@@ -40,8 +40,9 @@ fn test_sync_trait() {
4040
let f = CustomInt(&mut f as *mut i32);
4141
let fptr = SyncPointer(f);
4242
thread::spawn(move || { let _ = &fptr; unsafe {
43-
//~^ ERROR: `Sync`, `Send` trait implementation for closure
44-
//~| NOTE: in Rust 2018, this closure implements `Sync`, `Send` as `fptr` implements `Sync`, `Send`, but in Rust 2021, this closure will no longer implement `Sync`, `Send` as `fptr.0.0` does not implement `Sync`, `Send`
43+
//~^ ERROR: changes to closure capture
44+
//~| NOTE: in Rust 2018, this closure implements `Sync`
45+
//~| NOTE: in Rust 2018, this closure implements `Send`
4546
//~| NOTE: for more information, see
4647
//~| HELP: add a dummy let to cause `fptr` to be fully captured
4748
*fptr.0.0 = 20;
@@ -65,8 +66,8 @@ fn test_clone_trait() {
6566
let f = U(S(Foo(0)), T(0));
6667
let c = || {
6768
let _ = &f;
68-
//~^ ERROR: `Clone` trait implementation for closure and drop order
69-
//~| NOTE: in Rust 2018, this closure implements `Clone` as `f` implements `Clone`, but in Rust 2021, this closure will no longer implement `Clone` as `f.1` does not implement `Clone`
69+
//~^ ERROR: changes to closure capture in Rust 2021 will affect drop order and which traits the closure implements
70+
//~| NOTE: in Rust 2018, this closure implements `Clone`
7071
//~| NOTE: for more information, see
7172
//~| HELP: add a dummy let to cause `f` to be fully captured
7273
let f_1 = f.1;

src/test/ui/closures/2229_closure_analysis/migrations/auto_traits.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ fn test_send_trait() {
2020
let mut f = 10;
2121
let fptr = SendPointer(&mut f as *mut i32);
2222
thread::spawn(move || unsafe {
23-
//~^ ERROR: `Send` trait implementation for closure
24-
//~| NOTE: in Rust 2018, this closure implements `Send` as `fptr` implements `Send`, but in Rust 2021, this closure will no longer implement `Send` as `fptr.0` does not implement `Send`
23+
//~^ ERROR: changes to closure capture
24+
//~| NOTE: in Rust 2018, this closure implements `Send`
2525
//~| NOTE: for more information, see
2626
//~| HELP: add a dummy let to cause `fptr` to be fully captured
2727
*fptr.0 = 20;
@@ -40,8 +40,9 @@ fn test_sync_trait() {
4040
let f = CustomInt(&mut f as *mut i32);
4141
let fptr = SyncPointer(f);
4242
thread::spawn(move || unsafe {
43-
//~^ ERROR: `Sync`, `Send` trait implementation for closure
44-
//~| NOTE: in Rust 2018, this closure implements `Sync`, `Send` as `fptr` implements `Sync`, `Send`, but in Rust 2021, this closure will no longer implement `Sync`, `Send` as `fptr.0.0` does not implement `Sync`, `Send`
43+
//~^ ERROR: changes to closure capture
44+
//~| NOTE: in Rust 2018, this closure implements `Sync`
45+
//~| NOTE: in Rust 2018, this closure implements `Send`
4546
//~| NOTE: for more information, see
4647
//~| HELP: add a dummy let to cause `fptr` to be fully captured
4748
*fptr.0.0 = 20;
@@ -64,8 +65,8 @@ impl Clone for U {
6465
fn test_clone_trait() {
6566
let f = U(S(Foo(0)), T(0));
6667
let c = || {
67-
//~^ ERROR: `Clone` trait implementation for closure and drop order
68-
//~| NOTE: in Rust 2018, this closure implements `Clone` as `f` implements `Clone`, but in Rust 2021, this closure will no longer implement `Clone` as `f.1` does not implement `Clone`
68+
//~^ ERROR: changes to closure capture in Rust 2021 will affect drop order and which traits the closure implements
69+
//~| NOTE: in Rust 2018, this closure implements `Clone`
6970
//~| NOTE: for more information, see
7071
//~| HELP: add a dummy let to cause `f` to be fully captured
7172
let f_1 = f.1;

src/test/ui/closures/2229_closure_analysis/migrations/auto_traits.stderr

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: changes to closure capture in Rust 2021 will affect `Send` trait implementation for closure
1+
error: changes to closure capture in Rust 2021 will affect which traits the closure implements
22
--> $DIR/auto_traits.rs:22:19
33
|
44
LL | thread::spawn(move || unsafe {
5-
| ^^^^^^^^^^^^^^ in Rust 2018, this closure implements `Send` as `fptr` implements `Send`, but in Rust 2021, this closure will no longer implement `Send` as `fptr.0` does not implement `Send`
5+
| ^^^^^^^^^^^^^^ in Rust 2018, this closure implements `Send` as `fptr` implements `Send`, but in Rust 2021, this closure will no longer implement `Send` because `fptr` is not fully captured and `fptr.0` does not implement `Send`
66
...
77
LL | *fptr.0 = 20;
88
| ------- in Rust 2018, this closure captures all of `fptr`, but in Rust 2021, it will only capture `fptr.0`
@@ -23,11 +23,14 @@ LL |
2323
LL | *fptr.0 = 20;
2424
...
2525

26-
error: changes to closure capture in Rust 2021 will affect `Sync`, `Send` trait implementation for closure
26+
error: changes to closure capture in Rust 2021 will affect which traits the closure implements
2727
--> $DIR/auto_traits.rs:42:19
2828
|
2929
LL | thread::spawn(move || unsafe {
30-
| ^^^^^^^^^^^^^^ in Rust 2018, this closure implements `Sync`, `Send` as `fptr` implements `Sync`, `Send`, but in Rust 2021, this closure will no longer implement `Sync`, `Send` as `fptr.0.0` does not implement `Sync`, `Send`
30+
| ^^^^^^^^^^^^^^
31+
| |
32+
| in Rust 2018, this closure implements `Sync` as `fptr` implements `Sync`, but in Rust 2021, this closure will no longer implement `Sync` because `fptr` is not fully captured and `fptr.0.0` does not implement `Sync`
33+
| in Rust 2018, this closure implements `Send` as `fptr` implements `Send`, but in Rust 2021, this closure will no longer implement `Send` because `fptr` is not fully captured and `fptr.0.0` does not implement `Send`
3134
...
3235
LL | *fptr.0.0 = 20;
3336
| --------- in Rust 2018, this closure captures all of `fptr`, but in Rust 2021, it will only capture `fptr.0.0`
@@ -40,14 +43,14 @@ LL |
4043
LL |
4144
LL |
4245
LL |
43-
LL | *fptr.0.0 = 20;
46+
LL |
4447
...
4548

46-
error: changes to closure capture in Rust 2021 will affect `Clone` trait implementation for closure and drop order
47-
--> $DIR/auto_traits.rs:66:13
49+
error: changes to closure capture in Rust 2021 will affect drop order and which traits the closure implements
50+
--> $DIR/auto_traits.rs:67:13
4851
|
4952
LL | let c = || {
50-
| ^^ in Rust 2018, this closure implements `Clone` as `f` implements `Clone`, but in Rust 2021, this closure will no longer implement `Clone` as `f.1` does not implement `Clone`
53+
| ^^ in Rust 2018, this closure implements `Clone` as `f` implements `Clone`, but in Rust 2021, this closure will no longer implement `Clone` because `f` is not fully captured and `f.1` does not implement `Clone`
5154
...
5255
LL | let f_1 = f.1;
5356
| --- in Rust 2018, this closure captures all of `f`, but in Rust 2021, it will only capture `f.1`

src/test/ui/closures/2229_closure_analysis/migrations/mir_calls_to_shims.fixed

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ where
1919
let f = panic::AssertUnwindSafe(f);
2020
let result = panic::catch_unwind(move || {
2121
let _ = &f;
22-
//~^ ERROR: `UnwindSafe`, `RefUnwindSafe` trait implementation for closure
23-
//~| NOTE: in Rust 2018, this closure implements `UnwindSafe`, `RefUnwindSafe` as `f` implements `UnwindSafe`, `RefUnwindSafe`, but in Rust 2021, this closure will no longer implement `UnwindSafe`, `RefUnwindSafe` as `f.0` does not implement `UnwindSafe`, `RefUnwindSafe`
22+
//~^ ERROR: changes to closure capture in Rust 2021 will affect which traits the closure implements [rust_2021_incompatible_closure_captures]
23+
//~| NOTE: in Rust 2018, this closure implements `UnwindSafe`
24+
//~| NOTE: in Rust 2018, this closure implements `RefUnwindSafe`
2425
//~| NOTE: for more information, see
2526
//~| HELP: add a dummy let to cause `f` to be fully captured
2627
f.0()

src/test/ui/closures/2229_closure_analysis/migrations/mir_calls_to_shims.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ where
1818
{
1919
let f = panic::AssertUnwindSafe(f);
2020
let result = panic::catch_unwind(move || {
21-
//~^ ERROR: `UnwindSafe`, `RefUnwindSafe` trait implementation for closure
22-
//~| NOTE: in Rust 2018, this closure implements `UnwindSafe`, `RefUnwindSafe` as `f` implements `UnwindSafe`, `RefUnwindSafe`, but in Rust 2021, this closure will no longer implement `UnwindSafe`, `RefUnwindSafe` as `f.0` does not implement `UnwindSafe`, `RefUnwindSafe`
21+
//~^ ERROR: changes to closure capture in Rust 2021 will affect which traits the closure implements [rust_2021_incompatible_closure_captures]
22+
//~| NOTE: in Rust 2018, this closure implements `UnwindSafe`
23+
//~| NOTE: in Rust 2018, this closure implements `RefUnwindSafe`
2324
//~| NOTE: for more information, see
2425
//~| HELP: add a dummy let to cause `f` to be fully captured
2526
f.0()

src/test/ui/closures/2229_closure_analysis/migrations/mir_calls_to_shims.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
error: changes to closure capture in Rust 2021 will affect `UnwindSafe`, `RefUnwindSafe` trait implementation for closure
1+
error: changes to closure capture in Rust 2021 will affect which traits the closure implements
22
--> $DIR/mir_calls_to_shims.rs:20:38
33
|
44
LL | let result = panic::catch_unwind(move || {
5-
| ^^^^^^^ in Rust 2018, this closure implements `UnwindSafe`, `RefUnwindSafe` as `f` implements `UnwindSafe`, `RefUnwindSafe`, but in Rust 2021, this closure will no longer implement `UnwindSafe`, `RefUnwindSafe` as `f.0` does not implement `UnwindSafe`, `RefUnwindSafe`
5+
| ^^^^^^^
6+
| |
7+
| in Rust 2018, this closure implements `UnwindSafe` as `f` implements `UnwindSafe`, but in Rust 2021, this closure will no longer implement `UnwindSafe` because `f` is not fully captured and `f.0` does not implement `UnwindSafe`
8+
| in Rust 2018, this closure implements `RefUnwindSafe` as `f` implements `RefUnwindSafe`, but in Rust 2021, this closure will no longer implement `RefUnwindSafe` because `f` is not fully captured and `f.0` does not implement `RefUnwindSafe`
69
...
710
LL | f.0()
811
| --- in Rust 2018, this closure captures all of `f`, but in Rust 2021, it will only capture `f.0`

src/test/ui/closures/2229_closure_analysis/migrations/multi_diagnostics.fixed

+13-13
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ impl Foo {
1818
}
1919
}
2020

21-
2221
struct S(Foo);
2322

2423
#[derive(Clone)]
@@ -37,8 +36,8 @@ fn test_multi_issues() {
3736
let f2 = U(S(Foo::from("bar")), T(0));
3837
let c = || {
3938
let _ = (&f1, &f2);
40-
//~^ ERROR: `Clone` trait implementation for closure and drop order
41-
//~| NOTE: in Rust 2018, this closure implements `Clone` as `f1` implements `Clone`, but in Rust 2021, this closure will no longer implement `Clone` as `f1.0` does not implement `Clone`
39+
//~^ ERROR: changes to closure capture in Rust 2021
40+
//~| NOTE: in Rust 2018, this closure implements `Clone` as `f1` implements `Clone`
4241
//~| NOTE: for more information, see
4342
//~| HELP: add a dummy let to cause `f1`, `f2` to be fully captured
4443
let _f_1 = f1.0;
@@ -57,8 +56,8 @@ fn test_capturing_all_disjoint_fields_individually() {
5756
let f1 = U(S(Foo::from("foo")), T(0));
5857
let c = || {
5958
let _ = &f1;
60-
//~^ ERROR: `Clone` trait implementation for closure
61-
//~| NOTE: in Rust 2018, this closure implements `Clone` as `f1` implements `Clone`, but in Rust 2021, this closure will no longer implement `Clone` as `f1.0` does not implement `Clone`
59+
//~^ ERROR: changes to closure capture in Rust 2021 will affect which traits the closure implements [rust_2021_incompatible_closure_captures]
60+
//~| NOTE: in Rust 2018, this closure implements `Clone` as `f1` implements `Clone`
6261
//~| NOTE: for more information, see
6362
//~| HELP: add a dummy let to cause `f1` to be fully captured
6463
let _f_1 = f1.0;
@@ -83,9 +82,9 @@ fn test_capturing_several_disjoint_fields_individually_1() {
8382
let f1 = U1(S(Foo::from("foo")), T(0), S(Foo::from("bar")));
8483
let c = || {
8584
let _ = &f1;
86-
//~^ ERROR: `Clone` trait implementation for closure
87-
//~| NOTE: in Rust 2018, this closure implements `Clone` as `f1` implements `Clone`, but in Rust 2021, this closure will no longer implement `Clone` as `f1.0` does not implement `Clone`
88-
//~| NOTE: in Rust 2018, this closure implements `Clone` as `f1` implements `Clone`, but in Rust 2021, this closure will no longer implement `Clone` as `f1.2` does not implement `Clone`
85+
//~^ ERROR: changes to closure capture in Rust 2021 will affect which traits the closure implements [rust_2021_incompatible_closure_captures]
86+
//~| NOTE: in Rust 2018, this closure implements `Clone` as `f1` implements `Clone`
87+
//~| NOTE: in Rust 2018, this closure implements `Clone` as `f1` implements `Clone`
8988
//~| NOTE: for more information, see
9089
//~| HELP: add a dummy let to cause `f1` to be fully captured
9190
let _f_0 = f1.0;
@@ -103,8 +102,8 @@ fn test_capturing_several_disjoint_fields_individually_2() {
103102
let f1 = U1(S(Foo::from("foo")), T(0), S(Foo::from("bar")));
104103
let c = || {
105104
let _ = &f1;
106-
//~^ ERROR: `Clone` trait implementation for closure and drop order
107-
//~| NOTE: in Rust 2018, this closure implements `Clone` as `f1` implements `Clone`, but in Rust 2021, this closure will no longer implement `Clone` as `f1.0` does not implement `Clone`
105+
//~^ ERROR: changes to closure capture in Rust 2021 will affect drop order and which traits the closure implements
106+
//~| NOTE: in Rust 2018, this closure implements `Clone` as `f1` implements `Clone`
108107
//~| NOTE: for more information, see
109108
//~| HELP: add a dummy let to cause `f1` to be fully captured
110109
let _f_0 = f1.0;
@@ -136,9 +135,10 @@ fn test_multi_traits_issues() {
136135
let mut f2 = 10;
137136
let fptr2 = SendPointer(&mut f2 as *mut i32);
138137
thread::spawn(move || { let _ = (&fptr1, &fptr2); unsafe {
139-
//~^ ERROR: `Sync`, `Send` trait implementation for closure
140-
//~| NOTE: in Rust 2018, this closure implements `Sync`, `Send` as `fptr1` implements `Sync`, `Send`, but in Rust 2021, this closure will no longer implement `Sync`, `Send` as `fptr1.0.0` does not implement `Sync`, `Send`
141-
//~| NOTE: in Rust 2018, this closure implements `Send` as `fptr2` implements `Send`, but in Rust 2021, this closure will no longer implement `Send` as `fptr2.0` does not implement `Send`
138+
//~^ ERROR: changes to closure capture in Rust 2021
139+
//~| NOTE: in Rust 2018, this closure implements `Sync` as `fptr1` implements `Sync`
140+
//~| NOTE: in Rust 2018, this closure implements `Send` as `fptr1` implements `Send`
141+
//~| NOTE: in Rust 2018, this closure implements `Send` as `fptr2` implements `Send`
142142
//~| NOTE: for more information, see
143143
//~| HELP: add a dummy let to cause `fptr1`, `fptr2` to be fully captured
144144
*fptr1.0.0 = 20;

0 commit comments

Comments
 (0)