5
5
6
6
import StdlibUnittest
7
7
8
- var types = TestSuite ( " VariadicGenericCaptures " )
8
+ var captures = TestSuite ( " VariadicGenericCaptures " )
9
9
10
10
func hasMetadataPack< each T > ( _: repeat each T ) -> ( ) -> Any . Type {
11
11
return { return ( repeat each T) . self }
12
12
}
13
13
14
- types . test ( " Metadata " ) {
14
+ captures . test ( " Metadata " ) {
15
15
expectEqual ( Void . self, hasMetadataPack ( ) ( ) )
16
16
expectEqual ( ( Int, String, Bool) . self, hasMetadataPack ( 1 , " hi " , false ) ( ) )
17
17
}
@@ -20,7 +20,7 @@ func hasWitnessTablePack<each T: Sequence>(_: repeat each T) -> () -> Any.Type {
20
20
return { return ( repeat ( each T) . Element) . self }
21
21
}
22
22
23
- types . test ( " WitnessTable " ) {
23
+ captures . test ( " WitnessTable " ) {
24
24
expectEqual ( Void . self, hasWitnessTablePack ( ) ( ) )
25
25
expectEqual ( ( Int, String, Bool) . self, hasWitnessTablePack ( [ 1 ] , [ " hi " ] , [ false ] ) ( ) )
26
26
}
@@ -29,7 +29,7 @@ func hasWitnessTablePack2<each T: Sequence>(_: repeat each T) -> () -> Any.Type
29
29
return { return ( repeat ( each T) . Element. Element) . self }
30
30
}
31
31
32
- types . test ( " WitnessTable2 " ) {
32
+ captures . test ( " WitnessTable2 " ) {
33
33
expectEqual ( Void . self, hasWitnessTablePack2 ( ) ( ) )
34
34
expectEqual ( ( Int, String, Bool) . self, hasWitnessTablePack2 ( [ [ 1 ] ] , [ [ " hi " ] ] , [ [ false ] ] ) ( ) )
35
35
}
@@ -43,7 +43,7 @@ func lifetimeTest2() -> () -> Any.Type {
43
43
return hasMetadataPack ( 3 , 1.0 )
44
44
}
45
45
46
- types . test ( " Lifetime " ) {
46
+ captures . test ( " Lifetime " ) {
47
47
let fn1 = lifetimeTest1 ( )
48
48
let fn2 = lifetimeTest2 ( )
49
49
expectEqual ( ( String, Set < Int > ) . self, fn1 ( ) )
@@ -71,7 +71,7 @@ func testNonEscapingCapture<each T: Hashable>(_ t: repeat each T) -> [AnyHashabl
71
71
}
72
72
}
73
73
74
- types . test ( " CapturedValue " ) {
74
+ captures . test ( " CapturedValue " ) {
75
75
let fn1 = testEscapingCapture ( 1 , " hi " )
76
76
let fn2 = testEscapingCapture ( 5.0 , false )
77
77
@@ -82,4 +82,39 @@ types.test("CapturedValue") {
82
82
expectEqual ( [ true , 7 ] , testNonEscapingCapture ( true , 7 ) )
83
83
}
84
84
85
+ captures. test ( " Leaks " ) {
86
+ func callee< T> ( _: T ) { }
87
+
88
+ func takesEscapingClosure( _ fn: @escaping ( ) -> ( ) ) {
89
+ fn ( )
90
+ fn ( )
91
+ fn ( )
92
+ }
93
+
94
+ func takesNonEscapingClosure( _ fn: ( ) -> ( ) ) {
95
+ fn ( )
96
+ fn ( )
97
+ fn ( )
98
+ }
99
+
100
+ func formPackCaptures< each V > ( _ v: repeat each V ) {
101
+ takesEscapingClosure { repeat callee( each v) }
102
+ takesNonEscapingClosure { repeat callee( each v) }
103
+ { repeat callee( each v) } ( )
104
+ }
105
+
106
+ struct S {
107
+ init < each V > ( _ v: repeat each V ) {
108
+ takesEscapingClosure { repeat callee( each v) }
109
+ takesNonEscapingClosure { repeat callee( each v) }
110
+ { repeat callee( each v) } ( )
111
+ }
112
+ }
113
+
114
+ for _ in 0 ..< 10 {
115
+ formPackCaptures ( LifetimeTracked ( 0 ) , LifetimeTracked ( 0 ) , LifetimeTracked ( 0 ) )
116
+ callee ( S ( LifetimeTracked ( 1 ) , LifetimeTracked ( 1 ) , LifetimeTracked ( 1 ) ) )
117
+ }
118
+ }
119
+
85
120
runAllTests ( )
0 commit comments