Swift 5.9 Build Error on switch expression

Because Swift 5.9 includes SE-0380, I would expect the following to build:

final class Box {
  var value: Any

  func value<V>(forKey key: String) -> V? {
    switch key {
    case .stringKey:
      // error: variable '<unknown>' captured by a closure before being initialized
      value as? V
    case .intKey:
      // error: variable '<unknown>' captured by a closure before being initialized
      value as? V
    case .boolKey:
      // error: variable '<unknown>' captured by a closure before being initialized
      value as? V
    default:
      nil
    }
  }

  init(value: Any) {
    self.value = value
  }
}

extension String {
  static let stringKey = "stringKey"
  static let intKey = "intKey"
  static let boolKey = "boolKey"
}

Is value as? V not a single expression? Is this a compiler bug?

2 Likes

Resolved Error for switch expression with Swift 5.9 · Issue #68764 · apple/swift · GitHub

1 Like