Skip to content

Commit 4c2e16f

Browse files
Fix noCacheKeyConformance error on make-linux-sdk (#97)
Follow-up to 3e076d1 `SystemString` used inside `FilePath` is not a `CacheKey`, so the encoder complains about it, but we can consider `FilePath` as a leaf type and no need to encode it further.
1 parent 152a6fe commit 4c2e16f

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

Sources/GeneratorEngine/Cache/CacheKeyProtocol.swift

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ import struct SystemPackage.FilePath
1919
public protocol CacheKey: Encodable {
2020
}
2121

22-
extension Bool: CacheKey {
22+
/// Types that cannot be decomposed more to be hashed
23+
protocol LeafCacheKey: CacheKey {
24+
func hash(with hashFunction: inout some HashFunction)
25+
}
26+
27+
extension Bool: LeafCacheKey {
2328
func hash(with hashFunction: inout some HashFunction) {
2429
String(reflecting: Self.self).hash(with: &hashFunction)
2530
hashFunction.update(data: self ? [1] : [0])
2631
}
2732
}
2833

29-
extension Int: CacheKey {
34+
extension Int: LeafCacheKey {
3035
func hash(with hashFunction: inout some HashFunction) {
3136
String(reflecting: Self.self).hash(with: &hashFunction)
3237
withUnsafeBytes(of: self) {
@@ -35,7 +40,7 @@ extension Int: CacheKey {
3540
}
3641
}
3742

38-
extension Int8: CacheKey {
43+
extension Int8: LeafCacheKey {
3944
func hash(with hashFunction: inout some HashFunction) {
4045
String(reflecting: Self.self).hash(with: &hashFunction)
4146
withUnsafeBytes(of: self) {
@@ -44,7 +49,7 @@ extension Int8: CacheKey {
4449
}
4550
}
4651

47-
extension Int16: CacheKey {
52+
extension Int16: LeafCacheKey {
4853
func hash(with hashFunction: inout some HashFunction) {
4954
String(reflecting: Self.self).hash(with: &hashFunction)
5055
withUnsafeBytes(of: self) {
@@ -53,7 +58,7 @@ extension Int16: CacheKey {
5358
}
5459
}
5560

56-
extension Int32: CacheKey {
61+
extension Int32: LeafCacheKey {
5762
func hash(with hashFunction: inout some HashFunction) {
5863
String(reflecting: Self.self).hash(with: &hashFunction)
5964
withUnsafeBytes(of: self) {
@@ -62,7 +67,7 @@ extension Int32: CacheKey {
6267
}
6368
}
6469

65-
extension Int64: CacheKey {
70+
extension Int64: LeafCacheKey {
6671
func hash(with hashFunction: inout some HashFunction) {
6772
String(reflecting: Self.self).hash(with: &hashFunction)
6873
withUnsafeBytes(of: self) {
@@ -71,7 +76,7 @@ extension Int64: CacheKey {
7176
}
7277
}
7378

74-
extension UInt: CacheKey {
79+
extension UInt: LeafCacheKey {
7580
func hash(with hashFunction: inout some HashFunction) {
7681
String(reflecting: Self.self).hash(with: &hashFunction)
7782
withUnsafeBytes(of: self) {
@@ -80,7 +85,7 @@ extension UInt: CacheKey {
8085
}
8186
}
8287

83-
extension UInt8: CacheKey {
88+
extension UInt8: LeafCacheKey {
8489
func hash(with hashFunction: inout some HashFunction) {
8590
String(reflecting: Self.self).hash(with: &hashFunction)
8691
withUnsafeBytes(of: self) {
@@ -89,7 +94,7 @@ extension UInt8: CacheKey {
8994
}
9095
}
9196

92-
extension UInt16: CacheKey {
97+
extension UInt16: LeafCacheKey {
9398
func hash(with hashFunction: inout some HashFunction) {
9499
String(reflecting: Self.self).hash(with: &hashFunction)
95100
withUnsafeBytes(of: self) {
@@ -98,7 +103,7 @@ extension UInt16: CacheKey {
98103
}
99104
}
100105

101-
extension UInt32: CacheKey {
106+
extension UInt32: LeafCacheKey {
102107
func hash(with hashFunction: inout some HashFunction) {
103108
String(reflecting: Self.self).hash(with: &hashFunction)
104109
withUnsafeBytes(of: self) {
@@ -107,7 +112,7 @@ extension UInt32: CacheKey {
107112
}
108113
}
109114

110-
extension UInt64: CacheKey {
115+
extension UInt64: LeafCacheKey {
111116
func hash(with hashFunction: inout some HashFunction) {
112117
String(reflecting: Self.self).hash(with: &hashFunction)
113118
withUnsafeBytes(of: self) {
@@ -116,7 +121,7 @@ extension UInt64: CacheKey {
116121
}
117122
}
118123

119-
extension Float: CacheKey {
124+
extension Float: LeafCacheKey {
120125
func hash(with hashFunction: inout some HashFunction) {
121126
String(reflecting: Self.self).hash(with: &hashFunction)
122127
withUnsafeBytes(of: self) {
@@ -125,7 +130,7 @@ extension Float: CacheKey {
125130
}
126131
}
127132

128-
extension Double: CacheKey {
133+
extension Double: LeafCacheKey {
129134
func hash(with hashFunction: inout some HashFunction) {
130135
String(reflecting: Self.self).hash(with: &hashFunction)
131136
withUnsafeBytes(of: self) {
@@ -134,7 +139,7 @@ extension Double: CacheKey {
134139
}
135140
}
136141

137-
extension String: CacheKey {
142+
extension String: LeafCacheKey {
138143
func hash(with hashFunction: inout some HashFunction) {
139144
var t = String(reflecting: Self.self)
140145
t.withUTF8 {
@@ -147,21 +152,21 @@ extension String: CacheKey {
147152
}
148153
}
149154

150-
extension FilePath: CacheKey {
155+
extension FilePath: LeafCacheKey {
151156
func hash(with hashFunction: inout some HashFunction) {
152157
String(reflecting: Self.self).hash(with: &hashFunction)
153158
self.string.hash(with: &hashFunction)
154159
}
155160
}
156161

157-
extension FilePath.Component: CacheKey {
162+
extension FilePath.Component: LeafCacheKey {
158163
func hash(with hashFunction: inout some HashFunction) {
159164
String(reflecting: Self.self).hash(with: &hashFunction)
160165
self.string.hash(with: &hashFunction)
161166
}
162167
}
163168

164-
extension URL: CacheKey {
169+
extension URL: LeafCacheKey {
165170
func hash(with hashFunction: inout some HashFunction) {
166171
String(reflecting: Self.self).hash(with: &hashFunction)
167172
self.description.hash(with: &hashFunction)

Sources/GeneratorEngine/Query.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ extension HashEncoder {
229229
}
230230

231231
mutating func encode<T>(_ value: T, forKey key: K) throws where T : Encodable {
232+
if let leaf = value as? LeafCacheKey {
233+
leaf.hash(with: &self.encoder.hashFunction)
234+
return
235+
}
232236
guard value is CacheKey else {
233237
throw Error.noCacheKeyConformance(T.self)
234238
}

0 commit comments

Comments
 (0)