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

Regression: Operators in protocol extensions doesn't compile in Swift 6.1 and Swift 6.2 snapshots when the parameters are generalized to take any instance satisfying the protocol and not just Self #78733

Open
TheHarcker opened this issue Jan 18, 2025 · 2 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself operators Feature: operators protocol Feature → type declarations: Protocol declarations regression Swift 6.2-dev type checker Area → compiler: Semantic analysis

Comments

@TheHarcker
Copy link
Contributor

Description

No response

Reproduction

public protocol EqualById {
    var id: String { get }
}

extension EqualById {
    public static func ==(lhs: EqualById, rhs: EqualById) -> Bool {
        return lhs.id == rhs.id
    }
}

Which can be reduced to:

public protocol EqualById {}
extension EqualById {
    public static func ==(lhs: EqualById, rhs: EqualById) -> Bool{
        true
    }
}

These examples compile successfully on Swift 6.0.3, but compilation fails on the swift-6.1-DEVELOPMENT-SNAPSHOT-2025-01-17-a.xctoolchain and swift-DEVELOPMENT-SNAPSHOT-2025-01-10-a.xctoolchain snapshots with the diagnostic:

 4 | 
 5 | extension EqualById {
 6 |     public static func ==(lhs: EqualById, rhs: EqualById) -> Bool{
   |                        `- error: member operator '==' of protocol 'EqualById' must have at least one argument of type 'Self'
 7 |         return lhs.id == rhs.id
 8 |     }

Expected behavior

I expected the code to compile on all toolchains as Self conforms to EqualById.

Environment

The code compiles with swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1) Target: arm64-apple-macosx15.0

The codes doesn't compile with Apple Swift version 6.1-dev (LLVM 38a3fc37c5f5e7a, Swift eedae18ae4df60a) Target: arm64-apple-macosx15.0 and Apple Swift version 6.2-dev (LLVM 81859ac55f8d09a, Swift 8ec8a1229a7ea14) Target: arm64-apple-macosx15.0

Additional information

The problem is specific to protocol extensions, as the following examples doesn't compile using either toolchain:

protocol SomeProtocol {}
struct SomeType: SomeProtocol {
    static func ==(lhs: SomeProtocol, rhs: SomeProtocol) -> Bool {
        true
    }
}
protocol SomeProtocol {}
struct SomeType: SomeProtocol {}
extension SomeType {
    static func ==(lhs: SomeProtocol, rhs: SomeProtocol) -> Bool {
        true
    }
}

The following examples, where the parameters are further generalized, doesn't compile on Swift 6.0.3 either, suggesting that the behavior in Swift 6.1 and 6.2 may be intentional:

public protocol EqualById {}
extension EqualById {
    public static func ==(lhs: Any, rhs: Any) -> Bool {
        true
    }
}
public protocol EqualById: BinaryInteger {}
extension EqualById {
    public static func ==(lhs: BinaryInteger, rhs: BinaryInteger) -> Bool{
        true
    }
}
public protocol EqualById {}
extension EqualById {
    public static func ==<T: EqualById>(lhs: T, rhs: T) -> Bool{
        true
    }
}
@TheHarcker TheHarcker added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jan 18, 2025
@TheHarcker TheHarcker changed the title Regression: Operators in protocol extensions doesn't compile in Swift 6.1 and Swift 6.2 snapshots when the parameters are generalized to take any instance satisfying the protocol and not just Self. Regression: Operators in protocol extensions doesn't compile in Swift 6.1 and Swift 6.2 snapshots when the parameters are generalized to take any instance satisfying the protocol and not just Self Jan 18, 2025
@stzn
Copy link
Contributor

stzn commented Jan 18, 2025

I traced the change history. It appears to be due to this change, which could be the correct fix.

@xedin
Copy link
Contributor

xedin commented Jan 19, 2025

cc @AnthonyLatsis

@AnthonyLatsis AnthonyLatsis self-assigned this Jan 19, 2025
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler itself type checker Area → compiler: Semantic analysis operators Feature: operators protocol Feature → type declarations: Protocol declarations Swift 6.2-dev regression and removed triage needed This issue needs more specific labels labels Jan 19, 2025
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 itself operators Feature: operators protocol Feature → type declarations: Protocol declarations regression Swift 6.2-dev type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants