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-11385] Automated conformance can be hijacked by extension protocol conformances that import matching members #53786

Open
swift-ci opened this issue Aug 28, 2019 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself feature A feature request or implementation

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-11385
Radar None
Original Reporter CTMacUser (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, LanguageFeatureRequest
Assignee None
Priority Medium

md5: b79cf4dfcae039d527156fa5f643306d

relates to:

  • SR-9425 OpenClose enum comparison uses String compare

Issue Description:

Let the standard library protocols with automatic conformance (SLPwAC) be defined as those matching protocols from the Standard Library (that's Equatable, Hashable, Encodable, and Decodable as of this writing)

For the SLPwAC, said automation is implicit. You just have to declare the conformance and not bother adding any corresponding members. But if a different protocol has a members defined in an extension that match the signature of members from the SLPwAC, then automatically-defined implicit member definitions are cancelled and the extension's code is used, even if it customizes the member in a way the user doesn't want.

protocol Sucker {}

#if false
extension Sucker {
    static func == (lhs: Self, rhs: Self) -> Bool {
        print("Sucker")
        return true
    }
}
#endif

struct MyType: Equatable {}
extension MyType: Sucker {}

MyType() == MyType()

(It doesn't matter if MyType adds its Equatable conformance in the primary definition or in an extension block. It doesn't matter if Sucker refines Equatable or not.)

If you change the compilation test to true, then "Sucker" will be printed.

I suggest that we add an explicit declaration in nominal types that posts the desire to use the compiler-generated versions of SLPwAC members, with the same precedence as an explicitly written member in the target type (which out-prioritizes any imported definitions from protocols).

// Example:
struct MyType: Equatable {
    //...
    publish default Equatable
}

We could depreciate the current implicit behavior and maybe end it later.

@dan-zheng
Copy link
Collaborator

This issue was created in the Swift for TensorFlow [TF] project, but I believe it belongs in the general Swift project [SR]. Moved.

@belkadan
Copy link
Contributor

The current behavior is definitely the design provided by the proposals, but it's caused other problems too like SR-9425. Regardless, any change would have to go through the Swift Evolution Process.

@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 feature A feature request or implementation
Projects
None yet
Development

No branches or pull requests

3 participants