|
| 1 | +// RUN: %target-typecheck-verify-swift -swift-version 6 |
| 2 | + |
| 3 | +// REQUIRES: concurrency |
| 4 | + |
| 5 | +struct MyType<T: Sendable> : Sendable { |
| 6 | + enum MyError: Error { |
| 7 | + case error |
| 8 | + } |
| 9 | + |
| 10 | + public init(_ h: T) { self.handler = h } |
| 11 | + |
| 12 | + public var handler: T |
| 13 | + |
| 14 | + public func handle<OperationInput, OperationOutput>( |
| 15 | + forOperation operationID: String, |
| 16 | + using handlerMethod: @Sendable @escaping (T) |
| 17 | + -> ((OperationInput) async throws -> OperationOutput), |
| 18 | + deserializer: @Sendable @escaping ( |
| 19 | + ) throws -> OperationInput, |
| 20 | + serializer: @Sendable @escaping (OperationOutput) throws |
| 21 | + -> () |
| 22 | + ) async throws -> () where OperationInput: Sendable, |
| 23 | + OperationOutput: Sendable |
| 24 | + { |
| 25 | + @Sendable |
| 26 | + func wrappingErrors<R>( |
| 27 | + work: () async throws -> R, |
| 28 | + mapError: (any Error) -> any Error |
| 29 | + ) async throws -> R { |
| 30 | + fatalError() |
| 31 | + } |
| 32 | + @Sendable |
| 33 | + func makeError( |
| 34 | + input: OperationInput? = nil, |
| 35 | + output: OperationOutput? = nil, |
| 36 | + error: any Error |
| 37 | + ) -> any Error { |
| 38 | + fatalError() |
| 39 | + } |
| 40 | + var next: @Sendable () async throws // expected-warning {{}} |
| 41 | + -> () = { |
| 42 | + let input: OperationInput = try await wrappingErrors { |
| 43 | + try deserializer() |
| 44 | + } mapError: { error in |
| 45 | + makeError(error: error) |
| 46 | + } |
| 47 | + let output: OperationOutput = try await wrappingErrors { |
| 48 | + let method = handlerMethod(handler) |
| 49 | + return try await wrappingErrors { |
| 50 | + try await method(input) |
| 51 | + } mapError: { error in |
| 52 | + MyError.error |
| 53 | + } |
| 54 | + } mapError: { error in |
| 55 | + makeError(input: input, error: error) |
| 56 | + } |
| 57 | + return try await wrappingErrors { |
| 58 | + try serializer(output) |
| 59 | + } mapError: { error in |
| 60 | + makeError(input: input, output: output, error: error) |
| 61 | + } |
| 62 | + } |
| 63 | + return try await next() |
| 64 | + } |
| 65 | +} |
0 commit comments