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-13586] Bad diagnostic when we only have subscript(dynamicMember: WritableKeyPath) and the property is not settable #56021

Open
swift-ci opened this issue Sep 23, 2020 · 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 type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-13586
Radar rdar://problem/69440432
Original Reporter eimantas (JIRA User)
Type Bug

Attachment: Download

Environment

macOS 10.15.6
Xcode 12.0 GM (because AppStore version crashes when opening developer documentation)

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

md5: f05aa3b580e68340b6148c9f92d92c4e

Issue Description:

1) define a struct (say `Core`) with a read-only property;

struct Core {
    var someVar: String { "someVar" }
}

2) define another struct (say `Wrapper`) that has `var core: Core`;

struct Wrapper {
    var core: Core
}

3) make `Wrapper` use `dynamicMemberLookup` and define subscript with writeable key path;

@dynamicMemberLookup
struct Wrapper {

    var core: Core

    subscript(dynamicMember keyPath: WritableKeyPath<Core, String>) -> String {
        get { core[keyPath: keyPath] }
        set { core[keyPath: keyPath] = newValue }
    }
}

4) instantiate wrapper and try checking for the `core` property using dynamic member lookup

let wrapper = Wrapper(core: Core())

if wrapper.someVar.count < 3 {
    print("var is short")
}

The compiler, on `if` line , says that it can't write to `someVar` because the property is `get-only`.

Even trying to extract a value throws the same error on assignment statement:

let count = wrapper.someVar.count

if count < 3 {
    print("vars are short")
}
@typesanitizer
Copy link

@swift-ci create

@theblixguy
Copy link
Collaborator

You're using a WriteableKeyPath to access a read-only computed property, which is not allowed. It's not your fault! The diagnostic we generated isn't very good because it doesn't make it clear that it isn't allowed. If you change the key path type from WriteableKeyPath to KeyPath then the code will compile without an error.

It looks like we have a TODO to fix it in the codebase, I'll take a look at it shortly!

@beccadax
Copy link
Contributor

beccadax commented Oct 2, 2020

I concur—the behavior is correct but the diagnostic isn't explaining it well. I'll retitle the bug report to reflect this.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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

4 participants