Skip to content
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

[SR-11351] KeyPaths to @objc properties passed to @dynamicMemberLookup subscript lose KVO utility #53752

Closed
swift-ci opened this issue Aug 22, 2019 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-11351
Radar rdar://problem/54602235
Original Reporter johnestropia (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Swift 5.1 (Xcode 11 beta 6)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee johnestropia (JIRA)
Priority Medium

md5: d5fe1cc7bb00dd6f51b799cd1652db91

Issue Description:

Normally KeyPath s for @objc properties can be used in utilities like

class MyClass: NSManagedObject {
    @NSManaged var stringValue: String?
}

let keyPath = \MyClass.stringValue

print(NSExpression(forKeyPath: keyPath).keyPath) // "stringValue"
print(keyPath._kvcKeyPathString) // "stringValue"
myObject.observe(keyPath) { object, change in
    // ...
}

However, when using @dynamicMemberLookup accessing this property loses their KVO utility:

@dynamicMemberLookup Expression<O: NSManagedObject> {

    subscript<V>(dynamicMember member: KeyPath<O, V>) -> String {
        return NSExpression(forKeyPath: member).keyPath // crash here
        // return member._kvcKeyPathString // also crashes
    }
}
let expression = Expression<MyClass>()
print(expression.stringValue) // crash triggered here
@belkadan
Copy link
Contributor

@jckarter, any hypotheses?

@theblixguy
Copy link
Collaborator

It's hitting a fatal error here: https://github.com/apple/swift/blob/master/stdlib/public/Darwin/Foundation/NSObject.swift#L153

Fatal error: Could not extract a String from KeyPath Swift.ReferenceWritableKeyPath<test.MyClass, Swift.Optional<Swift.String>>

@jckarter
Copy link
Member

@swift-ci create

@swift-ci
Copy link
Collaborator Author

Comment by John Estropia (JIRA)

Still an issue with Xcode 11/Swift 5.1 release build

@jckarter
Copy link
Member

This looks like it should've been fixed by #33486 Does it still happen in newer compilers?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

4 participants