|
| 1 | +// RUN: %target-swift-frontend -emit-ir %s -disable-availability-checking |
| 2 | + |
| 3 | +protocol ContextDescriptor: Sendable { } |
| 4 | + |
| 5 | +protocol OutcomeDescriptor: Sendable { } |
| 6 | + |
| 7 | +protocol ResultDescriptor: Sendable { } |
| 8 | + |
| 9 | +protocol ErasedReducer<Context>: Sendable { |
| 10 | + associatedtype Context: ContextDescriptor |
| 11 | + associatedtype TriggerOutcome: OutcomeDescriptor |
| 12 | + associatedtype Result: ResultDescriptor |
| 13 | + |
| 14 | + var name: String { get } |
| 15 | + var function: @Sendable (_ context: Context?, _ trigger: TriggerOutcome) -> Result { get } |
| 16 | +} |
| 17 | + |
| 18 | +protocol Reducer<Context, TriggerOutcome>: ErasedReducer { |
| 19 | + var name: String { get } |
| 20 | + var function: @Sendable (_ context: Context?, _ trigger: TriggerOutcome) -> Result { get } |
| 21 | +} |
| 22 | + |
| 23 | +struct ExampleContext: ContextDescriptor { } |
| 24 | + |
| 25 | +struct ExampleOutcome: OutcomeDescriptor { } |
| 26 | + |
| 27 | +struct ExampleResult: ResultDescriptor { } |
| 28 | + |
| 29 | +struct ExampleReducer<Context: ContextDescriptor, TriggerOutcome: OutcomeDescriptor, Result: ResultDescriptor>: Reducer { |
| 30 | + let name: String |
| 31 | + let function: @Sendable (_ context: Context?, _ trigger: TriggerOutcome) -> Result |
| 32 | +} |
| 33 | + |
| 34 | +class ExampleService<Context: ContextDescriptor> { |
| 35 | + let reducers: [any ErasedReducer<Context>] |
| 36 | + |
| 37 | + public init(reducers: [any ErasedReducer<Context>]) { |
| 38 | + self.reducers = reducers |
| 39 | + } |
| 40 | + |
| 41 | + func reduce<TriggerOutcome: OutcomeDescriptor>(outcome: TriggerOutcome, context: Context) -> (any ResultDescriptor)? { |
| 42 | + // This line appears to be what's causing the crash |
| 43 | + guard let reducer = (reducers.compactMap { reducer in reducer as? any Reducer<Context, TriggerOutcome> }).first else { |
| 44 | + return nil |
| 45 | + } |
| 46 | + return reducer.function(context, outcome) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +public func testReducing() { |
| 52 | + let erasedReducers: [any ErasedReducer<ExampleContext>] = [ |
| 53 | + ExampleReducer<ExampleContext, ExampleOutcome, ExampleResult>(name: "Example", function: { (_, _) in ExampleResult() }) |
| 54 | + ] |
| 55 | + let context = ExampleContext() |
| 56 | + let trigger = ExampleOutcome() |
| 57 | + |
| 58 | + let service = ExampleService<ExampleContext>(reducers: erasedReducers) |
| 59 | + |
| 60 | + _ = service.reduce(outcome: trigger, context: context) |
| 61 | +} |
0 commit comments