Skip to content

Commit acc2e23

Browse files
authored
Rollup merge of #79177 - fanzier:drop-order-test, r=RalfJung
Test drop order for (destructuring) assignments Add a test that checks whether the drop order of `let` bindings is consistent with the drop order of the corresponding destructuring assignments. Thanks to ```@RalfJung``` for the suggesting this test ([here](#79016 (comment))) and an implementation! r? ```@RalfJung```
2 parents c2a277c + 1094f97 commit acc2e23

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// run-pass
2+
3+
//! Test that let bindings and destructuring assignments have consistent drop orders
4+
5+
#![feature(destructuring_assignment)]
6+
#![allow(unused_variables, unused_assignments)]
7+
8+
use std::cell::RefCell;
9+
10+
thread_local! {
11+
static DROP_ORDER: RefCell<Vec<usize>> = RefCell::new(Vec::new());
12+
}
13+
14+
struct DropRecorder(usize);
15+
impl Drop for DropRecorder {
16+
fn drop(&mut self) {
17+
DROP_ORDER.with(|d| d.borrow_mut().push(self.0));
18+
}
19+
}
20+
21+
fn main() {
22+
let expected_drop_order = vec![1, 4, 5, 3, 2];
23+
// Check the drop order for let bindings:
24+
{
25+
let _ = DropRecorder(1);
26+
let _val = DropRecorder(2);
27+
let (x, _) = (DropRecorder(3), DropRecorder(4));
28+
drop(DropRecorder(5));
29+
}
30+
DROP_ORDER.with(|d| {
31+
assert_eq!(&*d.borrow(), &expected_drop_order);
32+
d.borrow_mut().clear();
33+
});
34+
// Check that the drop order for destructuring assignment is the same:
35+
{
36+
let _val;
37+
let x;
38+
_ = DropRecorder(1);
39+
_val = DropRecorder(2);
40+
(x, _) = (DropRecorder(3), DropRecorder(4));
41+
drop(DropRecorder(5));
42+
}
43+
DROP_ORDER.with(|d| assert_eq!(&*d.borrow(), &expected_drop_order));
44+
}

0 commit comments

Comments
 (0)