Skip to content

Commit 59507ba

Browse files
committed
[Concurrency] Add an API for extracting the isolation of a dynamically isolated
function value.
1 parent 2c6344f commit 59507ba

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
@@ -7,6 +7,9 @@ func globalNonisolatedFunction() {}
77

88
actor A {
99
func actorFunction() {}
10+
func asyncActorFunction() async {}
11+
func asyncThrowsActorFunction() async throws {}
12+
func actorFunctionWithArgs(value: Int) async -> String { "" }
1013
}
1114

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

0 commit comments

Comments
 (0)