]> git.proxmox.com Git - rustc.git/blob - src/test/ui/closures/2229_closure_analysis/migrations/precise.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / src / test / ui / closures / 2229_closure_analysis / migrations / precise.rs
1 #![deny(disjoint_capture_drop_reorder)]
2 //~^ NOTE: the lint level is defined here
3
4 #[derive(Debug)]
5 struct Foo(i32);
6 impl Drop for Foo {
7 fn drop(&mut self) {
8 println!("{:?} dropped", self.0);
9 }
10 }
11
12 struct ConstainsDropField(Foo, Foo);
13
14 #[derive(Debug)]
15 struct ContainsAndImplsDrop(Foo);
16 impl Drop for ContainsAndImplsDrop {
17 fn drop(&mut self) {
18 println!("{:?} dropped", self.0);
19 }
20 }
21
22 // Test that even if all paths starting at root variable that implement Drop are captured,
23 // the lint is triggered if the root variable implements drop and isn't captured.
24 fn test_precise_analysis_parent_root_impl_drop_not_captured() {
25 let t = ContainsAndImplsDrop(Foo(10));
26
27 let c = || {
28 //~^ERROR: drop order affected for closure because of `capture_disjoint_fields`
29 //~| NOTE: drop(&(t));
30 let _t = t.0;
31 };
32
33 c();
34 }
35
36 // Test that lint is triggered if a path that implements Drop is not captured by move
37 fn test_precise_analysis_drop_paths_not_captured_by_move() {
38 let t = ConstainsDropField(Foo(10), Foo(20));
39
40 let c = || {
41 //~^ERROR: drop order affected for closure because of `capture_disjoint_fields`
42 //~| NOTE: drop(&(t));
43 let _t = t.0;
44 let _t = &t.1;
45 };
46
47 c();
48 }
49
50 struct S;
51 impl Drop for S {
52 fn drop(&mut self) {
53 }
54 }
55
56 struct T(S, S);
57 struct U(T, T);
58
59 // Test precise analysis for the lint works with paths longer than one.
60 fn test_precise_analysis_long_path_missing() {
61 let u = U(T(S, S), T(S, S));
62
63 let c = || {
64 //~^ERROR: drop order affected for closure because of `capture_disjoint_fields`
65 //~| NOTE: drop(&(u));
66 let _x = u.0.0;
67 let _x = u.0.1;
68 let _x = u.1.0;
69 };
70
71 c();
72 }
73
74 fn main() {
75 test_precise_analysis_parent_root_impl_drop_not_captured();
76 test_precise_analysis_drop_paths_not_captured_by_move();
77 test_precise_analysis_long_path_missing();
78 }