1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
//@ run-pass
#![allow(path_statements)]
#![allow(dead_code)]
// A battery of tests to ensure destructors of unboxed closure environments
// run at the right times.
static mut DROP_COUNT: usize = 0;
fn drop_count() -> usize {
unsafe {
DROP_COUNT
}
}
struct Droppable {
x: isize,
}
impl Droppable {
fn new() -> Droppable {
Droppable {
x: 1
}
}
}
impl Drop for Droppable {
fn drop(&mut self) {
unsafe {
DROP_COUNT += 1
}
}
}
fn a<F:Fn(isize, isize) -> isize>(f: F) -> isize {
f(1, 2)
}
fn b<F:FnMut(isize, isize) -> isize>(mut f: F) -> isize {
f(3, 4)
}
fn c<F:FnOnce(isize, isize) -> isize>(f: F) -> isize {
f(5, 6)
}
fn test_fn() {
{
a(move |a: isize, b| { a + b });
}
assert_eq!(drop_count(), 0);
{
let z = &Droppable::new();
a(move |a: isize, b| { z; a + b });
assert_eq!(drop_count(), 0);
}
assert_eq!(drop_count(), 1);
{
let z = &Droppable::new();
let zz = &Droppable::new();
a(move |a: isize, b| { z; zz; a + b });
assert_eq!(drop_count(), 1);
}
assert_eq!(drop_count(), 3);
}
fn test_fn_mut() {
{
b(move |a: isize, b| { a + b });
}
assert_eq!(drop_count(), 3);
{
let z = &Droppable::new();
b(move |a: isize, b| { z; a + b });
assert_eq!(drop_count(), 3);
}
assert_eq!(drop_count(), 4);
{
let z = &Droppable::new();
let zz = &Droppable::new();
b(move |a: isize, b| { z; zz; a + b });
assert_eq!(drop_count(), 4);
}
assert_eq!(drop_count(), 6);
}
fn test_fn_once() {
{
c(move |a: isize, b| { a + b });
}
assert_eq!(drop_count(), 6);
{
let z = Droppable::new();
c(move |a: isize, b| { z; a + b });
assert_eq!(drop_count(), 7);
}
assert_eq!(drop_count(), 7);
{
let z = Droppable::new();
let zz = Droppable::new();
c(move |a: isize, b| { z; zz; a + b });
assert_eq!(drop_count(), 9);
}
assert_eq!(drop_count(), 9);
}
fn main() {
test_fn();
test_fn_mut();
test_fn_once();
}
|