-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Compiler crash with cast involving any existentials #71000
Comments
Thanks! This is supposed to be rejected; Revision update: 6d66211. |
Good to know. Also, what does the revision update mean? Is that a version in which this is fixed? |
Just a more recent dev revision containing the bug. |
@AnthonyLatsis Just to clarify, shouldn't it be possible in the end (with In the other direction, I believe that /* ... */
@available(iOS 16, *)
func run(a: Foo<any D<Int>>) {
_ = a as Foo<any C>
} |
No. This is not sound in general. You can search the web for "contravariance and covariance of generics" for many explanations of why this does not work in general. A few programming languages allow you to explicitly state whether your types are covariant and/or contravariant. Swift does not currently have any such capability, though it does have special logic in the compiler and runtime to support this for specific types (such as Array and function types). Regardless, the compiler should not crash here, of course. It should issue an error message. |
With the right feature—sure. The status quo is that Swift does not support Note that coercions ( class A {}
class B: A {}
struct G<T> {
let t: T
}
let _ = G(t: B()) as G<A> // Equivalent to 'let _: G<A> = G(t: B())' |
Duplicate of #63877 |
Description
It appears that casting a wrapped
any
existential to another one can cause a compiler crash.Reproduction
Stack dump
Expected behavior
The code should either compile or produce a diagnostic, instead of crashing.
Interestingly, if you use a force-cast
as!
rather thanas
you get the following warning:Environment
swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx14.0
Additional information
No response
The text was updated successfully, but these errors were encountered: