Correctly implement hashCode for ImmArray, FrontStack, BackStack (#1641)

ImmArray, FrontStack, and BackStack violated the contract between equals
and hashCode. ImmArray had a wrong implementation of hashCode, and
FrontStack and BackStack had no implementation at all.

Fixes #1623.
This commit is contained in:
Gerolf Seitz 2019-06-13 22:40:58 +02:00 committed by GitHub
parent f9a07dc142
commit 8e144420c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 4 deletions

View File

@ -106,6 +106,8 @@ final class BackStack[+A] private (fq: BQ[A], len: Int) {
case _ => false
}
override def hashCode(): Int = toImmArray.hashCode()
/** O(n) */
override def toString: String =
"BackQueue(" + toImmArray.iterator.map(_.toString).mkString(",") + ")"

View File

@ -109,6 +109,8 @@ final class FrontStack[+A] private (fq: FQ[A], len: Int) {
case _ => false
}
override def hashCode(): Int = toImmArray.hashCode()
/** O(n) */
override def toString: String = "FrontStack(" + iterator.map(_.toString).mkString(",") + ")"
}

View File

@ -3,10 +3,8 @@
package com.digitalasset.daml.lf.data
import scala.language.higherKinds
import scala.collection.{IndexedSeqLike, IndexedSeqOptimized, mutable}
import scalaz.{Applicative, Equal, Foldable, Traverse}
import scalaz.syntax.applicative._
import scalaz.{Applicative, Equal, Foldable, Traverse}
import scala.annotation.tailrec
import scala.collection.generic.{
@ -16,6 +14,8 @@ import scala.collection.generic.{
IndexedSeqFactory
}
import scala.collection.immutable.IndexedSeq
import scala.collection.{IndexedSeqLike, IndexedSeqOptimized, mutable}
import scala.language.higherKinds
import scala.reflect.ClassTag
/** Simple immutable array. The intention is that all the operations have the "obvious"
@ -332,7 +332,7 @@ final class ImmArray[+A] private (
def filter(f: A => Boolean): ImmArray[A] =
collect { case x if f(x) => x }
override def hashCode(): Int = array.hashCode() // TODO is this fast?
override def hashCode(): Int = toSeq.hashCode()
}
object ImmArray {

View File

@ -136,6 +136,16 @@ class ImmArrayTest extends FlatSpec with Matchers with Checkers {
ImmArray[Int](1).relaxedSlice(0, -1) shouldBe ImmArray.empty[Int]
}
it should "implement equals and hashCode correctly" in {
val long = ImmArray(1, 2, 3, 4)
val shortened = long.relaxedSlice(1, 3)
val short = ImmArray(2, 3)
shortened.hashCode() shouldBe short.hashCode()
shortened shouldEqual short
}
behavior of "ImmArraySeq"
it should "use CanBuildFrom of ImmArraySeq" in {

View File

@ -9,6 +9,12 @@ This page contains release notes for the SDK.
HEAD — ongoing
--------------
Sandbox
~~~~~~~
- Fixed a bug in an internal data structure that broke contract keys.
See `#1623 <https://github.com/digital-asset/daml/issues/1623>`__.
0.12.25 — 2019-06-13
--------------------