Skip to content

Swift 6 Regression: Existential Casting Is Broken #74646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
stephencelis opened this issue Jun 23, 2024 · 1 comment
Open

Swift 6 Regression: Existential Casting Is Broken #74646

stephencelis opened this issue Jun 23, 2024 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. casting Feature: explicit casting (is, as, as? and as!) runtime The Swift Runtime

Comments

@stephencelis
Copy link
Contributor

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

@stephencelis stephencelis added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jun 23, 2024
@sindresorhus
Copy link

This may be related to #74282

@hborla hborla added runtime The Swift Runtime casting Feature: explicit casting (is, as, as? and as!) and removed triage needed This issue needs more specific labels labels Jul 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. casting Feature: explicit casting (is, as, as? and as!) runtime The Swift Runtime
Projects
None yet
Development

No branches or pull requests

3 participants