Skip to content

Commit da86348

Browse files
committed
Update test cases
1 parent 18af989 commit da86348

File tree

9 files changed

+522
-108
lines changed

9 files changed

+522
-108
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// run-rustfix
2+
3+
#![deny(disjoint_capture_drop_reorder)]
4+
//~^ NOTE: the lint level is defined here
5+
6+
// Test cases for types that implement a insignificant drop (stlib defined)
7+
8+
// `t` needs Drop because one of its elements needs drop,
9+
// therefore precise capture might affect drop ordering
10+
fn test1_all_need_migration() {
11+
let t = (String::new(), String::new());
12+
let t1 = (String::new(), String::new());
13+
let t2 = (String::new(), String::new());
14+
15+
let c = || { let _ = (&t, &t1, &t2); {
16+
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields`
17+
//~| HELP:` let _ = (&t, &t1, &t2)` causes `t`, `t1`, `t2` to be fully captured
18+
19+
let _t = t.0;
20+
let _t1 = t1.0;
21+
let _t2 = t2.0;
22+
} };
23+
24+
c();
25+
}
26+
27+
// String implements drop and therefore should be migrated.
28+
// But in this test cases, `t2` is completely captured and when it is dropped won't be affected
29+
fn test2_only_precise_paths_need_migration() {
30+
let t = (String::new(), String::new());
31+
let t1 = (String::new(), String::new());
32+
let t2 = (String::new(), String::new());
33+
34+
let c = || { let _ = (&t, &t1); {
35+
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields`
36+
//~| HELP:` let _ = (&t, &t1)` causes `t`, `t1` to be fully captured
37+
let _t = t.0;
38+
let _t1 = t1.0;
39+
let _t2 = t2;
40+
} };
41+
42+
c();
43+
}
44+
45+
// If a variable would've not been captured by value then it would've not been
46+
// dropped with the closure and therefore doesn't need migration.
47+
fn test3_only_by_value_need_migration() {
48+
let t = (String::new(), String::new());
49+
let t1 = (String::new(), String::new());
50+
let c = || { let _ = &t; {
51+
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields`
52+
//~| HELP: `let _ = &t` causes `t` to be fully captured
53+
let _t = t.0;
54+
println!("{}", t1.1);
55+
} };
56+
57+
c();
58+
}
59+
60+
// Copy types get copied into the closure instead of move. Therefore we don't need to
61+
// migrate then as their drop order isn't tied to the closure.
62+
fn test4_only_non_copy_types_need_migration() {
63+
let t = (String::new(), String::new());
64+
65+
// `t1` is Copy because all of its elements are Copy
66+
let t1 = (0i32, 0i32);
67+
68+
let c = || { let _ = &t; {
69+
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields`
70+
//~| HELP: `let _ = &t` causes `t` to be fully captured
71+
let _t = t.0;
72+
let _t1 = t1.0;
73+
} };
74+
75+
c();
76+
}
77+
78+
fn test5_only_drop_types_need_migration() {
79+
struct S(i32, i32);
80+
81+
let t = (String::new(), String::new());
82+
83+
// `s` doesn't implement Drop or any elements within it, and doesn't need migration
84+
let s = S(0i32, 0i32);
85+
86+
let c = || { let _ = &t; {
87+
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields`
88+
//~| HELP: `let _ = &t` causes `t` to be fully captured
89+
let _t = t.0;
90+
let _s = s.0;
91+
} };
92+
93+
c();
94+
}
95+
96+
// Since we are using a move closure here, both `t` and `t1` get moved
97+
// even though they are being used by ref inside the closure.
98+
fn test6_move_closures_non_copy_types_might_need_migration() {
99+
let t = (String::new(), String::new());
100+
let t1 = (String::new(), String::new());
101+
let c = move || { let _ = (&t1, &t); {
102+
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields`
103+
//~| HELP: `let _ = (&t1, &t)` causes `t1`, `t` to be fully captured
104+
println!("{} {}", t1.1, t.1);
105+
} };
106+
107+
c();
108+
}
109+
110+
// Test migration analysis in case of Drop + Non Drop aggregates.
111+
// Note we need migration here only because the non-copy (because Drop type) is captured,
112+
// otherwise we won't need to, since we can get away with just by ref capture in that case.
113+
fn test7_drop_non_drop_aggregate_need_migration() {
114+
let t = (String::new(), String::new(), 0i32);
115+
116+
let c = || { let _ = &t; {
117+
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields`
118+
//~| HELP: `let _ = &t` causes `t` to be fully captured
119+
let _t = t.0;
120+
} };
121+
122+
c();
123+
}
124+
125+
fn main() {
126+
test1_all_need_migration();
127+
test2_only_precise_paths_need_migration();
128+
test3_only_by_value_need_migration();
129+
test4_only_non_copy_types_need_migration();
130+
test5_only_drop_types_need_migration();
131+
test6_move_closures_non_copy_types_might_need_migration();
132+
test7_drop_non_drop_aggregate_need_migration();
133+
}

src/test/ui/closures/2229_closure_analysis/migrations/insignificant_drop.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// run-rustfix
2+
13
#![deny(disjoint_capture_drop_reorder)]
24
//~^ NOTE: the lint level is defined here
35

@@ -11,8 +13,9 @@ fn test1_all_need_migration() {
1113
let t2 = (String::new(), String::new());
1214

1315
let c = || {
14-
//~^ERROR: drop order affected for closure because of `capture_disjoint_fields`
15-
//~| NOTE: drop(&(t, t1, t2));
16+
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields`
17+
//~| HELP:` let _ = (&t, &t1, &t2)` causes `t`, `t1`, `t2` to be fully captured
18+
1619
let _t = t.0;
1720
let _t1 = t1.0;
1821
let _t2 = t2.0;
@@ -29,8 +32,8 @@ fn test2_only_precise_paths_need_migration() {
2932
let t2 = (String::new(), String::new());
3033

3134
let c = || {
32-
//~^ERROR: drop order affected for closure because of `capture_disjoint_fields`
33-
//~| NOTE: drop(&(t, t1));
35+
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields`
36+
//~| HELP:` let _ = (&t, &t1)` causes `t`, `t1` to be fully captured
3437
let _t = t.0;
3538
let _t1 = t1.0;
3639
let _t2 = t2;
@@ -45,8 +48,8 @@ fn test3_only_by_value_need_migration() {
4548
let t = (String::new(), String::new());
4649
let t1 = (String::new(), String::new());
4750
let c = || {
48-
//~^ERROR: drop order affected for closure because of `capture_disjoint_fields`
49-
//~| NOTE: drop(&(t));
51+
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields`
52+
//~| HELP: `let _ = &t` causes `t` to be fully captured
5053
let _t = t.0;
5154
println!("{}", t1.1);
5255
};
@@ -63,8 +66,8 @@ fn test4_only_non_copy_types_need_migration() {
6366
let t1 = (0i32, 0i32);
6467

6568
let c = || {
66-
//~^ERROR: drop order affected for closure because of `capture_disjoint_fields`
67-
//~| NOTE: drop(&(t));
69+
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields`
70+
//~| HELP: `let _ = &t` causes `t` to be fully captured
6871
let _t = t.0;
6972
let _t1 = t1.0;
7073
};
@@ -81,8 +84,8 @@ fn test5_only_drop_types_need_migration() {
8184
let s = S(0i32, 0i32);
8285

8386
let c = || {
84-
//~^ERROR: drop order affected for closure because of `capture_disjoint_fields`
85-
//~| NOTE: drop(&(t));
87+
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields`
88+
//~| HELP: `let _ = &t` causes `t` to be fully captured
8689
let _t = t.0;
8790
let _s = s.0;
8891
};
@@ -96,8 +99,8 @@ fn test6_move_closures_non_copy_types_might_need_migration() {
9699
let t = (String::new(), String::new());
97100
let t1 = (String::new(), String::new());
98101
let c = move || {
99-
//~^ERROR: drop order affected for closure because of `capture_disjoint_fields`
100-
//~| NOTE: drop(&(t1, t));
102+
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields`
103+
//~| HELP: `let _ = (&t1, &t)` causes `t1`, `t` to be fully captured
101104
println!("{} {}", t1.1, t.1);
102105
};
103106

@@ -111,8 +114,8 @@ fn test7_drop_non_drop_aggregate_need_migration() {
111114
let t = (String::new(), String::new(), 0i32);
112115

113116
let c = || {
114-
//~^ERROR: drop order affected for closure because of `capture_disjoint_fields`
115-
//~| NOTE: drop(&(t));
117+
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields`
118+
//~| HELP: `let _ = &t` causes `t` to be fully captured
116119
let _t = t.0;
117120
};
118121

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
error: drop order affected for closure because of `capture_disjoint_fields`
2-
--> $DIR/insignificant_drop.rs:13:13
2+
--> $DIR/insignificant_drop.rs:15:13
33
|
44
LL | let c = || {
55
| _____________^
66
LL | |
77
LL | |
8-
LL | | let _t = t.0;
9-
LL | | let _t1 = t1.0;
8+
LL | |
9+
... |
1010
LL | | let _t2 = t2.0;
1111
LL | | };
1212
| |_____^
1313
|
1414
note: the lint level is defined here
15-
--> $DIR/insignificant_drop.rs:1:9
15+
--> $DIR/insignificant_drop.rs:3:9
1616
|
1717
LL | #![deny(disjoint_capture_drop_reorder)]
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19-
= note: drop(&(t, t1, t2));
19+
help: `let _ = (&t, &t1, &t2)` causes `t`, `t1`, `t2` to be fully captured
20+
|
21+
LL | let c = || { let _ = (&t, &t1, &t2); {
22+
LL |
23+
LL |
24+
LL |
25+
LL | let _t = t.0;
26+
LL | let _t1 = t1.0;
27+
...
2028

2129
error: drop order affected for closure because of `capture_disjoint_fields`
22-
--> $DIR/insignificant_drop.rs:31:13
30+
--> $DIR/insignificant_drop.rs:34:13
2331
|
2432
LL | let c = || {
2533
| _____________^
@@ -31,10 +39,18 @@ LL | | let _t2 = t2;
3139
LL | | };
3240
| |_____^
3341
|
34-
= note: drop(&(t, t1));
42+
help: `let _ = (&t, &t1)` causes `t`, `t1` to be fully captured
43+
|
44+
LL | let c = || { let _ = (&t, &t1); {
45+
LL |
46+
LL |
47+
LL | let _t = t.0;
48+
LL | let _t1 = t1.0;
49+
LL | let _t2 = t2;
50+
...
3551

3652
error: drop order affected for closure because of `capture_disjoint_fields`
37-
--> $DIR/insignificant_drop.rs:47:13
53+
--> $DIR/insignificant_drop.rs:50:13
3854
|
3955
LL | let c = || {
4056
| _____________^
@@ -45,10 +61,18 @@ LL | | println!("{}", t1.1);
4561
LL | | };
4662
| |_____^
4763
|
48-
= note: drop(&(t));
64+
help: `let _ = &t` causes `t` to be fully captured
65+
|
66+
LL | let c = || { let _ = &t; {
67+
LL |
68+
LL |
69+
LL | let _t = t.0;
70+
LL | println!("{}", t1.1);
71+
LL | } };
72+
|
4973

5074
error: drop order affected for closure because of `capture_disjoint_fields`
51-
--> $DIR/insignificant_drop.rs:65:13
75+
--> $DIR/insignificant_drop.rs:68:13
5276
|
5377
LL | let c = || {
5478
| _____________^
@@ -59,10 +83,18 @@ LL | | let _t1 = t1.0;
5983
LL | | };
6084
| |_____^
6185
|
62-
= note: drop(&(t));
86+
help: `let _ = &t` causes `t` to be fully captured
87+
|
88+
LL | let c = || { let _ = &t; {
89+
LL |
90+
LL |
91+
LL | let _t = t.0;
92+
LL | let _t1 = t1.0;
93+
LL | } };
94+
|
6395

6496
error: drop order affected for closure because of `capture_disjoint_fields`
65-
--> $DIR/insignificant_drop.rs:83:13
97+
--> $DIR/insignificant_drop.rs:86:13
6698
|
6799
LL | let c = || {
68100
| _____________^
@@ -73,10 +105,18 @@ LL | | let _s = s.0;
73105
LL | | };
74106
| |_____^
75107
|
76-
= note: drop(&(t));
108+
help: `let _ = &t` causes `t` to be fully captured
109+
|
110+
LL | let c = || { let _ = &t; {
111+
LL |
112+
LL |
113+
LL | let _t = t.0;
114+
LL | let _s = s.0;
115+
LL | } };
116+
|
77117

78118
error: drop order affected for closure because of `capture_disjoint_fields`
79-
--> $DIR/insignificant_drop.rs:98:13
119+
--> $DIR/insignificant_drop.rs:101:13
80120
|
81121
LL | let c = move || {
82122
| _____________^
@@ -86,10 +126,17 @@ LL | | println!("{} {}", t1.1, t.1);
86126
LL | | };
87127
| |_____^
88128
|
89-
= note: drop(&(t1, t));
129+
help: `let _ = (&t1, &t)` causes `t1`, `t` to be fully captured
130+
|
131+
LL | let c = move || { let _ = (&t1, &t); {
132+
LL |
133+
LL |
134+
LL | println!("{} {}", t1.1, t.1);
135+
LL | } };
136+
|
90137

91138
error: drop order affected for closure because of `capture_disjoint_fields`
92-
--> $DIR/insignificant_drop.rs:113:13
139+
--> $DIR/insignificant_drop.rs:116:13
93140
|
94141
LL | let c = || {
95142
| _____________^
@@ -99,7 +146,14 @@ LL | | let _t = t.0;
99146
LL | | };
100147
| |_____^
101148
|
102-
= note: drop(&(t));
149+
help: `let _ = &t` causes `t` to be fully captured
150+
|
151+
LL | let c = || { let _ = &t; {
152+
LL |
153+
LL |
154+
LL | let _t = t.0;
155+
LL | } };
156+
|
103157

104158
error: aborting due to 7 previous errors
105159

0 commit comments

Comments
 (0)