Skip to content

Commit 8e3ee38

Browse files
committed
[embedded] Support withoutActuallyEscaping in Embedded Swift
1 parent 5770d59 commit 8e3ee38

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

stdlib/public/core/EmbeddedRuntime.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,19 @@ 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+
guard swift_isUniquelyReferenced_native(object: object) else {
283+
fatalError("non-escaping closure escaped")
284+
}
285+
return false
286+
}
287+
280288
@_cdecl("swift_isUniquelyReferenced_native")
281289
public func swift_isUniquelyReferenced_native(object: Builtin.RawPointer) -> Bool {
282290
if !isValidPointerForNativeRetain(object: object) { return false }
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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_feature_Embedded
10+
11+
var sink: ()->() = {}
12+
13+
func dontEscape(f: () -> ()) {
14+
withoutActuallyEscaping(f) {
15+
$0()
16+
}
17+
}
18+
19+
func dontEscape2(f: () -> ()) {
20+
withoutActuallyEscaping(f) {
21+
sink = $0
22+
sink()
23+
sink = {}
24+
}
25+
}
26+
27+
func letEscape(f: () -> ()) {
28+
withoutActuallyEscaping(f) {
29+
sink = $0
30+
sink()
31+
}
32+
}
33+
34+
dontEscape(f: { print("A") })
35+
dontEscape2(f: { print("B") })
36+
letEscape(f: { print("C") })
37+
38+
// CHECK: A
39+
// CHECK: B
40+
// CHECK: C
41+
// CHECK: non-escaping closure escaped

0 commit comments

Comments
 (0)