Skip to content

Commit 7a3e450

Browse files
committed
Add tests to verify the drop order of fields in fully captured upvars
1 parent 15d9ba0 commit 7a3e450

5 files changed

+360
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// edition:2021
2+
3+
// Tests that in cases where we individually capture all the fields of a type,
4+
// we still drop them in the order they would have been dropped in the 2018 edition.
5+
6+
#![feature(rustc_attrs)]
7+
8+
#[derive(Debug)]
9+
struct HasDrop;
10+
impl Drop for HasDrop {
11+
fn drop(&mut self) {
12+
println!("dropped");
13+
}
14+
}
15+
16+
fn test_one() {
17+
let a = (HasDrop, HasDrop);
18+
let b = (HasDrop, HasDrop);
19+
20+
let c = #[rustc_capture_analysis]
21+
//~^ ERROR: attributes on expressions are experimental
22+
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
23+
|| {
24+
//~^ ERROR: First Pass analysis includes:
25+
//~| ERROR: Min Capture analysis includes:
26+
println!("{:?}", a.0);
27+
//~^ NOTE: Capturing a[(0, 0)] -> ImmBorrow
28+
//~| NOTE: Min Capture a[(0, 0)] -> ImmBorrow
29+
println!("{:?}", a.1);
30+
//~^ NOTE: Capturing a[(1, 0)] -> ImmBorrow
31+
//~| NOTE: Min Capture a[(1, 0)] -> ImmBorrow
32+
33+
println!("{:?}", b.0);
34+
//~^ NOTE: Capturing b[(0, 0)] -> ImmBorrow
35+
//~| NOTE: Min Capture b[(0, 0)] -> ImmBorrow
36+
println!("{:?}", b.1);
37+
//~^ NOTE: Capturing b[(1, 0)] -> ImmBorrow
38+
//~| NOTE: Min Capture b[(1, 0)] -> ImmBorrow
39+
};
40+
}
41+
42+
fn test_two() {
43+
let a = (HasDrop, HasDrop);
44+
let b = (HasDrop, HasDrop);
45+
46+
let c = #[rustc_capture_analysis]
47+
//~^ ERROR: attributes on expressions are experimental
48+
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
49+
|| {
50+
//~^ ERROR: First Pass analysis includes:
51+
//~| ERROR: Min Capture analysis includes:
52+
println!("{:?}", a.1);
53+
//~^ NOTE: Capturing a[(1, 0)] -> ImmBorrow
54+
//~| NOTE: Min Capture a[(1, 0)] -> ImmBorrow
55+
println!("{:?}", a.0);
56+
//~^ NOTE: Capturing a[(0, 0)] -> ImmBorrow
57+
//~| NOTE: Min Capture a[(0, 0)] -> ImmBorrow
58+
59+
println!("{:?}", b.1);
60+
//~^ NOTE: Capturing b[(1, 0)] -> ImmBorrow
61+
//~| NOTE: Min Capture b[(1, 0)] -> ImmBorrow
62+
println!("{:?}", b.0);
63+
//~^ NOTE: Capturing b[(0, 0)] -> ImmBorrow
64+
//~| NOTE: Min Capture b[(0, 0)] -> ImmBorrow
65+
};
66+
}
67+
68+
fn test_three() {
69+
let a = (HasDrop, HasDrop);
70+
let b = (HasDrop, HasDrop);
71+
72+
let c = #[rustc_capture_analysis]
73+
//~^ ERROR: attributes on expressions are experimental
74+
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
75+
|| {
76+
//~^ ERROR: First Pass analysis includes:
77+
//~| ERROR: Min Capture analysis includes:
78+
println!("{:?}", b.1);
79+
//~^ NOTE: Capturing b[(1, 0)] -> ImmBorrow
80+
//~| NOTE: Min Capture b[(1, 0)] -> ImmBorrow
81+
println!("{:?}", a.1);
82+
//~^ NOTE: Capturing a[(1, 0)] -> ImmBorrow
83+
//~| NOTE: Min Capture a[(1, 0)] -> ImmBorrow
84+
println!("{:?}", a.0);
85+
//~^ NOTE: Capturing a[(0, 0)] -> ImmBorrow
86+
//~| NOTE: Min Capture a[(0, 0)] -> ImmBorrow
87+
88+
println!("{:?}", b.0);
89+
//~^ NOTE: Capturing b[(0, 0)] -> ImmBorrow
90+
//~| NOTE: Min Capture b[(0, 0)] -> ImmBorrow
91+
};
92+
}
93+
94+
fn main() {
95+
test_one();
96+
test_two();
97+
test_three();
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
error[E0658]: attributes on expressions are experimental
2+
--> $DIR/preserve_field_drop_order.rs:20:13
3+
|
4+
LL | let c = #[rustc_capture_analysis]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
8+
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
9+
10+
error[E0658]: attributes on expressions are experimental
11+
--> $DIR/preserve_field_drop_order.rs:46:13
12+
|
13+
LL | let c = #[rustc_capture_analysis]
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
17+
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
18+
19+
error[E0658]: attributes on expressions are experimental
20+
--> $DIR/preserve_field_drop_order.rs:72:13
21+
|
22+
LL | let c = #[rustc_capture_analysis]
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
24+
|
25+
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
26+
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
27+
28+
error: First Pass analysis includes:
29+
--> $DIR/preserve_field_drop_order.rs:23:5
30+
|
31+
LL | / || {
32+
LL | |
33+
LL | |
34+
LL | | println!("{:?}", a.0);
35+
... |
36+
LL | |
37+
LL | | };
38+
| |_____^
39+
|
40+
note: Capturing a[(0, 0)] -> ImmBorrow
41+
--> $DIR/preserve_field_drop_order.rs:26:26
42+
|
43+
LL | println!("{:?}", a.0);
44+
| ^^^
45+
note: Capturing a[(1, 0)] -> ImmBorrow
46+
--> $DIR/preserve_field_drop_order.rs:29:26
47+
|
48+
LL | println!("{:?}", a.1);
49+
| ^^^
50+
note: Capturing b[(0, 0)] -> ImmBorrow
51+
--> $DIR/preserve_field_drop_order.rs:33:26
52+
|
53+
LL | println!("{:?}", b.0);
54+
| ^^^
55+
note: Capturing b[(1, 0)] -> ImmBorrow
56+
--> $DIR/preserve_field_drop_order.rs:36:26
57+
|
58+
LL | println!("{:?}", b.1);
59+
| ^^^
60+
61+
error: Min Capture analysis includes:
62+
--> $DIR/preserve_field_drop_order.rs:23:5
63+
|
64+
LL | / || {
65+
LL | |
66+
LL | |
67+
LL | | println!("{:?}", a.0);
68+
... |
69+
LL | |
70+
LL | | };
71+
| |_____^
72+
|
73+
note: Min Capture a[(0, 0)] -> ImmBorrow
74+
--> $DIR/preserve_field_drop_order.rs:26:26
75+
|
76+
LL | println!("{:?}", a.0);
77+
| ^^^
78+
note: Min Capture a[(1, 0)] -> ImmBorrow
79+
--> $DIR/preserve_field_drop_order.rs:29:26
80+
|
81+
LL | println!("{:?}", a.1);
82+
| ^^^
83+
note: Min Capture b[(0, 0)] -> ImmBorrow
84+
--> $DIR/preserve_field_drop_order.rs:33:26
85+
|
86+
LL | println!("{:?}", b.0);
87+
| ^^^
88+
note: Min Capture b[(1, 0)] -> ImmBorrow
89+
--> $DIR/preserve_field_drop_order.rs:36:26
90+
|
91+
LL | println!("{:?}", b.1);
92+
| ^^^
93+
94+
error: First Pass analysis includes:
95+
--> $DIR/preserve_field_drop_order.rs:49:5
96+
|
97+
LL | / || {
98+
LL | |
99+
LL | |
100+
LL | | println!("{:?}", a.1);
101+
... |
102+
LL | |
103+
LL | | };
104+
| |_____^
105+
|
106+
note: Capturing a[(1, 0)] -> ImmBorrow
107+
--> $DIR/preserve_field_drop_order.rs:52:26
108+
|
109+
LL | println!("{:?}", a.1);
110+
| ^^^
111+
note: Capturing a[(0, 0)] -> ImmBorrow
112+
--> $DIR/preserve_field_drop_order.rs:55:26
113+
|
114+
LL | println!("{:?}", a.0);
115+
| ^^^
116+
note: Capturing b[(1, 0)] -> ImmBorrow
117+
--> $DIR/preserve_field_drop_order.rs:59:26
118+
|
119+
LL | println!("{:?}", b.1);
120+
| ^^^
121+
note: Capturing b[(0, 0)] -> ImmBorrow
122+
--> $DIR/preserve_field_drop_order.rs:62:26
123+
|
124+
LL | println!("{:?}", b.0);
125+
| ^^^
126+
127+
error: Min Capture analysis includes:
128+
--> $DIR/preserve_field_drop_order.rs:49:5
129+
|
130+
LL | / || {
131+
LL | |
132+
LL | |
133+
LL | | println!("{:?}", a.1);
134+
... |
135+
LL | |
136+
LL | | };
137+
| |_____^
138+
|
139+
note: Min Capture a[(1, 0)] -> ImmBorrow
140+
--> $DIR/preserve_field_drop_order.rs:52:26
141+
|
142+
LL | println!("{:?}", a.1);
143+
| ^^^
144+
note: Min Capture a[(0, 0)] -> ImmBorrow
145+
--> $DIR/preserve_field_drop_order.rs:55:26
146+
|
147+
LL | println!("{:?}", a.0);
148+
| ^^^
149+
note: Min Capture b[(1, 0)] -> ImmBorrow
150+
--> $DIR/preserve_field_drop_order.rs:59:26
151+
|
152+
LL | println!("{:?}", b.1);
153+
| ^^^
154+
note: Min Capture b[(0, 0)] -> ImmBorrow
155+
--> $DIR/preserve_field_drop_order.rs:62:26
156+
|
157+
LL | println!("{:?}", b.0);
158+
| ^^^
159+
160+
error: First Pass analysis includes:
161+
--> $DIR/preserve_field_drop_order.rs:75:5
162+
|
163+
LL | / || {
164+
LL | |
165+
LL | |
166+
LL | | println!("{:?}", b.1);
167+
... |
168+
LL | |
169+
LL | | };
170+
| |_____^
171+
|
172+
note: Capturing b[(1, 0)] -> ImmBorrow
173+
--> $DIR/preserve_field_drop_order.rs:78:26
174+
|
175+
LL | println!("{:?}", b.1);
176+
| ^^^
177+
note: Capturing a[(1, 0)] -> ImmBorrow
178+
--> $DIR/preserve_field_drop_order.rs:81:26
179+
|
180+
LL | println!("{:?}", a.1);
181+
| ^^^
182+
note: Capturing a[(0, 0)] -> ImmBorrow
183+
--> $DIR/preserve_field_drop_order.rs:84:26
184+
|
185+
LL | println!("{:?}", a.0);
186+
| ^^^
187+
note: Capturing b[(0, 0)] -> ImmBorrow
188+
--> $DIR/preserve_field_drop_order.rs:88:26
189+
|
190+
LL | println!("{:?}", b.0);
191+
| ^^^
192+
193+
error: Min Capture analysis includes:
194+
--> $DIR/preserve_field_drop_order.rs:75:5
195+
|
196+
LL | / || {
197+
LL | |
198+
LL | |
199+
LL | | println!("{:?}", b.1);
200+
... |
201+
LL | |
202+
LL | | };
203+
| |_____^
204+
|
205+
note: Min Capture b[(1, 0)] -> ImmBorrow
206+
--> $DIR/preserve_field_drop_order.rs:78:26
207+
|
208+
LL | println!("{:?}", b.1);
209+
| ^^^
210+
note: Min Capture b[(0, 0)] -> ImmBorrow
211+
--> $DIR/preserve_field_drop_order.rs:88:26
212+
|
213+
LL | println!("{:?}", b.0);
214+
| ^^^
215+
note: Min Capture a[(1, 0)] -> ImmBorrow
216+
--> $DIR/preserve_field_drop_order.rs:81:26
217+
|
218+
LL | println!("{:?}", a.1);
219+
| ^^^
220+
note: Min Capture a[(0, 0)] -> ImmBorrow
221+
--> $DIR/preserve_field_drop_order.rs:84:26
222+
|
223+
LL | println!("{:?}", a.0);
224+
| ^^^
225+
226+
error: aborting due to 9 previous errors
227+
228+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// run-pass
2+
// check-run-results
3+
// revisions: twenty_eighteen twenty_twentyone
4+
// [twenty_eighteen]compile-flags: --edition 2018
5+
// [twenty_twentyone]compile-flags: --edition 2021
6+
7+
#[derive(Debug)]
8+
struct Dropable(String);
9+
10+
impl Drop for Dropable {
11+
fn drop(&mut self) {
12+
println!("Dropping {}", self.0)
13+
}
14+
}
15+
16+
#[derive(Debug)]
17+
struct A {
18+
x: Dropable,
19+
y: Dropable,
20+
}
21+
22+
fn main() {
23+
let a = A { x: Dropable(format!("x")), y: Dropable(format!("y")) };
24+
25+
let c = move || println!("{:?} {:?}", a.y, a.x);
26+
27+
c();
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Dropable("y") Dropable("x")
2+
Dropping x
3+
Dropping y
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Dropable("y") Dropable("x")
2+
Dropping y
3+
Dropping x

0 commit comments

Comments
 (0)