2 #![deny(rust_2021_incompatible_closure_captures)]
3 //~^ NOTE: the lint level is defined here
11 println!("{:?} dropped", self.0);
16 fn from(s: &str) -> Self {
21 struct S(#[allow(unused_tuple_struct_fields)] Foo);
24 struct T(#[allow(unused_tuple_struct_fields)] i32);
29 fn clone(&self) -> Self {
30 U(S(Foo::from("Hello World")), T(0))
34 fn test_multi_issues() {
35 let f1 = U(S(Foo::from("foo")), T(0));
36 let f2 = U(S(Foo::from("bar")), T(0));
39 //~^ ERROR: changes to closure capture in Rust 2021
40 //~| NOTE: in Rust 2018, this closure implements `Clone` as `f1` implements `Clone`
41 //~| NOTE: for more information, see
42 //~| HELP: add a dummy let to cause `f1`, `f2` to be fully captured
44 //~^ NOTE: in Rust 2018, this closure captures all of `f1`, but in Rust 2021, it will only capture `f1.0`
46 //~^ NOTE: in Rust 2018, this closure captures all of `f2`, but in Rust 2021, it will only capture `f2.1`
49 let c_clone = c.clone();
53 //~^ NOTE: in Rust 2018, `f2` is dropped here, but in Rust 2021, only `f2.1` will be dropped here as part of the closure
55 fn test_capturing_all_disjoint_fields_individually() {
56 let f1 = U(S(Foo::from("foo")), T(0));
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`
61 //~| NOTE: for more information, see
62 //~| HELP: add a dummy let to cause `f1` to be fully captured
64 //~^ NOTE: in Rust 2018, this closure captures all of `f1`, but in Rust 2021, it will only capture `f1.0`
68 let c_clone = c.clone();
76 fn clone(&self) -> Self {
77 U1(S(Foo::from("foo")), T(0), S(Foo::from("bar")))
81 fn test_capturing_several_disjoint_fields_individually_1() {
82 let f1 = U1(S(Foo::from("foo")), T(0), S(Foo::from("bar")));
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`
88 //~| NOTE: for more information, see
89 //~| HELP: add a dummy let to cause `f1` to be fully captured
91 //~^ NOTE: in Rust 2018, this closure captures all of `f1`, but in Rust 2021, it will only capture `f1.0`
93 //~^ NOTE: in Rust 2018, this closure captures all of `f1`, but in Rust 2021, it will only capture `f1.2`
96 let c_clone = c.clone();
101 fn test_capturing_several_disjoint_fields_individually_2() {
102 let f1 = U1(S(Foo::from("foo")), T(0), S(Foo::from("bar")));
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`
107 //~| NOTE: for more information, see
108 //~| HELP: add a dummy let to cause `f1` to be fully captured
110 //~^ NOTE: in Rust 2018, this closure captures all of `f1`, but in Rust 2021, it will only capture `f1.0`
112 //~^ NOTE: in Rust 2018, this closure captures all of `f1`, but in Rust 2021, it will only capture `f1.1`
115 let c_clone = c.clone();
119 //~^ NOTE: in Rust 2018, `f1` is dropped here, but in Rust 2021, only `f1.1` will be dropped here as part of the closure
120 //~| NOTE: in Rust 2018, `f1` is dropped here, but in Rust 2021, only `f1.0` will be dropped here as part of the closure
122 struct SendPointer(*mut i32);
123 unsafe impl Send for SendPointer {}
125 struct CustomInt(*mut i32);
126 struct SyncPointer(CustomInt);
127 unsafe impl Sync for SyncPointer {}
128 unsafe impl Send for CustomInt {}
130 fn test_multi_traits_issues() {
132 let f1 = CustomInt(&mut f1 as *mut i32);
133 let fptr1 = SyncPointer(f1);
136 let fptr2 = SendPointer(&mut f2 as *mut i32);
137 thread::spawn(move || { let _ = (&fptr1, &fptr2); unsafe {
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`
142 //~| NOTE: for more information, see
143 //~| HELP: add a dummy let to cause `fptr1`, `fptr2` to be fully captured
145 //~^ NOTE: in Rust 2018, this closure captures all of `fptr1`, but in Rust 2021, it will only capture `fptr1.0.0`
147 //~^ NOTE: in Rust 2018, this closure captures all of `fptr2`, but in Rust 2021, it will only capture `fptr2.0`
153 test_capturing_all_disjoint_fields_individually();
154 test_capturing_several_disjoint_fields_individually_1();
155 test_capturing_several_disjoint_fields_individually_2();
156 test_multi_traits_issues();