Skip to content

Commit 53921e4

Browse files
authored
Merge pull request #78920 from kubamracek/embedded-wae
[embedded] Support withoutActuallyEscaping in Embedded Swift
2 parents 5d23a4d + f4c69e1 commit 53921e4

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

stdlib/public/core/EmbeddedRuntime.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,22 @@ public func swift_dynamicCastClass(object: UnsafeMutableRawPointer, targetMetada
272272
public func swift_dynamicCastClassUnconditional(object: UnsafeMutableRawPointer, targetMetadata: UnsafeRawPointer,
273273
file: UnsafePointer<CChar>, line: CUnsignedInt, column: CUnsignedInt) -> UnsafeMutableRawPointer {
274274
guard let result = swift_dynamicCastClass(object: object, targetMetadata: targetMetadata) else {
275-
Builtin.int_trap()
275+
fatalError("failed cast")
276276
}
277277
return result
278278
}
279279

280+
@_cdecl("swift_isEscapingClosureAtFileLocation")
281+
public func swift_isEscapingClosureAtFileLocation(object: Builtin.RawPointer, filename: UnsafePointer<CChar>, filenameLength: Int32, line: Int32, column: Int32, verificationType: CUnsignedInt) -> Bool {
282+
let objectBits = UInt(Builtin.ptrtoint_Word(object))
283+
if objectBits == 0 { return false }
284+
285+
guard swift_isUniquelyReferenced_native(object: object) else {
286+
fatalError("non-escaping closure escaped")
287+
}
288+
return false
289+
}
290+
280291
@_cdecl("swift_isUniquelyReferenced_native")
281292
public func swift_isUniquelyReferenced_native(object: Builtin.RawPointer) -> Bool {
282293
if !isValidPointerForNativeRetain(object: object) { return false }
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-clang -x c -c %S/Inputs/unbuffered-putchar.c -o %t/unbuffered-putchar.o
3+
4+
// RUN: %target-build-swift -enable-experimental-feature Embedded -wmo %s -Xlinker %t/unbuffered-putchar.o -o %t/a.out
5+
// RUN: not --crash %t/a.out 2>&1 | %FileCheck %s
6+
7+
// REQUIRES: swift_in_compiler
8+
// REQUIRES: executable_test
9+
// REQUIRES: swift_test_mode_optimize_none
10+
// REQUIRES: swift_feature_Embedded
11+
12+
var sink: ()->() = {}
13+
14+
func dontEscape(f: () -> ()) {
15+
withoutActuallyEscaping(f) {
16+
$0()
17+
}
18+
}
19+
20+
func dontEscape2(f: () -> ()) {
21+
withoutActuallyEscaping(f) {
22+
sink = $0
23+
sink()
24+
sink = {}
25+
}
26+
}
27+
28+
func letEscape(f: () -> ()) {
29+
withoutActuallyEscaping(f) {
30+
sink = $0
31+
sink()
32+
}
33+
}
34+
35+
dontEscape(f: { print("A") })
36+
dontEscape2(f: { print("B") })
37+
letEscape(f: { print("C") })
38+
39+
// CHECK: A
40+
// CHECK: B
41+
// CHECK: C
42+
// CHECK: non-escaping closure escaped

0 commit comments

Comments
 (0)