Skip to content

Commit 6100d64

Browse files
authored
Fix FilePath hashing in QueryEngine (#7926)
This vendors fixes to `HashEncoder` from `swift-sdk-generator` introduced in swiftlang/swift-sdk-generator#97 and swiftlang/swift-sdk-generator#134.
1 parent fbca3e6 commit 6100d64

File tree

4 files changed

+111
-73
lines changed

4 files changed

+111
-73
lines changed

Sources/QueryEngine/CacheKey.swift

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift open source project
44
//
5-
// Copyright (c) 2023 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2023-2024 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -16,17 +16,21 @@ import struct SystemPackage.FilePath
1616

1717
/// Indicates that values of a conforming type can be hashed with an arbitrary hashing function. Unlike `Hashable`,
1818
/// this protocol doesn't utilize random seed values and produces consistent hash values across process launches.
19-
package protocol CacheKey: Encodable {
19+
public protocol CacheKey: Encodable {}
20+
21+
/// Types that cannot be decomposed more to be hashed
22+
protocol LeafCacheKey: CacheKey {
23+
func hash(with hashFunction: inout some HashFunction)
2024
}
2125

22-
extension Bool: CacheKey {
26+
extension Bool: LeafCacheKey {
2327
func hash(with hashFunction: inout some HashFunction) {
2428
String(reflecting: Self.self).hash(with: &hashFunction)
2529
hashFunction.update(data: self ? [1] : [0])
2630
}
2731
}
2832

29-
extension Int: CacheKey {
33+
extension Int: LeafCacheKey {
3034
func hash(with hashFunction: inout some HashFunction) {
3135
String(reflecting: Self.self).hash(with: &hashFunction)
3236
withUnsafeBytes(of: self) {
@@ -35,7 +39,7 @@ extension Int: CacheKey {
3539
}
3640
}
3741

38-
extension Int8: CacheKey {
42+
extension Int8: LeafCacheKey {
3943
func hash(with hashFunction: inout some HashFunction) {
4044
String(reflecting: Self.self).hash(with: &hashFunction)
4145
withUnsafeBytes(of: self) {
@@ -44,7 +48,7 @@ extension Int8: CacheKey {
4448
}
4549
}
4650

47-
extension Int16: CacheKey {
51+
extension Int16: LeafCacheKey {
4852
func hash(with hashFunction: inout some HashFunction) {
4953
String(reflecting: Self.self).hash(with: &hashFunction)
5054
withUnsafeBytes(of: self) {
@@ -53,7 +57,7 @@ extension Int16: CacheKey {
5357
}
5458
}
5559

56-
extension Int32: CacheKey {
60+
extension Int32: LeafCacheKey {
5761
func hash(with hashFunction: inout some HashFunction) {
5862
String(reflecting: Self.self).hash(with: &hashFunction)
5963
withUnsafeBytes(of: self) {
@@ -62,7 +66,7 @@ extension Int32: CacheKey {
6266
}
6367
}
6468

65-
extension Int64: CacheKey {
69+
extension Int64: LeafCacheKey {
6670
func hash(with hashFunction: inout some HashFunction) {
6771
String(reflecting: Self.self).hash(with: &hashFunction)
6872
withUnsafeBytes(of: self) {
@@ -71,7 +75,7 @@ extension Int64: CacheKey {
7175
}
7276
}
7377

74-
extension UInt: CacheKey {
78+
extension UInt: LeafCacheKey {
7579
func hash(with hashFunction: inout some HashFunction) {
7680
String(reflecting: Self.self).hash(with: &hashFunction)
7781
withUnsafeBytes(of: self) {
@@ -80,7 +84,7 @@ extension UInt: CacheKey {
8084
}
8185
}
8286

83-
extension UInt8: CacheKey {
87+
extension UInt8: LeafCacheKey {
8488
func hash(with hashFunction: inout some HashFunction) {
8589
String(reflecting: Self.self).hash(with: &hashFunction)
8690
withUnsafeBytes(of: self) {
@@ -89,7 +93,7 @@ extension UInt8: CacheKey {
8993
}
9094
}
9195

92-
extension UInt16: CacheKey {
96+
extension UInt16: LeafCacheKey {
9397
func hash(with hashFunction: inout some HashFunction) {
9498
String(reflecting: Self.self).hash(with: &hashFunction)
9599
withUnsafeBytes(of: self) {
@@ -98,7 +102,7 @@ extension UInt16: CacheKey {
98102
}
99103
}
100104

101-
extension UInt32: CacheKey {
105+
extension UInt32: LeafCacheKey {
102106
func hash(with hashFunction: inout some HashFunction) {
103107
String(reflecting: Self.self).hash(with: &hashFunction)
104108
withUnsafeBytes(of: self) {
@@ -107,7 +111,7 @@ extension UInt32: CacheKey {
107111
}
108112
}
109113

110-
extension UInt64: CacheKey {
114+
extension UInt64: LeafCacheKey {
111115
func hash(with hashFunction: inout some HashFunction) {
112116
String(reflecting: Self.self).hash(with: &hashFunction)
113117
withUnsafeBytes(of: self) {
@@ -116,7 +120,7 @@ extension UInt64: CacheKey {
116120
}
117121
}
118122

119-
extension Float: CacheKey {
123+
extension Float: LeafCacheKey {
120124
func hash(with hashFunction: inout some HashFunction) {
121125
String(reflecting: Self.self).hash(with: &hashFunction)
122126
withUnsafeBytes(of: self) {
@@ -125,7 +129,7 @@ extension Float: CacheKey {
125129
}
126130
}
127131

128-
extension Double: CacheKey {
132+
extension Double: LeafCacheKey {
129133
func hash(with hashFunction: inout some HashFunction) {
130134
String(reflecting: Self.self).hash(with: &hashFunction)
131135
withUnsafeBytes(of: self) {
@@ -134,7 +138,7 @@ extension Double: CacheKey {
134138
}
135139
}
136140

137-
extension String: CacheKey {
141+
extension String: LeafCacheKey {
138142
func hash(with hashFunction: inout some HashFunction) {
139143
var t = String(reflecting: Self.self)
140144
t.withUTF8 {
@@ -147,21 +151,21 @@ extension String: CacheKey {
147151
}
148152
}
149153

150-
extension FilePath: CacheKey {
154+
extension FilePath: LeafCacheKey {
151155
func hash(with hashFunction: inout some HashFunction) {
152156
String(reflecting: Self.self).hash(with: &hashFunction)
153157
self.string.hash(with: &hashFunction)
154158
}
155159
}
156160

157-
extension FilePath.Component: CacheKey {
161+
extension FilePath.Component: LeafCacheKey {
158162
func hash(with hashFunction: inout some HashFunction) {
159163
String(reflecting: Self.self).hash(with: &hashFunction)
160164
self.string.hash(with: &hashFunction)
161165
}
162166
}
163167

164-
extension URL: CacheKey {
168+
extension URL: LeafCacheKey {
165169
func hash(with hashFunction: inout some HashFunction) {
166170
String(reflecting: Self.self).hash(with: &hashFunction)
167171
self.description.hash(with: &hashFunction)

0 commit comments

Comments
 (0)