Skip to content

Commit

Permalink
Rollup merge of #90597 - nikomatsakis:issue-90465, r=wesleywiser
Browse files Browse the repository at this point in the history
Warn for variables that are no longer captured

r? `@wesleywiser`

cc `@rust-lang/wg-rfc-2229`

Fixes #90465
  • Loading branch information
matthiaskrgr committed Nov 5, 2021
2 parents cd24ffb + 4154e8a commit 4b1cb73
Show file tree
Hide file tree
Showing 13 changed files with 328 additions and 147 deletions.
231 changes: 153 additions & 78 deletions compiler/rustc_typeck/src/check/upvar.rs

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions src/test/ui/closures/2229_closure_analysis/issue-90465.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// run-rustfix

#![deny(rust_2021_incompatible_closure_captures)]
//~^ NOTE lint level is defined here

fn main() {
struct Foo(u32);
impl Drop for Foo {
fn drop(&mut self) {
println!("dropped {}", self.0);
}
}

let f0 = Foo(0);
let f1 = Foo(1);

let c0 = move || {
let _ = &f0;
//~^ ERROR changes to closure capture in Rust 2021 will affect drop order
//~| NOTE for more information
let _ = f0;
//~^ NOTE in Rust 2018, this causes the closure to capture `f0`, but in Rust 2021, it has no effect
};

let c1 = move || {
let _ = &f1;
};

println!("dropping 0");
drop(c0);
println!("dropping 1");
drop(c1);
println!("dropped all");
}
//~^ NOTE in Rust 2018, `f0` is dropped here along with the closure, but in Rust 2021 `f0` is not part of the closure
34 changes: 34 additions & 0 deletions src/test/ui/closures/2229_closure_analysis/issue-90465.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// run-rustfix

#![deny(rust_2021_incompatible_closure_captures)]
//~^ NOTE lint level is defined here

fn main() {
struct Foo(u32);
impl Drop for Foo {
fn drop(&mut self) {
println!("dropped {}", self.0);
}
}

let f0 = Foo(0);
let f1 = Foo(1);

let c0 = move || {
//~^ ERROR changes to closure capture in Rust 2021 will affect drop order
//~| NOTE for more information
let _ = f0;
//~^ NOTE in Rust 2018, this causes the closure to capture `f0`, but in Rust 2021, it has no effect
};

let c1 = move || {
let _ = &f1;
};

println!("dropping 0");
drop(c0);
println!("dropping 1");
drop(c1);
println!("dropped all");
}
//~^ NOTE in Rust 2018, `f0` is dropped here along with the closure, but in Rust 2021 `f0` is not part of the closure
26 changes: 26 additions & 0 deletions src/test/ui/closures/2229_closure_analysis/issue-90465.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error: changes to closure capture in Rust 2021 will affect drop order
--> $DIR/issue-90465.rs:17:14
|
LL | let c0 = move || {
| ^^^^^^^
...
LL | let _ = f0;
| -- in Rust 2018, this causes the closure to capture `f0`, but in Rust 2021, it has no effect
...
LL | }
| - in Rust 2018, `f0` is dropped here along with the closure, but in Rust 2021 `f0` is not part of the closure
|
note: the lint level is defined here
--> $DIR/issue-90465.rs:3:9
|
LL | #![deny(rust_2021_incompatible_closure_captures)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/disjoint-capture-in-closures.html>
help: add a dummy let to cause `f0` to be fully captured
|
LL ~ let c0 = move || {
LL + let _ = &f0;
|

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ fn test_send_trait() {
let mut f = 10;
let fptr = SendPointer(&mut f as *mut i32);
thread::spawn(move || { let _ = &fptr; unsafe {
//~^ ERROR: `Send` trait implementation for closure
//~| 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`
//~^ ERROR: changes to closure capture
//~| NOTE: in Rust 2018, this closure implements `Send`
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `fptr` to be fully captured
*fptr.0 = 20;
Expand All @@ -40,8 +40,9 @@ fn test_sync_trait() {
let f = CustomInt(&mut f as *mut i32);
let fptr = SyncPointer(f);
thread::spawn(move || { let _ = &fptr; unsafe {
//~^ ERROR: `Sync`, `Send` trait implementation for closure
//~| 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`
//~^ ERROR: changes to closure capture
//~| NOTE: in Rust 2018, this closure implements `Sync`
//~| NOTE: in Rust 2018, this closure implements `Send`
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `fptr` to be fully captured
*fptr.0.0 = 20;
Expand All @@ -65,8 +66,8 @@ fn test_clone_trait() {
let f = U(S(Foo(0)), T(0));
let c = || {
let _ = &f;
//~^ ERROR: `Clone` trait implementation for closure and drop order
//~| 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`
//~^ ERROR: changes to closure capture in Rust 2021 will affect drop order and which traits the closure implements
//~| NOTE: in Rust 2018, this closure implements `Clone`
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `f` to be fully captured
let f_1 = f.1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ fn test_send_trait() {
let mut f = 10;
let fptr = SendPointer(&mut f as *mut i32);
thread::spawn(move || unsafe {
//~^ ERROR: `Send` trait implementation for closure
//~| 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`
//~^ ERROR: changes to closure capture
//~| NOTE: in Rust 2018, this closure implements `Send`
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `fptr` to be fully captured
*fptr.0 = 20;
Expand All @@ -40,8 +40,9 @@ fn test_sync_trait() {
let f = CustomInt(&mut f as *mut i32);
let fptr = SyncPointer(f);
thread::spawn(move || unsafe {
//~^ ERROR: `Sync`, `Send` trait implementation for closure
//~| 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`
//~^ ERROR: changes to closure capture
//~| NOTE: in Rust 2018, this closure implements `Sync`
//~| NOTE: in Rust 2018, this closure implements `Send`
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `fptr` to be fully captured
*fptr.0.0 = 20;
Expand All @@ -64,8 +65,8 @@ impl Clone for U {
fn test_clone_trait() {
let f = U(S(Foo(0)), T(0));
let c = || {
//~^ ERROR: `Clone` trait implementation for closure and drop order
//~| 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`
//~^ ERROR: changes to closure capture in Rust 2021 will affect drop order and which traits the closure implements
//~| NOTE: in Rust 2018, this closure implements `Clone`
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `f` to be fully captured
let f_1 = f.1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: changes to closure capture in Rust 2021 will affect `Send` trait implementation for closure
error: changes to closure capture in Rust 2021 will affect which traits the closure implements
--> $DIR/auto_traits.rs:22:19
|
LL | thread::spawn(move || unsafe {
| ^^^^^^^^^^^^^^ 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`
| ^^^^^^^^^^^^^^ 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`
...
LL | *fptr.0 = 20;
| ------- in Rust 2018, this closure captures all of `fptr`, but in Rust 2021, it will only capture `fptr.0`
Expand All @@ -23,11 +23,14 @@ LL |
LL | *fptr.0 = 20;
...

error: changes to closure capture in Rust 2021 will affect `Sync`, `Send` trait implementation for closure
error: changes to closure capture in Rust 2021 will affect which traits the closure implements
--> $DIR/auto_traits.rs:42:19
|
LL | thread::spawn(move || unsafe {
| ^^^^^^^^^^^^^^ 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`
| ^^^^^^^^^^^^^^
| |
| 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`
| 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`
...
LL | *fptr.0.0 = 20;
| --------- in Rust 2018, this closure captures all of `fptr`, but in Rust 2021, it will only capture `fptr.0.0`
Expand All @@ -40,14 +43,14 @@ LL |
LL |
LL |
LL |
LL | *fptr.0.0 = 20;
LL |
...

error: changes to closure capture in Rust 2021 will affect `Clone` trait implementation for closure and drop order
--> $DIR/auto_traits.rs:66:13
error: changes to closure capture in Rust 2021 will affect drop order and which traits the closure implements
--> $DIR/auto_traits.rs:67:13
|
LL | let c = || {
| ^^ 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`
| ^^ 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`
...
LL | let f_1 = f.1;
| --- in Rust 2018, this closure captures all of `f`, but in Rust 2021, it will only capture `f.1`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ where
let f = panic::AssertUnwindSafe(f);
let result = panic::catch_unwind(move || {
let _ = &f;
//~^ ERROR: `UnwindSafe`, `RefUnwindSafe` trait implementation for closure
//~| 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`
//~^ ERROR: changes to closure capture in Rust 2021 will affect which traits the closure implements [rust_2021_incompatible_closure_captures]
//~| NOTE: in Rust 2018, this closure implements `UnwindSafe`
//~| NOTE: in Rust 2018, this closure implements `RefUnwindSafe`
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `f` to be fully captured
f.0()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ where
{
let f = panic::AssertUnwindSafe(f);
let result = panic::catch_unwind(move || {
//~^ ERROR: `UnwindSafe`, `RefUnwindSafe` trait implementation for closure
//~| 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`
//~^ ERROR: changes to closure capture in Rust 2021 will affect which traits the closure implements [rust_2021_incompatible_closure_captures]
//~| NOTE: in Rust 2018, this closure implements `UnwindSafe`
//~| NOTE: in Rust 2018, this closure implements `RefUnwindSafe`
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `f` to be fully captured
f.0()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
error: changes to closure capture in Rust 2021 will affect `UnwindSafe`, `RefUnwindSafe` trait implementation for closure
error: changes to closure capture in Rust 2021 will affect which traits the closure implements
--> $DIR/mir_calls_to_shims.rs:20:38
|
LL | let result = panic::catch_unwind(move || {
| ^^^^^^^ 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`
| ^^^^^^^
| |
| 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`
| 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`
...
LL | f.0()
| --- in Rust 2018, this closure captures all of `f`, but in Rust 2021, it will only capture `f.0`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ impl Foo {
}
}


struct S(Foo);

#[derive(Clone)]
Expand All @@ -37,8 +36,8 @@ fn test_multi_issues() {
let f2 = U(S(Foo::from("bar")), T(0));
let c = || {
let _ = (&f1, &f2);
//~^ ERROR: `Clone` trait implementation for closure and drop order
//~| 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`
//~^ ERROR: changes to closure capture in Rust 2021
//~| NOTE: in Rust 2018, this closure implements `Clone` as `f1` implements `Clone`
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `f1`, `f2` to be fully captured
let _f_1 = f1.0;
Expand All @@ -57,8 +56,8 @@ fn test_capturing_all_disjoint_fields_individually() {
let f1 = U(S(Foo::from("foo")), T(0));
let c = || {
let _ = &f1;
//~^ ERROR: `Clone` trait implementation for closure
//~| 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`
//~^ ERROR: changes to closure capture in Rust 2021 will affect which traits the closure implements [rust_2021_incompatible_closure_captures]
//~| NOTE: in Rust 2018, this closure implements `Clone` as `f1` implements `Clone`
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `f1` to be fully captured
let _f_1 = f1.0;
Expand All @@ -83,9 +82,9 @@ fn test_capturing_several_disjoint_fields_individually_1() {
let f1 = U1(S(Foo::from("foo")), T(0), S(Foo::from("bar")));
let c = || {
let _ = &f1;
//~^ ERROR: `Clone` trait implementation for closure
//~| 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`
//~| 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`
//~^ ERROR: changes to closure capture in Rust 2021 will affect which traits the closure implements [rust_2021_incompatible_closure_captures]
//~| NOTE: in Rust 2018, this closure implements `Clone` as `f1` implements `Clone`
//~| NOTE: in Rust 2018, this closure implements `Clone` as `f1` implements `Clone`
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `f1` to be fully captured
let _f_0 = f1.0;
Expand All @@ -103,8 +102,8 @@ fn test_capturing_several_disjoint_fields_individually_2() {
let f1 = U1(S(Foo::from("foo")), T(0), S(Foo::from("bar")));
let c = || {
let _ = &f1;
//~^ ERROR: `Clone` trait implementation for closure and drop order
//~| 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`
//~^ ERROR: changes to closure capture in Rust 2021 will affect drop order and which traits the closure implements
//~| NOTE: in Rust 2018, this closure implements `Clone` as `f1` implements `Clone`
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `f1` to be fully captured
let _f_0 = f1.0;
Expand Down Expand Up @@ -136,9 +135,10 @@ fn test_multi_traits_issues() {
let mut f2 = 10;
let fptr2 = SendPointer(&mut f2 as *mut i32);
thread::spawn(move || { let _ = (&fptr1, &fptr2); unsafe {
//~^ ERROR: `Sync`, `Send` trait implementation for closure
//~| 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`
//~| 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`
//~^ ERROR: changes to closure capture in Rust 2021
//~| NOTE: in Rust 2018, this closure implements `Sync` as `fptr1` implements `Sync`
//~| NOTE: in Rust 2018, this closure implements `Send` as `fptr1` implements `Send`
//~| NOTE: in Rust 2018, this closure implements `Send` as `fptr2` implements `Send`
//~| NOTE: for more information, see
//~| HELP: add a dummy let to cause `fptr1`, `fptr2` to be fully captured
*fptr1.0.0 = 20;
Expand Down
Loading

0 comments on commit 4b1cb73

Please sign in to comment.