1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // A battery of tests to ensure destructors of unboxed closure environments
12 // run at the right times.
14 #![feature(overloaded_calls, unboxed_closures)]
16 static mut DROP_COUNT: uint = 0;
18 fn drop_count() -> uint {
29 fn new() -> Droppable {
36 impl Drop for Droppable {
44 fn a<F:|&: int, int| -> int>(f: F) -> int {
48 fn b<F:|&mut: int, int| -> int>(mut f: F) -> int {
52 fn c<F:|: int, int| -> int>(f: F) -> int {
58 a(|&: a: int, b| { a + b });
60 assert_eq!(drop_count(), 0);
63 let z = &Droppable::new();
64 a(|&: a: int, b| { z; a + b });
65 assert_eq!(drop_count(), 0);
67 assert_eq!(drop_count(), 1);
70 let z = &Droppable::new();
71 let zz = &Droppable::new();
72 a(|&: a: int, b| { z; zz; a + b });
73 assert_eq!(drop_count(), 1);
75 assert_eq!(drop_count(), 3);
80 b(|&mut: a: int, b| { a + b });
82 assert_eq!(drop_count(), 3);
85 let z = &Droppable::new();
86 b(|&mut: a: int, b| { z; a + b });
87 assert_eq!(drop_count(), 3);
89 assert_eq!(drop_count(), 4);
92 let z = &Droppable::new();
93 let zz = &Droppable::new();
94 b(|&mut: a: int, b| { z; zz; a + b });
95 assert_eq!(drop_count(), 4);
97 assert_eq!(drop_count(), 6);
102 c(|: a: int, b| { a + b });
104 assert_eq!(drop_count(), 6);
107 let z = Droppable::new();
108 c(|: a: int, b| { z; a + b });
109 assert_eq!(drop_count(), 7);
111 assert_eq!(drop_count(), 7);
114 let z = Droppable::new();
115 let zz = Droppable::new();
116 c(|: a: int, b| { z; zz; a + b });
117 assert_eq!(drop_count(), 9);
119 assert_eq!(drop_count(), 9);