Skip to content

Commit 33a22a0

Browse files
dmcyklorentey
authored andcommitted
[stdlib] Cover type difference in key path hash.
Include type metadata pointer in hashing. (cherry picked from commit 731b9b0)
1 parent 1aa78ba commit 33a22a0

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

stdlib/public/core/KeyPath.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class AnyKeyPath: Hashable, _AppendKeyPath {
5959
/// of this instance.
6060
@inlinable // FIXME(sil-serialize-all)
6161
final public func hash(into hasher: inout Hasher) {
62+
hasher.combine(unsafeBitCast(type(of: self) as Any.Type, to: Int.self))
6263
return withBuffer {
6364
var buffer = $0
6465
while true {

test/stdlib/KeyPathImplementation.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,24 @@ keyPathImpl.test("equality") {
680680
expectNotEqual(s_c_z_p_x, s_c_z_p)
681681
expectNotEqual(s_c_z_p, s_c_z_p_x)
682682

683+
let s_x = WritableKeyPath<S<String>, Int>
684+
.build(capacityInBytes: MemoryLayout<Int>.size + 4) {
685+
$0.addHeader(trivial: true, hasReferencePrefix: false)
686+
$0.addStructComponent(offset: S<String>.x_offset)
687+
}
688+
689+
let si_x = WritableKeyPath<S<Int>, Int>
690+
.build(capacityInBytes: MemoryLayout<Int>.size + 4) {
691+
$0.addHeader(trivial: true, hasReferencePrefix: false)
692+
$0.addStructComponent(offset: S<Int>.x_offset)
693+
}
694+
695+
expectNotEqual(s_x, si_x)
696+
expectNotEqual(s_x.hashValue, si_x.hashValue)
697+
698+
expectNotEqual(si_x, s_x)
699+
expectNotEqual(si_x.hashValue, s_x.hashValue)
700+
683701
// Same path, no reference prefix
684702
let s_c_z_p_x_readonly = KeyPath<S<S<String>>, Double>
685703
.build(capacityInBytes: 7 * MemoryLayout<Int>.size + 4) {

0 commit comments

Comments
 (0)