@@ -13,37 +13,50 @@ func runIn10ms(_ closure: @escaping @Sendable () -> Void) {
13
13
}
14
14
}
15
15
16
+ let checkInterval = 10_000_000
17
+ let checkIters = 1000
18
+
16
19
final class Weak : Sendable {
17
20
let property = " Self exists "
18
21
19
- func test( ) {
20
- runIn10ms { [ weak self] in
21
- if let self {
22
- // Use implicit self -- this should not result in a strong capture
23
- _ = property
24
- fatalError ( " Self was unexpectedly captured strongly " )
25
- } else {
26
- print ( " Self was captured weakly (1) " )
22
+ func test( ) async -> ( Task < Void , Never > , Task < Void , Never > ) {
23
+ let t1 = Task { [ weak self] in
24
+ for _ in 0 ..< checkIters {
25
+ if let self {
26
+ // Use implicit self -- this should not result in a strong capture
27
+ _ = property
28
+ } else {
29
+ print ( " Self was captured weakly (1) " )
30
+ return
31
+ }
32
+ try ! await Task . sleep ( nanoseconds: 10_000_000 )
27
33
}
34
+ fatalError ( " Self was unexpectedly captured strongly " )
28
35
}
29
36
30
- runIn10ms { [ weak self] in
31
- guard let self else {
32
- print ( " Self was captured weakly (2) " )
33
- return
34
- }
37
+ let t2 = Task { [ weak self] in
38
+ for _ in 0 ..< checkIters {
39
+ guard let self else {
40
+ print ( " Self was captured weakly (2) " )
41
+ return
42
+ }
35
43
36
- // Use implicit self -- this should not result in a strong capture
37
- _ = property
38
-
39
- runIn10ms { [ self ] in
40
44
// Use implicit self -- this should not result in a strong capture
41
45
_ = property
42
- fatalError ( " Self was unexpectedly captured strongly " )
46
+
47
+ runIn10ms { [ self ] in
48
+ // Use implicit self -- this should not result in a strong capture
49
+ _ = property
50
+ }
51
+ try ! await Task . sleep ( nanoseconds: 10_000_000 )
43
52
}
53
+ fatalError ( " Self was unexpectedly captured strongly " )
44
54
}
55
+
56
+ return ( t1, t2)
45
57
}
46
58
}
47
59
48
- Weak ( ) . test ( )
49
- try await Task . sleep ( nanoseconds: 30_000_000 )
60
+ let ( t1, t2) = await Weak ( ) . test ( )
61
+ await t1. value
62
+ await t2. value
0 commit comments