swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.58k stars 10.36k forks source link

[SR-15206] Inconsistent behaviour when overriding Equatable conformance #57528

Closed tachyonics closed 3 years ago

tachyonics commented 3 years ago
Previous ID SR-15206
Radar rdar://problem/83248771
Original Reporter @tachyonics
Type Bug
Status Resolved
Resolution Done
Environment Swift 5.4.3, Amazon Linux
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: 9450cc35a37b53d6fdaf489b7b262598

Issue Description:

We have just encountered an issue where there is inconsistent behaviour when overriding Equatable conformance across packages and using generic functions.

In Package 1 -

  1. Define a type with compiler-synthesised Equatable conformance.

In Package 2 -

  1. Create an extension that overrides the Equatable conformance that is different to the compiler-synthesised conformance.

  2. In a first test, test equality directly in a non-generic function

  3. In a second test, test equality in a generic function where the generic parameter extends Equatable.

We would expect the results of both equality tests to use the package-local conformance override. However we observed -

  1. the equality test in the non-generic function to use the override conformance. This is what we would expect.

  2. the equality test in the generic function to use the original compiler-synthesised conformance. This is not the result we would expect.

typesanitizer commented 3 years ago

@swift-ci create

CodaFi commented 3 years ago

This behaves correctly. There is no such thing as a local override of a protocol conformance - in fact, protocol conformances are required to be globally unique or you may run into runtime instabilities. What you are observing in the former case is name lookup preferring operators defined within the same module as the use. In the latter case, the lookup occurs against the requirements of Equatable, and so selects the operator that dispatches through the witness table for the conformance of the type to the protocol.