Skip to content

[stdlib] Add SPI to reroot a keypath for a given superclass #65408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions stdlib/public/core/KeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3937,6 +3937,66 @@ public func _createOffsetBasedKeyPath(
}
}

@_spi(ObservableRerootKeyPath)
@available(SwiftStdlib 5.9, *)
public func _rerootKeyPath<NewRoot>(
_ existingKp: AnyKeyPath,
to newRoot: NewRoot.Type
) -> PartialKeyPath<NewRoot> {
let (isTrivial, hasReferencePrefix, componentSize) = existingKp.withBuffer {
($0.trivial, $0.hasReferencePrefix, $0.data.count)
}

let existingKpTy = type(of: existingKp)

func openedRoot<Root>(_: Root.Type) -> AnyKeyPath.Type {
func openedValue<Value>(_: Value.Type) -> AnyKeyPath.Type {
if existingKpTy == ReferenceWritableKeyPath<Root, Value>.self {
return ReferenceWritableKeyPath<NewRoot, Value>.self
} else if existingKpTy == KeyPath<Root, Value>.self {
return KeyPath<NewRoot, Value>.self
} else {
fatalError("Unsupported KeyPath type to be rerooted")
}
}

return _openExistential(existingKpTy.valueType, do: openedValue(_:))
}

let newKpTy = _openExistential(existingKpTy.rootType, do: openedRoot(_:))

return newKpTy._create(
// This is the buffer header + padding (if needed) + size of components
capacityInBytes: MemoryLayout<Int>.size + componentSize
) {
var builder = KeyPathBuffer.Builder($0)
let header = KeyPathBuffer.Header(
size: componentSize,
trivial: isTrivial,
hasReferencePrefix: hasReferencePrefix
)

builder.pushHeader(header)

existingKp.withBuffer {
var existingBuffer = $0

while true {
let (rawComponent, componentTy) = existingBuffer.next()

rawComponent.clone(
into: &builder.buffer,
endOfReferencePrefix: rawComponent.header.endOfReferencePrefix
)

if componentTy == nil {
break
}
}
}
} as! PartialKeyPath<NewRoot>
}

#if SWIFT_ENABLE_REFLECTION

@_silgen_name("swift_keyPath_copySymbolName")
Expand Down
43 changes: 43 additions & 0 deletions test/stdlib/KeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
// REQUIRES: executable_test
// UNSUPPORTED: freestanding

@_spi(ObservableRerootKeyPath)
import Swift

import StdlibUnittest

var keyPath = TestSuite("key paths")
Expand Down Expand Up @@ -1086,5 +1089,45 @@ if #available(SwiftStdlib 5.9, *) {
}
}

class RerootedSuper {
var x = "hello world"
}

class RerootedSub0: RerootedSuper {}
class RerootedSub1: RerootedSub0 {}

if #available(SwiftStdlib 5.9, *) {
keyPath.test("_rerootKeyPath") {
let x = \RerootedSub1.x

let superValue = RerootedSuper()
let sub0 = RerootedSub0()
let sub1 = RerootedSub1()

let sub0Kp = _rerootKeyPath(x, to: RerootedSub0.self)

expectTrue(type(of: sub0Kp) == ReferenceWritableKeyPath<RerootedSub0, String>.self)

let superKp = _rerootKeyPath(x, to: RerootedSuper.self)

expectTrue(type(of: superKp) == ReferenceWritableKeyPath<RerootedSuper, String>.self)

let x0 = sub1[keyPath: sub0Kp] as! String
expectEqual(x0, "hello world")

let x1 = sub1[keyPath: superKp] as! String
expectEqual(x1, "hello world")

let x2 = sub0[keyPath: sub0Kp] as! String
expectEqual(x2, "hello world")

let x3 = sub0[keyPath: superKp] as! String
expectEqual(x3, "hello world")

let x4 = superValue[keyPath: superKp] as! String
expectEqual(x4, "hello world")
}
}

runAllTests()