// RUN: %target-swift-frontend -emit-sil -verify %s

// https://github.com/apple/swift/issues/58149
// Crash while compiling Swift for TensorFlow
//
// This was caused by a bug in GenericSignatureBuilder, similar to one involving
// `CaseIterable`. In this reproducer, `KeyPathIterable` is similar enough to
// `CaseIterable` to have caused the GSB to crash. It was fixed by
// RequirementMachine abstract signatures.

import _Differentiation
@_spi(Reflection) import Swift

struct RNNCellInput<Input>: Differentiable {}
struct RNNCellOutput<Output>: Differentiable {}

protocol Layer: Differentiable {
  associatedtype Input
  associatedtype Output: Differentiable
  
  @differentiable(reverse)
  func callAsFunction(_ input: Input) -> Output
}

public protocol KeyPathIterable {
  associatedtype AllKeyPaths: Sequence
    where AllKeyPaths.Element == PartialKeyPath<Self>
}

protocol RecurrentLayerCell: Layer, KeyPathIterable
where
  Input == RNNCellInput<TimeStepInput>,
  Output == RNNCellOutput<TimeStepOutput>
{
  associatedtype TimeStepInput
  associatedtype TimeStepOutput: Differentiable
}

struct RecurrentLayer<Cell: RecurrentLayerCell>: Layer {
  typealias Input = Cell.TimeStepInput
  typealias Output = Cell.TimeStepOutput

  var cell: Cell

  @differentiable(reverse)
  func callAsFunction(_ inputs: Cell.TimeStepInput) -> Cell.TimeStepOutput {
    // expected-warning @+1 {{function call causes an infinite recursion}}
    return self(inputs)
  }
}
