Skip to content

Fix #8314: Define TupleXXL equals and hashCode as in case classes #8316

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

Merged
merged 1 commit into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions library/src/scala/runtime/TupleXXL.scala
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
package scala.runtime
import java.util.Arrays.{deepEquals, deepHashCode}

final class TupleXXL private (es: IArray[Object]) extends Product {
assert(es.length > 22)

def productElement(n: Int): Any = es(n)
def productArity: Int = es.length
override def productPrefix: String = "TupleXXL"

override def toString: String =
elems.asInstanceOf[Array[Object]].mkString("(", ",", ")")

override def hashCode: Int =
scala.runtime.ScalaRunTime._hashCode(this)

override def toString = elems.asInstanceOf[Array[Object]].mkString("(", ",", ")")
override def hashCode = getClass.hashCode * 41 + deepHashCode(elems.asInstanceOf[Array[Object]])
override def canEqual(that: Any): Boolean = that match {
case that: TupleXXL => that.productArity == this.productArity
case _ => false
}

override def equals(that: Any) = that match {
case that: TupleXXL => deepEquals(this.elems.asInstanceOf[Array[Object]], that.elems.asInstanceOf[Array[Object]])
override def equals(that: Any): Boolean = that match {
case that: TupleXXL =>
es.asInstanceOf[AnyRef].eq(that.elems.asInstanceOf[AnyRef]) || {
if es.length != that.elems.length then return false
var i = 0
while i < es.length do
if es(i) != that.elems(i) then return false
i += 1
true
}
case _ => false
}

def elems: IArray[Object] = es

def tailXXL: TupleXXL = {
Expand Down
28 changes: 28 additions & 0 deletions tests/run/i8314.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

@main def Test = {

val t1: Tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25)
val t2: Tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25.0)
val t3: Tuple = (1, 2, 3, 4, 5, 6, 7, 8.0, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25)
assert((1, 2) == (1, 2.0))
assert(t1 == t2)
assert(t1 == t3)
assert(t1.hashCode == t2.hashCode)
assert(t1.hashCode == t3.hashCode)

val t4: Tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, t1)
val t5: Tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, t2)
assert((1, (1, 2)) == (1, (1, 2.0)))
assert(t4 == t5)
assert(t4.hashCode == t5.hashCode)

val t6: Tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, Array(1, 2, 3))
val t7: Tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, Array(1, 2, 3))
assert((1, 2, Array(3, 4)) != (1, 2, Array(3, 4)))
assert(t6 != t7)
assert(t6.hashCode != t7.hashCode)

val t8: Tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26)
assert(t1 != t8)
assert(t1.hashCode != t8.hashCode)
}