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-5115] KeyPath-based KVO: No diagnostic for inaccessible KVO KeyPaths #47691

Closed
zwaldowski opened this issue Jun 6, 2017 · 6 comments
Closed
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis

Comments

@zwaldowski
Copy link

Previous ID SR-5115
Radar rdar://problem/32593787
Original Reporter @zwaldowski
Type Bug
Status Resolved
Resolution Done
Environment

Xcode Version 9.0 beta (9M136h) / swiftlang 900.0.43

Additional Detail from JIRA
Votes 4
Component/s Compiler
Labels Bug, DiagnosticsQoI, TypeChecker
Assignee @theblixguy
Priority Medium

md5: 43fed09e52695de1a92ee6f5d4a8cbdd

is duplicated by:

  • SR-5159 Warn when using KVO on a non-dynamic @objc property
  • SR-5177 Observing keypath for non-objc type crashes with misleading error message

Issue Description:

Consider the following:

class MyObject: NSObject {
    var foo: Int = 0
    var bar: String = ""
    var baz: NSAttributedStringKey?
}

let x = MyObject()
x.observe(\.foo) { (x, y) in print(x, y) } // => fatal error: Could not extract a String from KeyPath Swift.ReferenceWritableKeyPath<__lldb_expr_55.MyObject, Swift.Int>
// => no execution after this point
x.observe(\.bar) { (x, y) in print(x, y) }
x.observe(\.baz) { (x, y) in print(x, y) }
x.foo = 5
x.bar = "blah"
x.baz = .link

I presume like in Swift 3, `@objc dynamic` needs to be added for real KVO support, but this is non-obvious and there's no diagnostic for it.

@jckarter
Copy link
Member

jckarter commented Jun 6, 2017

@swift-ci create

@belkadan
Copy link
Contributor

Thomas points out in SR-5177 that we could at least improve the error message.

@bobergj
Copy link

bobergj commented Oct 5, 2017

Still an issue in Xcode Version 9.0 (9A235) / Apple Swift version 4.0 (swiftlang-900.0.65 clang-900.0.37)

@swift-ci
Copy link
Collaborator

Comment by Amit Kumar Swami (JIRA)

Is the issue in below code also similar to above?

struct Foo {
    let bar: Int
    let baz: String
}

let sortDescriptor = NSSortDescriptor.init(keyPath: \Foo.bar, ascending: true)
// Could not extract a String from KeyPath Swift.WritableKeyPath<__lldb_expr_37.Foo, Swift.Int>: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-900.0.65/src/swift/stdlib/public/SDK/Foundation/NSObject.swift, line 85

@theblixguy
Copy link
Collaborator

PR: #28135 will now emit a warning if the property isn't @objc dynamic

@theblixguy
Copy link
Collaborator

Fixed on master. Pleas verify using the next available development snapshot!

@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 diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

6 participants