Skip to content

Commit 3a95db9

Browse files
committed
Add test case that implicit self in a weak closure doesn't result in a strong capture
1 parent 8528021 commit 3a95db9

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

test/expr/implicit_weak_capture.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %target-run-simple-swift(-Xfrontend -disable-availability-checking)
2+
3+
// REQUIRES: executable_test
4+
5+
func runIn10ms(_ closure: @escaping @Sendable () -> Void) {
6+
Task {
7+
try! await Task.sleep(nanoseconds: 10_000_000)
8+
closure()
9+
}
10+
}
11+
12+
final class Weak: Sendable {
13+
let property = "Self exists"
14+
15+
func test() {
16+
runIn10ms { [weak self] in
17+
if let self {
18+
// Use implicit self -- this should not result in a strong capture
19+
_ = property
20+
fatalError("Self was unexpectedly captured strongly")
21+
} else {
22+
print("Self was captured weakly")
23+
}
24+
}
25+
}
26+
}
27+
28+
Weak().test()
29+
try await Task.sleep(nanoseconds: 20_000_000)

0 commit comments

Comments
 (0)