Skip to content

Commit 323e759

Browse files
Merge pull request #12744 from aschwaighofer/fix_deallocBox
runtime: Fix swift_deallocBox
2 parents b6ab713 + 7489bf7 commit 323e759

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

stdlib/public/runtime/HeapObject.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ BoxPair::Return SWIFT_RT_ENTRY_IMPL(swift_allocBox)(const Metadata *type) {
248248

249249
void swift::swift_deallocBox(HeapObject *o) {
250250
auto metadata = static_cast<const GenericBoxHeapMetadata *>(o->metadata);
251+
// Move the object to the deallocating state (+1 -> +0).
252+
o->refCounts.decrementFromOneNonAtomic();
251253
SWIFT_RT_ENTRY_CALL(swift_deallocObject)(o, metadata->getAllocSize(),
252254
metadata->getAllocAlignMask());
253255
}

test/Interpreter/closures.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,37 @@ map({()})
4848

4949
map({(x: Int) -> Int in x})
5050
// CHECK: Non-void overload
51+
52+
// This used to assert in runtime assert builds.
53+
protocol Initializable {
54+
init()
55+
}
56+
57+
func f2<T: Initializable>(_ x: T) -> T? { return nil }
58+
59+
func c<T: Initializable>(_ x: T) {
60+
61+
({
62+
guard var b = f2(x) else { print("success") ; return }
63+
let c = { b = T() }
64+
_ = (b, c)
65+
})()
66+
67+
}
68+
extension Bool : Initializable {
69+
init() {
70+
self = true
71+
}
72+
}
73+
// CHECK: success
74+
c(true)
75+
76+
77+
func f() -> Bool? { return nil }
78+
79+
// CHECK: success
80+
({
81+
guard var b = f() else { print("success") ; return }
82+
let c = { b = true }
83+
_ = (b, c)
84+
})()

0 commit comments

Comments
 (0)