Skip to content

Commit 946706c

Browse files
committed
Add capture with custom destructor test
1 parent 6a7cee4 commit 946706c

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

‎tests/unit/lambda_capture_copy_ctor.cpp‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ struct Counted {
1111
Counted(Counted &&o) : copies(o.copies), moves(o.moves + 1) {}
1212
};
1313

14+
int drops = 0;
15+
16+
struct Dropped {
17+
Dropped() {}
18+
Dropped(const Dropped &) {}
19+
Dropped(Dropped &&) {}
20+
~Dropped() { drops++; }
21+
};
22+
1423
int main() {
1524
Counted c;
1625
auto f = [c]() { return c.copies * 10 + c.moves; };
@@ -33,5 +42,17 @@ int main() {
3342
auto a2 = a;
3443
assert(a2() == 4);
3544

45+
{
46+
auto m = [d = Dropped()]() {};
47+
auto m2 = std::move(m);
48+
}
49+
assert(drops == 2);
50+
51+
{
52+
auto k = [d = Dropped()]() {};
53+
auto k2 = k;
54+
}
55+
assert(drops == 4);
56+
3657
return 0;
3758
}

‎tests/unit/out/unsafe/lambda_capture_copy_ctor.rs‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,37 @@ impl Default for Counted {
4545
unsafe { Counted::new() }
4646
}
4747
}
48+
pub static mut drops_0: std::cell::LazyCell<i32> = std::cell::LazyCell::new(|| unsafe { 0 });
49+
#[repr(C)]
50+
#[derive()]
51+
pub struct Dropped {}
52+
impl Dropped {
53+
pub unsafe fn new() -> Self {
54+
let mut this = Self {};
55+
this
56+
}
57+
pub unsafe fn copy_from(_a0: *const Dropped) -> Self {
58+
let mut this = Self {};
59+
this
60+
}
61+
pub unsafe fn move_from(_a0: *mut Dropped) -> Self {
62+
let mut this = Self {};
63+
this
64+
}
65+
pub unsafe fn destructor(&mut self) {
66+
(*std::cell::LazyCell::force_mut(&mut *&raw mut drops_0)).postfix_inc();
67+
}
68+
}
69+
impl Clone for Dropped {
70+
fn clone(&self) -> Self {
71+
unsafe { Dropped::copy_from(self as *const Dropped) }
72+
}
73+
}
74+
impl Default for Dropped {
75+
fn default() -> Self {
76+
unsafe { Dropped::new() }
77+
}
78+
}
4879
pub fn main() {
4980
unsafe {
5081
__cpp2rust_init_globals();
@@ -96,6 +127,16 @@ unsafe fn main_0() -> i32 {
96127
})
97128
.clone();
98129
assert!(((unsafe { a2() }) == (4)));
130+
{
131+
let mut m2: _ = (|| {});
132+
}
133+
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut drops_0)) == (2)));
134+
{
135+
let mut k2: _ = (|| {}).clone();
136+
}
137+
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut drops_0)) == (4)));
99138
return 0;
100139
}
101-
pub unsafe fn __cpp2rust_init_globals() {}
140+
pub unsafe fn __cpp2rust_init_globals() {
141+
std::cell::LazyCell::force(&*&raw const drops_0);
142+
}

0 commit comments

Comments
 (0)