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

[SR-2146] Crash when @objc protocol existential is cast to another type and back in generic context #44754

Closed
jadengeller mannequin opened this issue Jul 22, 2016 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution

Comments

@jadengeller
Copy link
Mannequin

jadengeller mannequin commented Jul 22, 2016

Previous ID SR-2146
Radar None
Original Reporter @JadenGeller
Type Bug
Status Resolved
Resolution Done
Environment

Swift 2.2

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, RunTimeCrash
Assignee None
Priority Medium

md5: f13672703fe6c8f14099dcba6e9aeb7b

Issue Description:

The following crashes with the error "Could not cast value of type 'SwiftObjCProtocolBug.Test' (0x1002c6988) to 'SwiftObjCProtocolBug.Foo' (0x1005fd040)."

import Foundation

@objc protocol Foo { }
protocol Bar: Foo { }
class Test: Bar { }

func magic<T: Foo>(x: T) -> T {
    let y = x as Any
    return y as! T
}

print(magic(Test() as Foo))

Workaround: Add a redundant conformance to `Foo` when `Test` is declared. Oddly, the compiler doesn't warn us about this. Note that the redundant conformance cannot be added in an extension due to SR-2078.

@belkadan
Copy link
Contributor

Still broken in Swift 3 (with the necessary argument label change).

@Dante-Broggi
Copy link
Contributor

I believe this is fixed in or before Xcode 9.4.1's toolchain's Swift version.

@Dante-Broggi
Copy link
Contributor

Resolving as the provided code does not crash for me, and no one has responded since I asked.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
This issue was closed.
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. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution
Projects
None yet
Development

No branches or pull requests

3 participants