scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

case value class `canEqual` is inconsistent #13034

Open lrytz opened 2 months ago

lrytz commented 2 months ago
scala> case class VCC(x: Int) extends AnyVal
class VCC

scala> val v = VCC(1)
val v: VCC = VCC(1)

scala> v.canEqual(v)
val res0: Boolean = false

scala> v.canEqual(1)
val res1: Boolean = true

scala> v == v
val res2: Boolean = true

Fortunately, equals doesn't invoke canEqual because the class is final (it's a value class) https://github.com/scala/scala/blob/v2.13.14/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala#L200.

lrytz commented 2 months ago

... somewhat related: value case classes don't take canEquals into account when they should. It works for final case classes.

scala> case class VCC(x: Int) extends AnyVal { def canEqual(x: Any) = false }
class VCC

scala> VCC(1) == VCC(1)
val res0: Boolean = true

scala> final case class FCC(x: Int) { def canEqual(x: Any) = false }
class FCC

scala> FCC(1) == FCC(1)
val res2: Boolean = false