Skip to content

Commit c0ea02f

Browse files
committed
[embedded] Add swift_allocEmptyBox to the embedded runtime
1 parent e9253b7 commit c0ea02f

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

stdlib/public/core/EmbeddedRuntime.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,15 @@ func swift_initStackObject(metadata: UnsafeMutablePointer<ClassMetadata>, object
236236
return unsafe object
237237
}
238238

239+
@unsafe
240+
public var _emptyBoxStorage: (Int, Int) = (/*isa*/0, /*refcount*/-1)
241+
242+
@_cdecl("swift_allocEmptyBox")
243+
public func swift_allocEmptyBox() -> Builtin.RawPointer {
244+
let box = unsafe Builtin.addressof(&_emptyBoxStorage)
245+
swift_retain(object: box)
246+
return box
247+
}
239248

240249

241250
/// Refcounting

test/embedded/empty-box.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -wmo) | %FileCheck %s
2+
3+
// REQUIRES: swift_in_compiler
4+
// REQUIRES: executable_test
5+
// REQUIRES: swift_feature_Embedded
6+
7+
public struct ZeroSizedStruct {}
8+
9+
var escape: (()->())? = nil
10+
11+
public func sink<T>(t: inout T) {
12+
13+
}
14+
15+
public func foo() {
16+
var s = ZeroSizedStruct()
17+
escape = {
18+
sink(t: &s)
19+
}
20+
}
21+
22+
foo()
23+
print("OK!")
24+
// CHECK: OK!

0 commit comments

Comments
 (0)