Skip to content

Commit 3f68495

Browse files
committed
add some simple compilation tests
1 parent 89763f6 commit 3f68495

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

stdlib/public/Concurrency/Task.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extension Task {
3939
///
4040
/// This is appropriate when the caller is something "busy", like an event
4141
/// loop, and doesn't want to be potentially delayed by arbitrary work.
42-
func resume(returning: T) {
42+
public func resume(returning: T) {
4343
fatalError("\(#function) not implemented yet.")
4444
}
4545
}
@@ -52,26 +52,26 @@ extension Task {
5252
///
5353
/// This is appropriate when the caller is something "busy", like an event
5454
/// loop, and doesn't want to be potentially delayed by arbitrary work.
55-
func resume(returning: T) {
55+
public func resume(returning: T) {
5656
fatalError("\(#function) not implemented yet.")
5757
}
5858

59-
/// Resume the continuation with an error value and make the task schedulable.
59+
/// Resume the continuation with an error and make the task schedulable.
6060
///
6161
/// The task will never run synchronously, even if the task does not
6262
/// need to be resumed on a specific executor.
6363
///
6464
/// This is appropriate when the caller is something "busy", like an event
6565
/// loop, and doesn't want to be potentially delayed by arbitrary work.
66-
func resume(throwing: E) {
66+
public func resume(throwing: E) {
6767
fatalError("\(#function) not implemented yet.")
6868
}
6969
}
7070

7171
/// The operation functions must resume the continuation *exactly once*.
7272
///
7373
/// The continuation will not begin executing until the operation function returns.
74-
public func withUnsafeContinuation<T>(
74+
public static func withUnsafeContinuation<T>(
7575
operation: (UnsafeContinuation<T>) -> Void
7676
) async -> T {
7777
fatalError("\(#function) not implemented yet.")
@@ -80,7 +80,9 @@ extension Task {
8080
/// The operation functions must resume the continuation *exactly once*.
8181
///
8282
/// The continuation will not begin executing until the operation function returns.
83-
public func withUnsafeThrowingContinuation<T>(operation: (UnsafeThrowingContinuation<T, Error>) -> Void) async throws -> T {
83+
public static func withUnsafeThrowingContinuation<T>(
84+
operation: (UnsafeThrowingContinuation<T, Error>) -> Void
85+
) async throws -> T {
8486
fatalError("\(#function) not implemented yet.")
8587
}
8688
}

test/Concurrency/async_tasks.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-concurrency
2+
// REQUIRES: concurrency
3+
4+
func someAsyncFunc() async -> String { "" }
5+
struct MyError: Error {}
6+
7+
// non-async function with "weird shape" representing some API that has not adopted
8+
// swift concurrency yet, but we want to call it from an async function and make it
9+
// play nice with the rest of the system.
10+
func someManyCallbacksFunction(
11+
calledSometimes: () -> (),
12+
otherwiseThisIsCalled: () -> (),
13+
calledOnError: (Error) -> ()
14+
) { }
15+
16+
func test_unsafeContinuations() async {
17+
// the closure should not allow async operations;
18+
// after all: if you have async code, just call it directly, without the unsafe continuation
19+
let _: String = Task.withUnsafeContinuation { continuation in // expected-error{{cannot convert value of type '(_) async -> ()' to expected argument type '(Task.UnsafeContinuation<String>) -> Void'}}
20+
let s = await someAsyncFunc() // rdar://70610141 for getting a better error message here
21+
continuation.resume(returning: s)
22+
}
23+
24+
let _: String = await Task.withUnsafeContinuation { continuation in
25+
continuation.resume(returning: "")
26+
}
27+
}
28+
29+
func test_unsafeThrowingContinuations() async {
30+
let _: String = try await Task.withUnsafeThrowingContinuation { continuation in
31+
continuation.resume(returning: "")
32+
}
33+
34+
let _: String = try await Task.withUnsafeThrowingContinuation { continuation in
35+
continuation.resume(throwing: MyError())
36+
}
37+
38+
// TODO: Potentially could offer some warnings if we know that a continuation was resumed or escaped at all in a closure?
39+
}

0 commit comments

Comments
 (0)