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

Misleading error message when comparing generics that vary in types #73029

Open
Alc-Alc opened this issue Apr 14, 2024 · 1 comment
Open

Misleading error message when comparing generics that vary in types #73029

Alc-Alc opened this issue Apr 14, 2024 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation expressions Feature: expressions generics Feature: generic declarations and types operators Feature: operators swift 6.0 type checker Area → compiler: Semantic analysis

Comments

@Alc-Alc
Copy link

Alc-Alc commented Apr 14, 2024

Description

When the following code is run, the error I get is

error: binary operator '==' cannot be applied to two 'MyStruct<String>' operands

While it is apparent that generics that differ in types cannot be compared, the error message does not indicate that is indeed the problem here. It says it cannot compare "two 'MyStruct<String>' operands", however, one is a MyStruct<String> and other is a MyStruct<Int>.

Reproduction

struct MyStruct<T: Equatable> {
    let val: T

    init(_ val: T) {
        self.val = val
    }

    static func == (lhs: MyStruct, rhs: MyStruct) -> Bool{
        lhs.val == rhs.val
    }
}

print(MyStruct(0) == MyStruct(0))  // true
print(MyStruct("0") == MyStruct(0))

Expected behavior

I would expect an intuitive error message like so, that shows what exactly is the problem (types are different)

error: binary operator '==' cannot be applied to 'MyStruct<String>' and 'MyStruct<Int>' operands

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)                                                                                                                                                              
Target: arm64-apple-macosx14.0

Additional information

Asked this in Swift forums and I was suggested to file a report here.

@Alc-Alc Alc-Alc added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Apr 14, 2024
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis operators Feature: operators expressions Feature: expressions swift 6.0 generics Feature: generic declarations and types and removed triage needed This issue needs more specific labels labels Apr 16, 2024
@AnthonyLatsis
Copy link
Collaborator

Observation: Given

struct S<T> {
  static func ==(lhs: S, rhs: S) -> Bool {
    true
  }
}

reproduces with

func getS<T>(value: T) -> S<T> {}

let _ = getS(value: "") == getS(value: 0)

but not

func getS<T>(type: T.Type) -> S<T> {}

let _ = getS(type: String.self) == getS(type: Int.self)

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 diagnostics QoI Bug: Diagnostics Quality of Implementation expressions Feature: expressions generics Feature: generic declarations and types operators Feature: operators swift 6.0 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants