Skip to content

Commit 74a0935

Browse files
authored
Merge pull request #85071 from Azoy/fix-keypath-append
[stdlib] Add the intermediate type size for every leaf component
2 parents dfd16a0 + 714fd26 commit 74a0935

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

stdlib/public/core/KeyPath.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2925,6 +2925,10 @@ internal func calculateAppendedKeyPathSize(
29252925

29262926
if isLast {
29272927
break
2928+
} else {
2929+
// Add the intermediate type.
2930+
result = MemoryLayout<Int>._roundingUpToAlignment(result)
2931+
result &+= MemoryLayout<Int>.size
29282932
}
29292933
}
29302934

test/stdlib/KeyPath.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,5 +1271,26 @@ if #available(SwiftStdlib 6.3, *) {
12711271
}
12721272
}
12731273

1274-
runAllTests()
1274+
struct Thingy {
1275+
var thingy: Thingy { self }
1276+
1277+
var a = 42
1278+
}
1279+
1280+
extension Int {
1281+
var subscripty: Thingy {
1282+
Thingy(a: 67)
1283+
}
1284+
}
1285+
1286+
keyPath.test("appending keypath with multiple components") {
1287+
var kp: KeyPath = \Thingy.self
1288+
kp = kp.appending(path: \.thingy)
1289+
kp = kp.appending(path: \.a.subscripty)
12751290

1291+
let t = Thingy()
1292+
let value = t[keyPath: kp]
1293+
expectEqual(value.a, 67)
1294+
}
1295+
1296+
runAllTests()

0 commit comments

Comments
 (0)