Skip to content

[embedded] Support withoutActuallyEscaping in Embedded Swift #78920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion stdlib/public/core/EmbeddedRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,22 @@ public func swift_dynamicCastClass(object: UnsafeMutableRawPointer, targetMetada
public func swift_dynamicCastClassUnconditional(object: UnsafeMutableRawPointer, targetMetadata: UnsafeRawPointer,
file: UnsafePointer<CChar>, line: CUnsignedInt, column: CUnsignedInt) -> UnsafeMutableRawPointer {
guard let result = swift_dynamicCastClass(object: object, targetMetadata: targetMetadata) else {
Builtin.int_trap()
fatalError("failed cast")
}
return result
}

@_cdecl("swift_isEscapingClosureAtFileLocation")
public func swift_isEscapingClosureAtFileLocation(object: Builtin.RawPointer, filename: UnsafePointer<CChar>, filenameLength: Int32, line: Int32, column: Int32, verificationType: CUnsignedInt) -> Bool {
let objectBits = UInt(Builtin.ptrtoint_Word(object))
if objectBits == 0 { return false }

guard swift_isUniquelyReferenced_native(object: object) else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will raise a fatal error when object is NULL, whereas the C++ implementation returns false in that case. I'm not sure if it actually matters in practice (is this ever called with NULL legitimately?) but it would probably be good to match them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, fixed!

fatalError("non-escaping closure escaped")
}
return false
}

@_cdecl("swift_isUniquelyReferenced_native")
public func swift_isUniquelyReferenced_native(object: Builtin.RawPointer) -> Bool {
if !isValidPointerForNativeRetain(object: object) { return false }
Expand Down
42 changes: 42 additions & 0 deletions test/embedded/without-actually-escaping.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// RUN: %empty-directory(%t)
// RUN: %target-clang -x c -c %S/Inputs/unbuffered-putchar.c -o %t/unbuffered-putchar.o

// RUN: %target-build-swift -enable-experimental-feature Embedded -wmo %s -Xlinker %t/unbuffered-putchar.o -o %t/a.out
// RUN: not --crash %t/a.out 2>&1 | %FileCheck %s

// REQUIRES: swift_in_compiler
// REQUIRES: executable_test
// REQUIRES: swift_test_mode_optimize_none
// REQUIRES: swift_feature_Embedded

var sink: ()->() = {}

func dontEscape(f: () -> ()) {
withoutActuallyEscaping(f) {
$0()
}
}

func dontEscape2(f: () -> ()) {
withoutActuallyEscaping(f) {
sink = $0
sink()
sink = {}
}
}

func letEscape(f: () -> ()) {
withoutActuallyEscaping(f) {
sink = $0
sink()
}
}

dontEscape(f: { print("A") })
dontEscape2(f: { print("B") })
letEscape(f: { print("C") })

// CHECK: A
// CHECK: B
// CHECK: C
// CHECK: non-escaping closure escaped