Skip to content

[SR-7497] [stdlib] Fix KeyPaths hashing #17467

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

Merged
merged 3 commits into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stdlib/public/core/KeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class AnyKeyPath: Hashable, _AppendKeyPath {
/// of this instance.
@_effects(releasenone)
final public func hash(into hasher: inout Hasher) {
ObjectIdentifier(type(of: self)).hash(into: &hasher)
return withBuffer {
var buffer = $0
while true {
Expand Down Expand Up @@ -597,7 +598,6 @@ internal enum KeyPathComponent: Hashable {

@_effects(releasenone)
internal func hash(into hasher: inout Hasher) {
var hasher = hasher
func appendHashFromArgument(
_ argument: KeyPathComponent.ArgumentRef?
) {
Expand Down
21 changes: 21 additions & 0 deletions test/stdlib/KeyPathImplementation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,10 @@ keyPathImpl.test("equality") {
}

expectNotEqual(s_c_z_p_x, s_c_z_p_y)
expectNotEqual(s_c_z_p_x.hashValue, s_c_z_p_y.hashValue)

expectNotEqual(s_c_z_p_y, s_c_z_p_x)
expectNotEqual(s_c_z_p_y.hashValue, s_c_z_p_x.hashValue)

// Different path type
let s_c_z_p = ReferenceWritableKeyPath<S<S<String>>, Point>
Expand All @@ -677,6 +680,24 @@ keyPathImpl.test("equality") {
expectNotEqual(s_c_z_p_x, s_c_z_p)
expectNotEqual(s_c_z_p, s_c_z_p_x)

let s_x = WritableKeyPath<S<String>, Int>
.build(capacityInBytes: MemoryLayout<Int>.size + 4) {
$0.addHeader(trivial: true, hasReferencePrefix: false)
$0.addStructComponent(offset: S<String>.x_offset)
}

let si_x = WritableKeyPath<S<Int>, Int>
.build(capacityInBytes: MemoryLayout<Int>.size + 4) {
$0.addHeader(trivial: true, hasReferencePrefix: false)
$0.addStructComponent(offset: S<Int>.x_offset)
}

expectNotEqual(s_x, si_x)
expectNotEqual(s_x.hashValue, si_x.hashValue)

expectNotEqual(si_x, s_x)
expectNotEqual(si_x.hashValue, s_x.hashValue)

// Same path, no reference prefix
let s_c_z_p_x_readonly = KeyPath<S<S<String>>, Double>
.build(capacityInBytes: 7 * MemoryLayout<Int>.size + 4) {
Expand Down