Skip to content

[stdlib] Adopt @_inheritActorContext(always) on Task.immediate #81572

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
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
10 changes: 7 additions & 3 deletions stdlib/public/Concurrency/Task+immediate.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ extension Task where Failure == ${FAILURE_TYPE} {
public static func startSynchronously(
name: String? = nil,
priority: TaskPriority? = nil,
@_implicitSelfCapture _ operation: __owned @isolated(any) @escaping () async throws -> Success
@_implicitSelfCapture @_inheritActorContext(always) _ operation: sending @isolated(any) @escaping () async throws -> Success
) -> Task<Success, ${FAILURE_TYPE}> {
immediate(name: name, priority: priority, operation)
immediate(name: name, priority: priority, operation: operation)
}

/// Create and immediately start running a new task in the context of the calling thread/task.
Expand All @@ -66,13 +66,14 @@ extension Task where Failure == ${FAILURE_TYPE} {
/// - operation: the operation to be run immediately upon entering the task.
/// - Returns: A reference to the unstructured task which may be awaited on.
@available(SwiftStdlib 6.2, *)
@_alwaysEmitIntoClient
@discardableResult
public static func immediate(
name: String? = nil,
priority: TaskPriority? = nil,
% # NOTE: This closure cannot be 'sending' because we'll trigger ' pattern that the region based isolation checker does not understand how to check'
% # In this case: `func syncOnMyGlobalActor() { Task.immediate { @MyGlobalActor in } }`
@_implicitSelfCapture _ operation: __owned @isolated(any) @escaping () async throws -> Success
@_implicitSelfCapture @_inheritActorContext(always) operation: sending @isolated(any) @escaping () async throws -> Success
) -> Task<Success, ${FAILURE_TYPE}> {

let builtinSerialExecutor =
Expand Down Expand Up @@ -188,6 +189,7 @@ extension ${GROUP_TYPE} {
/// is semantically equivalent to its basic version which can be
/// created using ``${GROUP_TYPE}/addTask``.
@available(SwiftStdlib 6.2, *)
@_alwaysEmitIntoClient
public func ${METHOD_NAME}( // in ${GROUP_TYPE}
name: String? = nil,
priority: TaskPriority? = nil,
Expand Down Expand Up @@ -275,5 +277,7 @@ extension Task where Failure == ${FAILURE_TYPE} {
@_silgen_name("swift_task_startOnMainActor")
internal func _startTaskOnMainActor(_ task: Builtin.NativeObject)

@available(SwiftStdlib 6.2, *)
@_silgen_name("swift_task_immediate")
@usableFromInline
internal func _startTaskImmediately(_ task: Builtin.NativeObject, targetExecutor: Builtin.Executor?)
3 changes: 0 additions & 3 deletions test/Concurrency/Runtime/startImmediately.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// FIXME: Marking this disabled since we're reworking the semantics and the test is a bit racy until we do
// REQUIRES: rdar149506152

// RUN: %empty-directory(%t)
// RUN: %target-build-swift -Xfrontend -disable-availability-checking %s %import-libdispatch -swift-version 6 -o %t/a.out
// RUN: %target-codesign %t/a.out
Expand Down
11 changes: 11 additions & 0 deletions test/Concurrency/startImmediatelyIsolation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,14 @@ func async() async throws {
group.addImmediateTaskUnlessCancelled { () async throws -> Void in }
}
}

@available(SwiftStdlib 6.2, *)
actor TestSelfCapture {
func method() {}

func test() {
Task.immediate {
method() // Ok due to `@_implicitSelfCapture`
}
}
}
10 changes: 0 additions & 10 deletions test/abi/macOS/arm64/concurrency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -393,16 +393,6 @@ Added: _swift_task_getCurrentTaskName
Added: _swift_task_immediate
Added: _$sScTss5Error_pRs_rlE18startSynchronously4name8priority_ScTyxsAA_pGSSSg_ScPSgxyYaKYAcntFZ
Added: _$sScTss5NeverORs_rlE18startSynchronously4name8priority_ScTyxABGSSSg_ScPSgxyYaKYAcntFZ
Added: _$sScG16addImmediateTask4name8priority9operationySSSg_ScPSgxyYaYAcntF
Added: _$sScg31addImmediateTaskUnlessCancelled4name8priority9operationySSSg_ScPSgxyYaKYAcntF
Added: _$sScG31addImmediateTaskUnlessCancelled4name8priority9operationySSSg_ScPSgxyYaYAcntF
Added: _$sScTss5NeverORs_rlE9immediate4name8priority_ScTyxABGSSSg_ScPSgxyYaKYAcntFZ
Added: _$sScTss5Error_pRs_rlE9immediate4name8priority_ScTyxsAA_pGSSSg_ScPSgxyYaKYAcntFZ
Added: _$sScg16addImmediateTask4name8priority9operationySSSg_ScPSgxyYaKYAcntF
Added: _$ss19DiscardingTaskGroupV012addImmediateB04name8priority9operationySSSg_ScPSgyyYaYAcntF
Added: _$ss19DiscardingTaskGroupV012addImmediateB15UnlessCancelled4name8priority9operationySSSg_ScPSgyyYaYAcntF
Added: _$ss27ThrowingDiscardingTaskGroupV012addImmediateC04name8priority9operationySSSg_ScPSgyyYaKYAcntF
Added: _$ss27ThrowingDiscardingTaskGroupV012addImmediateC15UnlessCancelled4name8priority9operationySSSg_ScPSgyyYaKYAcntF

// isIsolatingCurrentContext
Added: _swift_task_invokeSwiftIsIsolatingCurrentContext
Expand Down
10 changes: 0 additions & 10 deletions test/abi/macOS/x86_64/concurrency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -393,16 +393,6 @@ Added: _swift_task_getCurrentTaskName
Added: _swift_task_immediate
Added: _$sScTss5Error_pRs_rlE18startSynchronously4name8priority_ScTyxsAA_pGSSSg_ScPSgxyYaKYAcntFZ
Added: _$sScTss5NeverORs_rlE18startSynchronously4name8priority_ScTyxABGSSSg_ScPSgxyYaKYAcntFZ
Added: _$sScG16addImmediateTask4name8priority9operationySSSg_ScPSgxyYaYAcntF
Added: _$sScg31addImmediateTaskUnlessCancelled4name8priority9operationySSSg_ScPSgxyYaKYAcntF
Added: _$sScG31addImmediateTaskUnlessCancelled4name8priority9operationySSSg_ScPSgxyYaYAcntF
Added: _$sScTss5NeverORs_rlE9immediate4name8priority_ScTyxABGSSSg_ScPSgxyYaKYAcntFZ
Added: _$sScTss5Error_pRs_rlE9immediate4name8priority_ScTyxsAA_pGSSSg_ScPSgxyYaKYAcntFZ
Added: _$sScg16addImmediateTask4name8priority9operationySSSg_ScPSgxyYaKYAcntF
Added: _$ss19DiscardingTaskGroupV012addImmediateB04name8priority9operationySSSg_ScPSgyyYaYAcntF
Added: _$ss19DiscardingTaskGroupV012addImmediateB15UnlessCancelled4name8priority9operationySSSg_ScPSgyyYaYAcntF
Added: _$ss27ThrowingDiscardingTaskGroupV012addImmediateC04name8priority9operationySSSg_ScPSgyyYaKYAcntF
Added: _$ss27ThrowingDiscardingTaskGroupV012addImmediateC15UnlessCancelled4name8priority9operationySSSg_ScPSgyyYaKYAcntF

// isIsolatingCurrentContext
Added: _swift_task_invokeSwiftIsIsolatingCurrentContext
Expand Down