Skip to content

Swift 6 Regression: Existential Casting Is Broken #74646

Open
@stephencelis

Description

@stephencelis

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.castingFeature: explicit casting (is, as, as? and as!)runtimeThe Swift Runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions