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-10589] Compiler warning about "cast always fails" but it doesn't #52989

Closed
swift-ci opened this issue May 1, 2019 · 0 comments
Closed

[SR-10589] Compiler warning about "cast always fails" but it doesn't #52989

swift-ci opened this issue May 1, 2019 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented May 1, 2019

Previous ID SR-10589
Radar None
Original Reporter deanWombourne (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment

Apple Swift version 5.0.1 (swiftlang-1001.0.82.4 clang-1001.0.46.5)

Target: x86_64-apple-darwin18.5.0

Xcode Version 10.2.1 (10E1001)

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

md5: 20f8c9007a8bdceb03d9fbd10a5f2973

duplicates:

  • SR-7187 False positive warning: cast always fails from '[Any]' to '[String?]'

Issue Description:

The compiler is giving me a warning

Cast from '[String : Any]' to unrelated type '[String : MyProtocol?]' always fails

but this isn't true - at runtime the cast works correctly.

Here's an example showing this:

// Create a protocol, and conform some types to it
protocol MyProtocol { }
extension Int: MyProtocol { }
extension String: MyProtocol { }


// Create a dictionary out of those types
let dictionary = [ "a": 1, "b": "2" ] as [String: Any]

 
// Warning here, but the cast works correctly at runtime
let castDictionary = dictionary as? [String: MyProtocol?]

The warning only appears if the dictionary is cast to [String: MyProtocol?]. Casting to [String: MyProtocol] works as expected.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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
Projects
None yet
Development

No branches or pull requests

1 participant