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-12041] KeyPath @dynamicMemberLookup does not compile using implicit self #54477

Open
swift-ci opened this issue Jan 15, 2020 · 4 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-12041
Radar rdar://problem/72801672
Original Reporter ebg (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 3
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: d83ce0bfd15401e94983d491a83ac1fb

Issue Description:

Forum thread: https://forums.swift.org/t/keypath-dynamicmemberlookup-does-not-compile-using-implicit-self/32622

You can't use implicit self when using the dynamicMember Keypath feature. The compiler should permit both `self.str1` and `str1`:

public protocol TestProtocol {
    var str1: String { get }
}

@dynamicMemberLookup
open class BaseClass<ProtocolType> {
    
    public let proto: ProtocolType
    
    init(proto: ProtocolType) {
        self.proto = proto
    }
    
    public subscript<T>(dynamicMember keyPath: KeyPath<ProtocolType, T>) -> T {
        return proto[keyPath: keyPath]
    }
}

public final class TestClass: BaseClass<TestProtocol> {
    func test() {
        print(self.proto.str1) // compiles
        print(proto.str1)      // compiles
        print(self.str1)       // compiles
        print(str1)            // ERROR: Use of unresolved identifier 'str1'
    }
}
@theblixguy
Copy link
Collaborator

cc @xedin

@swift-ci
Copy link
Collaborator Author

Comment by Erich Graham (JIRA)

anywhere you can point to in the compiler code to look for a fix?

@beccadax
Copy link
Contributor

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@robin-on-git
Copy link

This is something I use daily since years. For some reason I remember it working properly when I implemented it, then breaking at some point and now I have to use self. prefixes for all state accesses in my Views and ViewModels. I was hoping this would eventually be fixed and we could remove all the self references from our code as it was intended to be.

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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants