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-10202] Trying to write through key path on private(set) var gives bad error and fixit #52602

Closed
ZevEisenberg opened this issue Mar 27, 2019 · 3 comments
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 good first issue Good for newcomers key paths Feature: key paths (both native and Objective-C) type checker Area → compiler: Semantic analysis

Comments

@ZevEisenberg
Copy link
Contributor

Previous ID SR-10202
Radar None
Original Reporter @ZevEisenberg
Type Bug
Status Resolved
Resolution Done
Environment

Xcode 10.2 (10E125) on macOS 10.14.3 (18D109) in a macOS playground

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, KeyPaths, StarterBug, TypeChecker
Assignee @ZevEisenberg
Priority Medium

md5: 3b0684655bd95e8c0ba4b082046ebf2a

Issue Description:

struct Person {
    private(set) var name: String
    init(name: String) {
        self.name = name
    }
}
let keyPath = \Person.name
var person = Person(name: "oldName")
person[keyPath: keyPath] = "newName"

This produces the following output when compiled:

error: My Page.xcplaygroundpage:22:7: error: cannot assign through subscript: 'keyPath' is a read-only key path
person[keyPath: keyPath] = "newName" // Cannot assign through subscript: 'keyPath' is a read-only key path
      ^         ~~~~~~~


My Page.xcplaygroundpage:20:1: note: change 'let' to 'var' to make it mutable
let keyPath = \Person.name
^~~
var 

The first error is good: it's true that you can't assign through a read-only key path.

However, the fixit is not useful at all. Changing `let` to `var` doesn't actually fix anything.

@belkadan
Copy link
Contributor

@xedin, @jckarter, did we already have one of these?

@gregomni
Copy link
Collaborator

Still see this in Xcode 11 beta 3, but appears fixed now in master.

@theblixguy
Copy link
Collaborator

Yeah, slazarus (JIRA User) fixed it here: #24450

@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 good first issue Good for newcomers key paths Feature: key paths (both native and Objective-C) type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants