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-8077] Compile error when using @dynamicMemberLookup as a protocol requirement #50610

Closed
ole opened this issue Jun 22, 2018 · 6 comments
Closed
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@ole
Copy link
Contributor

ole commented Jun 22, 2018

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

Tested in:

  • Xcode 10b2, Apple Swift version 4.2 (swiftlang-1000.0.16.9 clang-1000.10.25.3)

  • and with the Swift-Development-Snapshot-2018-06-20-a toolchain.

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @dan-zheng
Priority Medium

md5: 5a6078ecbd3a511f4ebcc3bee56fffa2

Issue Description:

See the associated Swift forums discussion.

Using @dynamicMemberLookup as a protocol requirement without a default implementation for the subscript produces a compile error:

// error: @dynamicMemberLookup attribute requires 'P'
// to have a 'subscript(dynamicMember:)' member with
// a string index
@dynamicMemberLookup
protocol P {
    subscript(dynamicMember member: String) -> String { get }
}

Adding a default implementation for the subscript compiles and works correctly:

@dynamicMemberLookup
protocol P {
    subscript(dynamicMember member: String) -> String { get }
}

extension P {
    subscript(dynamicMember member: String) -> String {
        return ""
    }
}
@dan-zheng
Copy link
Collaborator

This is because subscript(dynamicMember🙂 lookup does not search through protocol requirements, only concrete methods.

This is a bug, not a feature. The @dynamicCallable implementation fixed this bug but the logic needs to be backported to @dynamicMemberLookup. I'll handle this before the Swift 5 release cutoff.

@dan-zheng
Copy link
Collaborator

Fixed in #20516
subscript(dynamicMember🙂 methods now work as @dynamicMemberLookup protocol requirements.

@ole
Copy link
Contributor Author

ole commented Nov 14, 2018

@dan-zheng Awesome, thanks for working on this!

@dan-zheng
Copy link
Collaborator

My pleasure!
Now, we can implement "true" @dynamicMemberLookup protocols.

@swift-ci
Copy link
Collaborator

Comment by Frank Valbuena (JIRA)

The fix worked however when trying to use it on a conforming type the autocompletion of the properties is broken. Tested on Xcode 11 beta 5.

@swift-ci
Copy link
Collaborator

Comment by Frank Valbuena (JIRA)

The previous comment is for SE-0252 Swift 5.1

@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
Projects
None yet
Development

No branches or pull requests

3 participants