Skip to content
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

Casting fails in Swift 6 #74738

Closed
grantjbutler opened this issue Jun 26, 2024 · 2 comments
Closed

Casting fails in Swift 6 #74738

grantjbutler opened this issue Jun 26, 2024 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@grantjbutler
Copy link

grantjbutler commented Jun 26, 2024

Description

When compiling code with the Swift 6 compiler and running in a Swift 5 environment, such as an iOS 17 simulator, casting of values to protocols fails. When running the same code in other environment, such as an iOS 18 simulator, the casting succeeds as I would expect.

I'm not totally sure if there are specific requirements for the protocol or how it's used. The reproduction steps listed below are based off of the part of my codebase that's exhibiting this behavior, and I've confirmed that the issue exists in a fresh project with this code snippet.

Reproduction

protocol OptionalProtocol {}
extension Optional: OptionalProtocol {}

let value = Optional<Int>.some(0) as Any
if let optionalValue = value as? OptionalProtocol {
    print("The value is wrapped in an optional")
} else {
    print("The value is not wrapped in an optional")
}

Expected behavior

When running with Xcode 16 and targeting an iOS 17 simulator (that is, when using the Swift 6 compiler and targeting a device with a Swift 5 runtime), we should see "The value is wrapped in an optional" printed. Instead, we see "The value is not wrapped in an optional" printed. We see the correct behavior on the iOS 18 simulator. This code also behaves correctly when compiled with the Swift 5 compiler in Xcode 15.

Environment

Xcode 16 Beta 2 (16A5171r)
iOS 17.4 simulator, macOS 14
Apple Swift version 6.0 (swiftlang-6.0.0.4.52 clang-1600.0.21.1.3)

Additional information

I think whatever is causing this behavior, might be causing the behavior in #74333? They're similar enough that I'm suspicious, but I'm also not knowledgable enough with the compiler code to know if that's actually true or if I'm incorrectly relating two issues together.

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

Maybe related to #74282

@tbkka
Copy link
Contributor

tbkka commented Jun 27, 2024

Yes, it does seem to be related to #74282 which should be fixed by #74604.

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. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

3 participants