swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
66.76k stars 10.29k forks source link

Swift 6 Regression: Existential Casting Is Broken #74646

Open stephencelis opened 2 weeks ago

stephencelis commented 2 weeks ago

Description

A common trick employed in Swift is to conform Optional to some protocol in order to work around a lack of parameterized extensions (see this pitch).

This trick unfortunately broke in Swift 6.

Reproduction

Here's a test case:

import Testing

private protocol OptionalProtocol { static var none: Self { get } }
extension Optional: OptionalProtocol {}
func none<Result>(_: Result.Type) throws -> Result {
  if let result = Result.self as? any OptionalProtocol.Type {
    return result.none as! Result
  }
  throw MyError()
}
struct MyError: Error {}

@Test func example() async throws {
  let int = try none(Int?.self)
  #expect(int == nil)
}

Expected behavior

I would expect the test to pass, but instead an error is thrown.

Environment

swift-driver version: 1.109.2 Apple Swift version 6.0 (swiftlang-6.0.0.3.300 clang-1600.0.20.10) Target: arm64-apple-macosx14.0

Additional information

No response

sindresorhus commented 2 weeks ago

This may be related to https://github.com/swiftlang/swift/issues/74282