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-13514] @dynamicMemberLookup does not satisfy protocol requirements #55950

Open
davedelong opened this issue Sep 6, 2020 · 5 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

@davedelong
Copy link

Previous ID SR-13514
Radar rdar://problem/68430975
Original Reporter @davedelong
Type Bug
Environment

Xcode 11.7 (11E801a) on macOS 10.15.6 (19G2021)

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

md5: 4efb4cc2f38a497fd03f6aeefcc6aeb2

cloned to:

Issue Description:

Given this:

protocol ModelValue {
    var formattedName: String { get }
}


struct RawModel {
    var formattedName: String
}


@dynamicMemberLookup
class RawModelController {
    let value: RawModel
    init(value: RawModel) { self.value = value }
    subscript<V>(dynamicMember keyPath: KeyPath<RawModel, V>) -> V { value[keyPath: keyPath] }
}

// this should work because @dynamicMemberLookup can fulfill all the protocol requirements
// instead, the compiler produces an error that the class does not conform to the protocol,
// because it's missing the required "formattedName" property.
extension RawModelController: ModelValue { }

I would expect that I could do extension RawModelController: ModelValue {} and have it "just work", because the @dynamicMemberLookup subscript will satisfy all of the requirements of the protocol.

Instead, the compiler produces an error.

@typesanitizer
Copy link

That's strange. Seems to compile just fine for Swift 5.1/5.2/nightly in https://godbolt.org/z/oneGW9

Have you been able to reproduce this with another toolchain?

@theblixguy
Copy link
Collaborator

Dave is talking about satisfying protocol requirements with @dynamicMemberLookup because if you add the above extension then the code doesn’t compile, but Dave argues it should.

// This is currently an error because the 'formattedName' property
// is not implemented, but arguably it should be discovered and
// satisfied by @dynamicMemberLookup because the conforming
// type provides a subscript(dynamicMember:) that provides access
// to a 'formattedName' property (from RawModel).
extension RawModelController: ModelValue {}

It might be a good idea to put the extension code into the snippet itself to avoid any confusion!

@davedelong
Copy link
Author

Good point; I added it to the snippet.

@typesanitizer
Copy link

Sorry, I misunderstood the issue. That makes sense.

@swift-ci create

@theblixguy
Copy link
Collaborator

Note that the witness matching manifesto refers to this functionality as a "maybe/unlikely" addition, but can be bumped up to "likely" based on what the community and core team thinks.

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

No branches or pull requests

3 participants