Skip to content

Commit f48f3ad

Browse files
committed
[Test] Checked variable lifetimes extend self.
Verified that when a __consuming method calls a function which takes a closure that captures self weakly, self is not deallocated until the call returns. (Note that this is a behavioral change from what occurs when lexical borrow scopes are disabled; in that case, self is deallocated before the call to the function.)
1 parent 43324e8 commit f48f3ad

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

validation-test/SILOptimizer/lexical-lifetimes.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,31 @@ func test_localVar_keepsObjectAliveBeyondCallToSynchronizationPointFunction() {
142142
test_localVar_keepsObjectAliveBeyondCallToSynchronizationPointFunction_doit("blue")
143143
}
144144

145+
func do_foo(_ work: () -> ()) {
146+
work()
147+
}
148+
class Fooer {
149+
__consuming func foo() {
150+
weak var weakSelf = self
151+
do_foo {
152+
weakSelf?.foo1()
153+
weakSelf?.foo2()
154+
}
155+
}
156+
func foo1() {
157+
// CHECK: Fooer foo1
158+
print(type(of: self), #function)
159+
}
160+
func foo2() {
161+
// CHECK: Fooer foo2
162+
print(type(of: self), #function)
163+
}
164+
}
165+
166+
func test_self_keepsObjectAliveBeyond_callTo_functionTakingClosureCapturingWeakVar() {
167+
Fooer().foo()
168+
}
169+
145170
// =============================================================================
146171
// = Tests }} =
147172
// =============================================================================
@@ -154,6 +179,8 @@ func run() {
154179
test_localVar_keepsObjectAliveBeyondCallToClassWithPointer()
155180
test_localLet_keepsObjectAliveBeyondCallToSynchronizationPointFunction()
156181
test_localVar_keepsObjectAliveBeyondCallToSynchronizationPointFunction()
182+
183+
test_self_keepsObjectAliveBeyond_callTo_functionTakingClosureCapturingWeakVar()
157184
}
158185

159186
run()

0 commit comments

Comments
 (0)