Skip to content

[Concurrency] Task.immediate returning Never error must not have thro… #82373

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

Open
wants to merge 1 commit into
base: release/6.2
Choose a base branch
from
Open
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: 7 additions & 6 deletions stdlib/public/Concurrency/Task+immediate.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import Swift
% 'THROWING',
% 'NON_THROWING',
% ]
% OPERATION_PARAM = '@_inheritActorContext @_implicitSelfCapture _ operation: __owned @Sendable @escaping () async throws -> Success'
% for METHOD_VARIANT in METHOD_VARIANTS:

% IS_THROWING = METHOD_VARIANT == 'THROWING'
% if IS_THROWING:
% FAILURE_TYPE = 'Error'
% THROWS = 'throws '
% else:
% FAILURE_TYPE = 'Never'
% THROWS = ''
% end

@available(SwiftStdlib 6.2, *)
Expand All @@ -46,7 +47,7 @@ extension Task where Failure == ${FAILURE_TYPE} {
public static func startSynchronously(
name: String? = nil,
priority: TaskPriority? = nil,
@_implicitSelfCapture @_inheritActorContext(always) _ operation: sending @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: operation)
}
Expand Down Expand Up @@ -79,7 +80,7 @@ extension Task where Failure == ${FAILURE_TYPE} {
public static func immediate(
name: String? = nil,
priority: TaskPriority? = nil,
@_implicitSelfCapture @_inheritActorContext(always) operation: sending @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 @@ -235,15 +236,15 @@ extension ${GROUP_TYPE} {
% 'THROWING',
% 'NON_THROWING',
% ]
% OPERATION_PARAM = '@_inheritActorContext @_implicitSelfCapture _ operation: __owned @Sendable @escaping @MainActor () async throws -> Success'
% for METHOD_VARIANT in METHOD_VARIANTS:

% IS_THROWING = METHOD_VARIANT == 'THROWING'
% if IS_THROWING:
% FAILURE_TYPE = 'Error'
% THROWS = 'throws '
% else:
% FAILURE_TYPE = 'Never'
% OPERATION_PARAM = OPERATION_PARAM.replace('throws', '')
% THROWS = ''
% end

#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY && !SWIFT_CONCURRENCY_EMBEDDED
Expand All @@ -256,7 +257,7 @@ extension Task where Failure == ${FAILURE_TYPE} {
@discardableResult
public static func startOnMainActor(
priority: TaskPriority? = nil,
${OPERATION_PARAM}
@_inheritActorContext @_implicitSelfCapture _ operation: __owned @Sendable @escaping @MainActor () async ${THROWS} -> Success
) -> Task<Success, ${FAILURE_TYPE}> {
let flags = taskCreateFlags(
priority: priority,
Expand Down
32 changes: 25 additions & 7 deletions test/Concurrency/startImmediatelyIsolation.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// RUN: %target-build-swift -swift-version 6 %s -strict-concurrency=complete -Xfrontend -verify

// RUN: %target-swift-frontend -parse-as-library -swift-version 6 -strict-concurrency=complete -emit-sil -verify %s
// REQUIRES: concurrency

@available(SwiftStdlib 6.2, *)
Expand Down Expand Up @@ -41,11 +40,30 @@ func async() async throws {

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

func test() {
Task.immediate {
method() // Ok due to `@_implicitSelfCapture`
}
}
}

func test() {
Task.immediate {
method() // Ok due to `@_implicitSelfCapture`
}
@available(SwiftStdlib 6.2, *)
struct TestThrowing {
func test() {
// expected-error@+1{{invalid conversion from throwing function of type '() throws -> Void' to non-throwing function type '@isolated(any) () async -> Void'}}
let t: Task<Void, Never> = Task.immediate {
throw Boom()
}
_ = t

// ok
let t2: Task<Void, Error> = Task.immediate {
throw Boom()
}
_ = t2
}
}

struct Boom: Error {}