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-2911] Casting through options has bugs and misleading warning #45505

Closed
swift-ci opened this issue Oct 10, 2016 · 1 comment
Closed

[SR-2911] Casting through options has bugs and misleading warning #45505

swift-ci opened this issue Oct 10, 2016 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-2911
Radar None
Original Reporter erica (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 3525a407b4ca643437db033ecbad447d

Issue Description:

Normally in guard and if condition lists, you look up a value in a dictionary and conditionally cast:

if let value = dict[key] as? T, ...

The "as?" operator passes the Any? type through, and the lhs result is T, not T?. However, this doesn't happen when you conditionally cast T? to U, for example, when a dictionary is [AnyHashable: String]:

guard let value = dict["Key"] as? NSString
else { fatalError() }

See error shown here http://i.imgur.com/SkXkk6o.jpg

In this case, the compiler asserts that a cast from String? to an unrelated type NSString always fails. You can mitigate this by sticking an "Any" cast in the middle:

guard let value = dict["Key"] as Any as? NSString
else { fatalError() }

Joe Groff: "This is a bug. 'String as NSString' works, and you can cast through Optionals 'T? as? U', so transitively this also works, despite the misleading warning. Please file a bug report if you haven't yet."

@slavapestov
Copy link
Member

In Swift 3.1, we no longer diagnose the cast as always failing, instead we suggest changing 'as? NSString' to 'as NSString?', which is reasonable:

d.swift:5:31: warning: conditional downcast from 'String?' to 'NSString' is a bridging conversion; did you mean to use 'as'?
guard let value = dict["Key"] as? NSString
                  ~~~~~~~~~~~ ^~~ ~~~~~~~~
                              as          ?

After changing 'as? NSString' to 'as NSString?' (note the trailing ?) the code compiles without warning.

@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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants