]>
Commit | Line | Data |
---|---|---|
17df50a5 | 1 | // run-rustfix |
136023e0 XL |
2 | #![deny(rust_2021_incompatible_closure_captures)] |
3 | //~^ NOTE: the lint level is defined here | |
17df50a5 XL |
4 | |
5 | use std::thread; | |
6 | ||
94222f64 XL |
7 | #[derive(Debug)] |
8 | struct Foo(i32); | |
9 | impl Drop for Foo { | |
10 | fn drop(&mut self) { | |
11 | println!("{:?} dropped", self.0); | |
12 | } | |
13 | } | |
14 | ||
17df50a5 | 15 | /* Test Send Trait Migration */ |
136023e0 | 16 | struct SendPointer(*mut i32); |
17df50a5 XL |
17 | unsafe impl Send for SendPointer {} |
18 | ||
19 | fn test_send_trait() { | |
20 | let mut f = 10; | |
21 | let fptr = SendPointer(&mut f as *mut i32); | |
22 | thread::spawn(move || { let _ = &fptr; unsafe { | |
c295e0f8 XL |
23 | //~^ ERROR: changes to closure capture |
24 | //~| NOTE: in Rust 2018, this closure implements `Send` | |
136023e0 | 25 | //~| NOTE: for more information, see |
17df50a5 XL |
26 | //~| HELP: add a dummy let to cause `fptr` to be fully captured |
27 | *fptr.0 = 20; | |
94222f64 | 28 | //~^ NOTE: in Rust 2018, this closure captures all of `fptr`, but in Rust 2021, it will only capture `fptr.0` |
17df50a5 XL |
29 | } }); |
30 | } | |
31 | ||
32 | /* Test Sync Trait Migration */ | |
136023e0 XL |
33 | struct CustomInt(*mut i32); |
34 | struct SyncPointer(CustomInt); | |
17df50a5 XL |
35 | unsafe impl Sync for SyncPointer {} |
36 | unsafe impl Send for CustomInt {} | |
37 | ||
38 | fn test_sync_trait() { | |
39 | let mut f = 10; | |
40 | let f = CustomInt(&mut f as *mut i32); | |
41 | let fptr = SyncPointer(f); | |
42 | thread::spawn(move || { let _ = &fptr; unsafe { | |
c295e0f8 XL |
43 | //~^ ERROR: changes to closure capture |
44 | //~| NOTE: in Rust 2018, this closure implements `Sync` | |
45 | //~| NOTE: in Rust 2018, this closure implements `Send` | |
136023e0 | 46 | //~| NOTE: for more information, see |
17df50a5 XL |
47 | //~| HELP: add a dummy let to cause `fptr` to be fully captured |
48 | *fptr.0.0 = 20; | |
94222f64 | 49 | //~^ NOTE: in Rust 2018, this closure captures all of `fptr`, but in Rust 2021, it will only capture `fptr.0.0` |
17df50a5 XL |
50 | } }); |
51 | } | |
52 | ||
53 | /* Test Clone Trait Migration */ | |
94222f64 | 54 | struct S(Foo); |
17df50a5 XL |
55 | struct T(i32); |
56 | ||
136023e0 | 57 | struct U(S, T); |
17df50a5 XL |
58 | |
59 | impl Clone for U { | |
60 | fn clone(&self) -> Self { | |
94222f64 | 61 | U(S(Foo(0)), T(0)) |
17df50a5 XL |
62 | } |
63 | } | |
64 | ||
65 | fn test_clone_trait() { | |
94222f64 XL |
66 | let f = U(S(Foo(0)), T(0)); |
67 | let c = || { | |
68 | let _ = &f; | |
c295e0f8 XL |
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` | |
136023e0 | 71 | //~| NOTE: for more information, see |
17df50a5 XL |
72 | //~| HELP: add a dummy let to cause `f` to be fully captured |
73 | let f_1 = f.1; | |
94222f64 | 74 | //~^ NOTE: in Rust 2018, this closure captures all of `f`, but in Rust 2021, it will only capture `f.1` |
17df50a5 XL |
75 | println!("{:?}", f_1.0); |
76 | }; | |
77 | ||
78 | let c_clone = c.clone(); | |
79 | ||
80 | c_clone(); | |
81 | } | |
94222f64 | 82 | //~^ NOTE: in Rust 2018, `f` is dropped here, but in Rust 2021, only `f.1` will be dropped here as part of the closure |
17df50a5 XL |
83 | |
84 | fn main() { | |
85 | test_send_trait(); | |
86 | test_sync_trait(); | |
87 | test_clone_trait(); | |
88 | } |