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-10734] RawRepresentable & Hashable #53126

Open
swift-ci opened this issue May 22, 2019 · 3 comments
Open

[SR-10734] RawRepresentable & Hashable #53126

swift-ci opened this issue May 22, 2019 · 3 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-10734
Radar rdar://problem/51319164
Original Reporter swhitty (JIRA User)
Type Bug
Status In Progress
Resolution
Environment

Xcode 10.2.1 (Swift 5 release)
Swift 5.1 Toolchain (9th May)

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

md5: b92071aaba993a89203e3d7671922455

Issue Description:

When a type conforms to RawRepresentable it is unable to provide reliable custom Hashable conformance.

struct Fish: Hashable, RawRepresentable {

  var rawValue: String

  init?(rawValue: String) {
    self.rawValue = rawValue
  }

  func hash(into hasher: inout Hasher) {
    hasher.combine(1)
  }

  static func == (lhs: Fish, rhs: Fish) -> Bool {
    return true
  }
}

let f1 = Fish(rawValue: "Cod")!
let f2 = Fish(rawValue: "cod")!

print("\(f1 == f2)") // always true
print("\(Set([f1]) == Set([f2]))") // sometimes true

If conformance to RawRepresentable is removed, then the Set example above is always true.

This appears to be a Swift 5 regression as Swift 4.1/2 does not exhibit this defect.

@belkadan
Copy link
Contributor

Hm, uh oh. RawRepresentable provides default implementations for hashing, but I guess that causes problems when there are two hashing entry points. @lorentey?

@lorentey
Copy link
Member

Ah, hm, that's not great. It's because `RawRepresentable` provides a definition of `_rawHashValue(seed🙂` that assumes the hash encoding is the raw value.

We can't remove that definition now but we can replace it with the default implementation that calls `hash(into🙂`.

@lorentey
Copy link
Member

#25873

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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 standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

3 participants