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-10234] AnyHashable doesn't do NSNumber-like bridging on Linux #3502

Open
lilyball mannequin opened this issue Mar 29, 2019 · 3 comments
Open

[SR-10234] AnyHashable doesn't do NSNumber-like bridging on Linux #3502

lilyball mannequin opened this issue Mar 29, 2019 · 3 comments

Comments

@lilyball
Copy link
Mannequin

lilyball mannequin commented Mar 29, 2019

Previous ID SR-10234
Radar rdar://problem/49434879
Original Reporter @lilyball
Type Bug
Environment

Apple Swift version 5.0 (swiftlang-1001.0.69.5 clang-1001.0.46.3)
Target: x86_64-apple-darwin18.5.0

Swift version 4.2.3 (swift-4.2.3-RELEASE)
Target: x86_64-unknown-linux-gnu

Additional Detail from JIRA
Votes 0
Component/s Foundation, Standard Library
Labels Bug
Assignee @lorentey
Priority Medium

md5: 3762a595cf0c814bdbe714ab92679f7b

Issue Description:

On Apple platforms, after importing Foundation, AnyHashable does Obj-C bridging the way AnyObject does, which means it does NSNumber bridging rules for numeric casts. For example:

import Foundation
print(Int(4) as NSNumber as? Double as Any) // Optional(4.0)
print(Int(4) as AnyHashable as? Double as Any) // Optional(4.0)

On Linux (tested using the official swift 4.2 Docker image) this doesn't work. NSNumber bridges properly but AnyObject doesn't.

import Foundation
print(Int(4) as NSNumber as? Double as Any) // Optional(4.0)
print(Int(4) as AnyHashable as? Double as Any) // nil
@lilyball
Copy link
Mannequin Author

lilyball mannequin commented Mar 29, 2019

Also worth noting that on macOS, if you don’t import Foundation then AnyHashable doesn’t handle casts this way (it returns nil like Linux’s current behavior).

@lorentey
Copy link
Member

Since apple/swift#17396 AnyHashable has its own parallel reimplementation of Foundation's equality semantics, so that it can correctly implement Hashable.

Unfortunately, this mechanism doesn't currently extend to downcasting – as?-casts through AnyHashable between "equal" values of different types only works on Objective-C runtimes and only when Foundation is loaded.

swift-corelibs-foundation has some additional issues (https://bugs.swift.org/browse/SR-7436).

@lorentey
Copy link
Member

For primitive types that bridge with NSNumber, we can implement cross-casting by simply checking for all supported target types.

This approach would not work for collections, though:

[1, 2, 3] as [Int] as AnyHashable as? [Double]  // non-nil on Darwin if Foundation is loaded
[[1, 2, 3]] as Set<[Int]> as AnyHashable as? Set<[Double]>
etc.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant