Skip to content

Commit 7af6776

Browse files
committed
[test] PersistentVector: use a custom hashing interface
This prototype is not fully implemented, and it relies on specific hash values to not trigger unhandled cases. To keep its test working, define and use a custom hashing interface that emulates hashValue behavior prior to SE-0206.
1 parent 01baa5a commit 7af6776

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

validation-test/stdlib/Prototypes/PersistentVector.swift.gyb

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ Memory layout:
100100
import Swift
101101
import SwiftShims
102102

103+
// This prototype is only partially implemented, and it relies on specific hash
104+
// values. To keep it working, define an alternative hashing interface emulating
105+
// pre-SE-0206 Hashable.
106+
protocol LegacyHashable: Equatable {
107+
var legacyHashValue: Int { get }
108+
}
109+
103110
//
104111
// Standard library extras
105112
//
@@ -252,7 +259,7 @@ struct _PVSparseVectorNodeLayoutParameters {
252259
var keyCount: Int
253260
}
254261

255-
struct _PVSparseVectorNodePointer<Key : Hashable, Value>
262+
struct _PVSparseVectorNodePointer<Key : LegacyHashable, Value>
256263
: CustomReflectable {
257264
typealias _Self = _PVSparseVectorNodePointer
258265

@@ -917,7 +924,7 @@ struct _PVSparseVectorNodePointer<Key : Hashable, Value>
917924
}
918925
}
919926

920-
struct _PVArrayNodePointer<Key : Hashable, Value>
927+
struct _PVArrayNodePointer<Key : LegacyHashable, Value>
921928
: CustomReflectable {
922929

923930
typealias _Self = _PVArrayNodePointer
@@ -1158,7 +1165,7 @@ struct _PVCollisionNodePointerLayoutParameters {
11581165
var keyCount: Int
11591166
}
11601167

1161-
struct _PVCollisionNodePointer<Key : Hashable, Value>
1168+
struct _PVCollisionNodePointer<Key : LegacyHashable, Value>
11621169
: CustomReflectable {
11631170

11641171
typealias _Self = _PVCollisionNodePointer
@@ -1461,7 +1468,7 @@ struct _PVCollisionNodePointer<Key : Hashable, Value>
14611468
}
14621469
}
14631470

1464-
struct _PVAnyNodePointer<Key : Hashable, Value>
1471+
struct _PVAnyNodePointer<Key : LegacyHashable, Value>
14651472
: CustomReflectable, Equatable {
14661473

14671474
let taggedPointer: UnsafeMutableRawPointer
@@ -1654,7 +1661,8 @@ func == <Key, Value> (
16541661
return lhs.taggedPointer == rhs.taggedPointer
16551662
}
16561663

1657-
final internal class _NativePVDictionaryStorageRef<Key : Hashable, Value> {
1664+
final internal
1665+
class _NativePVDictionaryStorageRef<Key : LegacyHashable, Value> {
16581666

16591667
var _rootNode: _PVAnyNodePointer<Key, Value>?
16601668
var _count: Int
@@ -1675,7 +1683,7 @@ final internal class _NativePVDictionaryStorageRef<Key : Hashable, Value> {
16751683
}
16761684
}
16771685

1678-
struct _NativePVDictionaryStorage<Key : Hashable, Value> {
1686+
struct _NativePVDictionaryStorage<Key : LegacyHashable, Value> {
16791687
var _storageRef: _NativePVDictionaryStorageRef<Key, Value>
16801688

16811689
var _rootNode: _PVAnyNodePointer<Key, Value>? {
@@ -1715,13 +1723,13 @@ struct _NativePVDictionaryStorage<Key : Hashable, Value> {
17151723

17161724
func assertingGet(key: Key) -> Value {
17171725
let valuePointer = _rootNode!.unsafeMaybeGet(
1718-
key: key, hashValue: key.hashValue, depth: 0)
1726+
key: key, hashValue: key.legacyHashValue, depth: 0)
17191727
return valuePointer!.pointee
17201728
}
17211729

17221730
func maybeGet(key: Key) -> Value? {
17231731
let valuePointer = _rootNode?.unsafeMaybeGet(
1724-
key: key, hashValue: key.hashValue, depth: 0)
1732+
key: key, hashValue: key.legacyHashValue, depth: 0)
17251733
return valuePointer.map { $0.pointee }
17261734
}
17271735

@@ -1748,7 +1756,7 @@ struct _NativePVDictionaryStorage<Key : Hashable, Value> {
17481756
}
17491757

17501758
mutating func updateValue(_ value: Value, forKey key: Key) -> Value? {
1751-
let hashValue = key.hashValue
1759+
let hashValue = key.legacyHashValue
17521760
guard let oldRootNode = _rootNode else {
17531761
let layout = _PVSparseVectorNodeLayoutParameters(
17541762
childNodeCount: 0,
@@ -1787,7 +1795,7 @@ struct _NativePVDictionaryStorage<Key : Hashable, Value> {
17871795
guard let oldRootNode = _rootNode else {
17881796
return nil
17891797
}
1790-
let hashValue = key.hashValue
1798+
let hashValue = key.legacyHashValue
17911799
let isUnique = isKnownUniquelyReferenced(&_storageRef)
17921800
let (oldValue, newRootNode) = oldRootNode.removeValue(
17931801
forKey: key,
@@ -1858,6 +1866,12 @@ Bitmap.test("setBitIndices") {
18581866
expectEqualSequence([ 0, 4, 5, 7, 31 ], Array(bm.setBitIndices))
18591867
}
18601868

1869+
extension MinimalHashableValue: LegacyHashable {
1870+
var legacyHashValue: Int {
1871+
return self.value % 10000
1872+
}
1873+
}
1874+
18611875
var PersistentVectorTests = TestSuite("PersistentVector")
18621876

18631877
PersistentVectorTests.test("sizeof") {
@@ -1993,10 +2007,6 @@ func testDictionary(
19932007
print("---------------------------------------")
19942008
}
19952009

1996-
MinimalHashableValue.hashValueImpl.value = {
1997-
return $0 % 10000
1998-
}
1999-
20002010
typealias MyDictionary = _NativePVDictionaryStorage<
20012011
MinimalHashableValue, OpaqueValue<Int>
20022012
>

0 commit comments

Comments
 (0)