]> git.lizzy.rs Git - rust.git/blob - tests/ui/closures/2229_closure_analysis/migrations/multi_diagnostics.fixed
Rollup merge of #106670 - albertlarsan68:check-docs-in-pr-ci, r=Mark-Simulacrum
[rust.git] / tests / ui / closures / 2229_closure_analysis / migrations / multi_diagnostics.fixed
1 // run-rustfix
2 #![deny(rust_2021_incompatible_closure_captures)]
3 //~^ NOTE: the lint level is defined here
4
5 use std::thread;
6
7 #[derive(Debug)]
8 struct Foo(String);
9 impl Drop for Foo {
10     fn drop(&mut self) {
11         println!("{:?} dropped", self.0);
12     }
13 }
14
15 impl Foo {
16     fn from(s: &str) -> Self {
17         Self(String::from(s))
18     }
19 }
20
21 struct S(#[allow(unused_tuple_struct_fields)] Foo);
22
23 #[derive(Clone)]
24 struct T(#[allow(unused_tuple_struct_fields)] i32);
25
26 struct U(S, T);
27
28 impl Clone for U {
29     fn clone(&self) -> Self {
30         U(S(Foo::from("Hello World")), T(0))
31     }
32 }
33
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));
37     let c = || {
38         let _ = (&f1, &f2);
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
43         let _f_1 = f1.0;
44         //~^ NOTE: in Rust 2018, this closure captures all of `f1`, but in Rust 2021, it will only capture `f1.0`
45         let _f_2 = f2.1;
46         //~^ NOTE: in Rust 2018, this closure captures all of `f2`, but in Rust 2021, it will only capture `f2.1`
47     };
48
49     let c_clone = c.clone();
50
51     c_clone();
52 }
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
54
55 fn test_capturing_all_disjoint_fields_individually() {
56     let f1 = U(S(Foo::from("foo")), T(0));
57     let c = || {
58         let _ = &f1;
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
63         let _f_1 = f1.0;
64         //~^ NOTE: in Rust 2018, this closure captures all of `f1`, but in Rust 2021, it will only capture `f1.0`
65         let _f_2 = f1.1;
66     };
67
68     let c_clone = c.clone();
69
70     c_clone();
71 }
72
73 struct U1(S, T, S);
74
75 impl Clone for U1 {
76     fn clone(&self) -> Self {
77         U1(S(Foo::from("foo")), T(0), S(Foo::from("bar")))
78     }
79 }
80
81 fn test_capturing_several_disjoint_fields_individually_1() {
82     let f1 = U1(S(Foo::from("foo")), T(0), S(Foo::from("bar")));
83     let c = || {
84         let _ = &f1;
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
90         let _f_0 = f1.0;
91         //~^ NOTE: in Rust 2018, this closure captures all of `f1`, but in Rust 2021, it will only capture `f1.0`
92         let _f_2 = f1.2;
93         //~^ NOTE: in Rust 2018, this closure captures all of `f1`, but in Rust 2021, it will only capture `f1.2`
94     };
95
96     let c_clone = c.clone();
97
98     c_clone();
99 }
100
101 fn test_capturing_several_disjoint_fields_individually_2() {
102     let f1 = U1(S(Foo::from("foo")), T(0), S(Foo::from("bar")));
103     let c = || {
104         let _ = &f1;
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
109         let _f_0 = f1.0;
110         //~^ NOTE: in Rust 2018, this closure captures all of `f1`, but in Rust 2021, it will only capture `f1.0`
111         let _f_1 = f1.1;
112         //~^ NOTE: in Rust 2018, this closure captures all of `f1`, but in Rust 2021, it will only capture `f1.1`
113     };
114
115     let c_clone = c.clone();
116
117     c_clone();
118 }
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
121
122 struct SendPointer(*mut i32);
123 unsafe impl Send for SendPointer {}
124
125 struct CustomInt(*mut i32);
126 struct SyncPointer(CustomInt);
127 unsafe impl Sync for SyncPointer {}
128 unsafe impl Send for CustomInt {}
129
130 fn test_multi_traits_issues() {
131     let mut f1 = 10;
132     let f1 = CustomInt(&mut f1 as *mut i32);
133     let fptr1 = SyncPointer(f1);
134
135     let mut f2 = 10;
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
144         *fptr1.0.0 = 20;
145         //~^ NOTE: in Rust 2018, this closure captures all of `fptr1`, but in Rust 2021, it will only capture `fptr1.0.0`
146         *fptr2.0 = 20;
147         //~^ NOTE: in Rust 2018, this closure captures all of `fptr2`, but in Rust 2021, it will only capture `fptr2.0`
148     } });
149 }
150
151 fn main() {
152     test_multi_issues();
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();
157 }