Files
swift-mirror/validation-test/compiler_crashers_fixed/issue-74729.swift
Hamish Knight 4e811c3a88 [test] Merge crasher directories
There is no longer much of a good reason to keep these separate,
merge them.
2025-10-18 12:51:30 +01:00

62 lines
2.1 KiB
Swift

// RUN: %target-swift-frontend -emit-ir %s -disable-availability-checking
protocol ContextDescriptor: Sendable { }
protocol OutcomeDescriptor: Sendable { }
protocol ResultDescriptor: Sendable { }
protocol ErasedReducer<Context>: Sendable {
associatedtype Context: ContextDescriptor
associatedtype TriggerOutcome: OutcomeDescriptor
associatedtype Result: ResultDescriptor
var name: String { get }
var function: @Sendable (_ context: Context?, _ trigger: TriggerOutcome) -> Result { get }
}
protocol Reducer<Context, TriggerOutcome>: ErasedReducer {
var name: String { get }
var function: @Sendable (_ context: Context?, _ trigger: TriggerOutcome) -> Result { get }
}
struct ExampleContext: ContextDescriptor { }
struct ExampleOutcome: OutcomeDescriptor { }
struct ExampleResult: ResultDescriptor { }
struct ExampleReducer<Context: ContextDescriptor, TriggerOutcome: OutcomeDescriptor, Result: ResultDescriptor>: Reducer {
let name: String
let function: @Sendable (_ context: Context?, _ trigger: TriggerOutcome) -> Result
}
class ExampleService<Context: ContextDescriptor> {
let reducers: [any ErasedReducer<Context>]
public init(reducers: [any ErasedReducer<Context>]) {
self.reducers = reducers
}
func reduce<TriggerOutcome: OutcomeDescriptor>(outcome: TriggerOutcome, context: Context) -> (any ResultDescriptor)? {
// This line appears to be what's causing the crash
guard let reducer = (reducers.compactMap { reducer in reducer as? any Reducer<Context, TriggerOutcome> }).first else {
return nil
}
return reducer.function(context, outcome)
}
}
public func testReducing() {
let erasedReducers: [any ErasedReducer<ExampleContext>] = [
ExampleReducer<ExampleContext, ExampleOutcome, ExampleResult>(name: "Example", function: { (_, _) in ExampleResult() })
]
let context = ExampleContext()
let trigger = ExampleOutcome()
let service = ExampleService<ExampleContext>(reducers: erasedReducers)
_ = service.reduce(outcome: trigger, context: context)
}