Skip to content

Commit 11764ae

Browse files
committed
[Concurrency] Add an API for extracting the isolation of a dynamically isolated
function value.
1 parent cbd70e0 commit 11764ae

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

include/swift/Basic/Features.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ EXPERIMENTAL_FEATURE(DynamicActorIsolation, false)
290290
EXPERIMENTAL_FEATURE(BorrowingSwitch, true)
291291

292292
// Enable isolated(any) attribute on function types.
293-
EXPERIMENTAL_FEATURE(IsolatedAny, false)
293+
EXPERIMENTAL_FEATURE(IsolatedAny, true)
294294

295295
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
296296
#undef EXPERIMENTAL_FEATURE

stdlib/public/Concurrency/Actor.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,11 @@ internal func _enqueueOnMain(_ job: UnownedJob)
8585
public macro isolation<T>() -> T = Builtin.IsolationMacro
8686
#endif
8787

88+
#if $IsolatedAny
89+
@available(SwiftStdlib 5.1, *)
90+
public func extractIsolation<each Arg, Result>(
91+
_ fn: @escaping @isolated(any) (repeat each Arg) async throws -> Result
92+
) -> (any Actor)? {
93+
return Builtin.extractFunctionIsolation(fn)
94+
}
95+
#endif

stdlib/public/Concurrency/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ else()
6060
list(APPEND SWIFT_RUNTIME_CONCURRENCY_C_FLAGS "-fswift-async-fp=never")
6161
endif()
6262

63+
list(APPEND SWIFT_RUNTIME_CONCURRENCY_SWIFT_FLAGS
64+
"-enable-experimental-feature"
65+
"IsolatedAny"
66+
)
6367

6468
list(APPEND SWIFT_RUNTIME_CONCURRENCY_C_FLAGS
6569
"-D__STDC_WANT_LIB_EXT1__=1")

test/Concurrency/isolated_any.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ func globalNonisolatedFunction() {}
55

66
actor A {
77
func actorFunction() {}
8+
func asyncActorFunction() async {}
9+
func asyncThrowsActorFunction() async throws {}
10+
func actorFunctionWithArgs(value: Int) async -> String { "" }
811
}
912

1013
func testBasic_sync() {
@@ -69,3 +72,12 @@ func testConvertIsolatedAnyToMainActor(fn: @Sendable @isolated(any) () -> ()) {
6972
// expected-error @+1 {{cannot convert value of type '@isolated(any) @Sendable () -> ()' to expected argument type '@MainActor @Sendable () -> ()'}}
7073
requireSendableGlobalActor(fn)
7174
}
75+
76+
func extractFunctionIsolation(_ fn: @isolated(any) @escaping () async -> Void) {
77+
let _: (any Actor)? = extractIsolation(fn)
78+
79+
let myActor = A()
80+
let _: (any Actor)? = extractIsolation(myActor.asyncActorFunction)
81+
let _: (any Actor)? = extractIsolation(myActor.asyncThrowsActorFunction)
82+
let _: (any Actor)? = extractIsolation(myActor.actorFunctionWithArgs(value:))
83+
}

0 commit comments

Comments
 (0)