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

AnyObject key path diagnostic is broken #73521

Open
stephencelis opened this issue May 8, 2024 · 0 comments
Open

AnyObject key path diagnostic is broken #73521

stephencelis opened this issue May 8, 2024 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@stephencelis
Copy link
Contributor

Description

The following code has a compile error:

struct S {
  let object: AnyObject
  let keyPath: AnyKeyPath
  func f1() {
    object[keyPath: keyPath]  // 🛑
  }
}

🛑 The root type of a Swift key path cannot be 'AnyObject'

According to this very old comment AnyObject key paths are forbidden for Objective-C messaging reasons. I'm not sure that still applies, but I would expect an AnyObject to be able to apply an AnyKeyPath regardless.

Stranger still, the diagnostic goes away with the mere act of copying object over to a local, mutable variable:

func f2() {
  var object = object
  object[keyPath: keyPath]  // ✅
}

Regardless of the correct behavior, I believe the above should at least behave consistently.

See also: https://forums.swift.org/t/anyobject-and-key-paths/71671

Reproduction

struct S {
  let object: AnyObject
  let keyPath: AnyKeyPath
  func f1() {
    object[keyPath: keyPath]  // 🛑
  }
  func f2() {
    var object = object
    object[keyPath: keyPath]  // ✅
  }
}

Expected behavior

struct S {
  let object: AnyObject
  let keyPath: AnyKeyPath
  func f1() {
    object[keyPath: keyPath]  // ✅
  }
  func f2() {
    var object = object
    object[keyPath: keyPath]  // ✅
  }
}

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0

Additional information

No response

@stephencelis stephencelis added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels May 8, 2024
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. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant