Open Alc-Alc opened 6 months ago
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)
additional observation: if the struct & operator declarations are local the behavior differs and you get the expected diagnostic:
func test_local_op_ambig() {
struct S<T> {
static func ==(lhs: S, rhs: S) -> Bool {
true
}
}
func getS<T>(value: T) -> S<T> {}
let _ = getS(value: "") == getS(value: 0) // Binary operator '==' cannot be applied to operands of type 'S<String>' and 'S<Int>'
}
seems likely this behavior is due to multiple valid solutions being found by the type constraint solver that have the same 'score' and the one being arbitrarily picked is more confusing than other valid solutions in this context. for posterity, the code that appears responsible for emitting the diagnostic is here.
Description
When the following code is run, the error I get is
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 aMyStruct<String>
and other is aMyStruct<Int>
.Reproduction
Expected behavior
I would expect an intuitive error message like so, that shows what exactly is the problem (types are different)
Environment
Additional information
Asked this in Swift forums and I was suggested to file a report here.