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-12091] AnyHashable changes hashValue of floating-point numbers #54527

Closed
NevinBR opened this issue Jan 27, 2020 · 5 comments
Closed

[SR-12091] AnyHashable changes hashValue of floating-point numbers #54527

NevinBR opened this issue Jan 27, 2020 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@NevinBR
Copy link
Contributor

NevinBR commented Jan 27, 2020

Previous ID SR-12091
Radar rdar://problem/58999149
Original Reporter @NevinBR
Type Bug
Status Closed
Resolution Invalid
Environment

• Swift 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15)
• MacOS 10.14.6 (18G103)

I observe the same results in both:
• Xcode 11.3.1 playground for MacOS
• Swift REPL in Terminal

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

md5: f2c77ce20074961f74e964f514ed3697

Issue Description:

My understanding is that AnyHashable should preserve the hashValue of what it wraps. However, this does not hold for floating-point values which represent an integer in the range -0x1p63..<0x1p64:

let x = 1.0
let y = AnyHashable(x)
x.hashValue == y.hashValue  // false
@xwu
Copy link
Collaborator

xwu commented Jan 27, 2020

As commented on the Swift forums thread, this is in the intended behavior and is implemented [here|

// Float and Double are bridged with NSNumber, so we have to follow
]. As explained in the comments there, it is required for NSNumber bridging purposes.

@NevinBR
Copy link
Contributor Author

NevinBR commented Jan 28, 2020

The documentation for AnyHashable states:

The `AnyHashable` type forwards equality comparisons and hashing operations to an underlying hashable value, hiding its specific underlying type.

This strongly implies that the hashing operations will be forwarded to the wrapped value, and thus the result will be the same and unmodified therefrom.

@xwu
Copy link
Collaborator

xwu commented Jan 28, 2020

The documentation is out of date. See: https://bugs.swift.org/browse/SR-7496

@beccadax
Copy link
Contributor

@swift-ci create

@lorentey
Copy link
Member

While AnyHashable does often call the underlying value's hash(into🙂 implementation, it does not guarantee to preserve the hash values of any type. (Even if AnyHashable happens to preserve hash values in the current stdlib, please do not assume that this will always be the case – we may need to switch to a different hash encoding in a future release to prevent hash collisions between values of different underlying types.)

Please see the links below for more details:

https://forums.swift.org/t/why-isn-t-anyhashable-implemented-in-pure-swift/33124/7
#29498

@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. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

4 participants