Skip to content

Commit ef622dd

Browse files
authored
Merge pull request #82373 from ktoso/pick-wip-NeverErrorTaskShouldNotThrowInOperation
[6.2][Concurrency] Task.immediate returning Never error must not have thro…
2 parents 4a39ed2 + 336f3cd commit ef622dd

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

stdlib/public/Concurrency/Task+immediate.swift.gyb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ import Swift
1919
% 'THROWING',
2020
% 'NON_THROWING',
2121
% ]
22-
% OPERATION_PARAM = '@_inheritActorContext @_implicitSelfCapture _ operation: __owned @Sendable @escaping () async throws -> Success'
2322
% for METHOD_VARIANT in METHOD_VARIANTS:
2423

2524
% IS_THROWING = METHOD_VARIANT == 'THROWING'
2625
% if IS_THROWING:
2726
% FAILURE_TYPE = 'Error'
27+
% THROWS = 'throws '
2828
% else:
2929
% FAILURE_TYPE = 'Never'
30+
% THROWS = ''
3031
% end
3132

3233
@available(SwiftStdlib 6.2, *)
@@ -46,7 +47,7 @@ extension Task where Failure == ${FAILURE_TYPE} {
4647
public static func startSynchronously(
4748
name: String? = nil,
4849
priority: TaskPriority? = nil,
49-
@_implicitSelfCapture @_inheritActorContext(always) _ operation: sending @isolated(any) @escaping () async throws -> Success
50+
@_implicitSelfCapture @_inheritActorContext(always) _ operation: sending @isolated(any) @escaping () async ${THROWS} -> Success
5051
) -> Task<Success, ${FAILURE_TYPE}> {
5152
immediate(name: name, priority: priority, operation: operation)
5253
}
@@ -79,7 +80,7 @@ extension Task where Failure == ${FAILURE_TYPE} {
7980
public static func immediate(
8081
name: String? = nil,
8182
priority: TaskPriority? = nil,
82-
@_implicitSelfCapture @_inheritActorContext(always) operation: sending @isolated(any) @escaping () async throws -> Success
83+
@_implicitSelfCapture @_inheritActorContext(always) operation: sending @isolated(any) @escaping () async ${THROWS} -> Success
8384
) -> Task<Success, ${FAILURE_TYPE}> {
8485

8586
let builtinSerialExecutor =
@@ -235,15 +236,15 @@ extension ${GROUP_TYPE} {
235236
% 'THROWING',
236237
% 'NON_THROWING',
237238
% ]
238-
% OPERATION_PARAM = '@_inheritActorContext @_implicitSelfCapture _ operation: __owned @Sendable @escaping @MainActor () async throws -> Success'
239239
% for METHOD_VARIANT in METHOD_VARIANTS:
240240

241241
% IS_THROWING = METHOD_VARIANT == 'THROWING'
242242
% if IS_THROWING:
243243
% FAILURE_TYPE = 'Error'
244+
% THROWS = 'throws '
244245
% else:
245246
% FAILURE_TYPE = 'Never'
246-
% OPERATION_PARAM = OPERATION_PARAM.replace('throws', '')
247+
% THROWS = ''
247248
% end
248249

249250
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY && !SWIFT_CONCURRENCY_EMBEDDED
@@ -256,7 +257,7 @@ extension Task where Failure == ${FAILURE_TYPE} {
256257
@discardableResult
257258
public static func startOnMainActor(
258259
priority: TaskPriority? = nil,
259-
${OPERATION_PARAM}
260+
@_inheritActorContext @_implicitSelfCapture _ operation: __owned @Sendable @escaping @MainActor () async ${THROWS} -> Success
260261
) -> Task<Success, ${FAILURE_TYPE}> {
261262
let flags = taskCreateFlags(
262263
priority: priority,

test/Concurrency/startImmediatelyIsolation.swift

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// RUN: %target-build-swift -swift-version 6 %s -strict-concurrency=complete -Xfrontend -verify
2-
1+
// RUN: %target-swift-frontend -parse-as-library -swift-version 6 -emit-sil -verify %s
32
// REQUIRES: concurrency
43

54
@available(SwiftStdlib 6.2, *)
@@ -41,11 +40,30 @@ func async() async throws {
4140

4241
@available(SwiftStdlib 6.2, *)
4342
actor TestSelfCapture {
44-
func method() {}
43+
func method() {}
44+
45+
func test() {
46+
Task.immediate {
47+
method() // Ok due to `@_implicitSelfCapture`
48+
}
49+
}
50+
}
4551

46-
func test() {
47-
Task.immediate {
48-
method() // Ok due to `@_implicitSelfCapture`
49-
}
52+
@available(SwiftStdlib 6.2, *)
53+
struct TestThrowing {
54+
func test() {
55+
// expected-error@+1{{invalid conversion from throwing function of type '() throws -> Void' to non-throwing function type '@isolated(any) () async -> Void'}}
56+
let t: Task<Void, Never> = Task.immediate {
57+
throw Boom()
58+
}
59+
_ = t
60+
61+
// ok
62+
let t2: Task<Void, Error> = Task.immediate {
63+
throw Boom()
5064
}
65+
_ = t2
66+
}
5167
}
68+
69+
struct Boom: Error {}

0 commit comments

Comments
 (0)