From a206f11b6480dbf88d0a0defe785eff8b91d8eb5 Mon Sep 17 00:00:00 2001 From: Moritz Kiefer Date: Thu, 18 Nov 2021 07:46:16 +0100 Subject: [PATCH 1/3] Drop Scala 2.12 compatibility layer from //daml-lf/data changelog_begin changelog_end --- daml-lf/data/BUILD.bazel | 5 +- .../daml/lf/data/AbstractImmArraySeq.scala | 62 ------------------- .../daml/lf/data/AbstractInsertOrdSet.scala | 42 ------------- .../daml/lf/data/FrontStackInstances.scala | 40 ------------ .../com/daml/lf/data/ImmArrayInstances.scala | 40 ------------ .../com/daml/lf/data/LawlessTraversals.scala | 32 ---------- .../main/2.12/com/daml/lf/data/MapKOps.scala | 19 ------ .../daml/lf/data/AbstractInsertOrdSet.scala | 44 ------------- .../daml/lf/data/FrontStackInstances.scala | 20 ------ .../com/daml/lf/data/ImmArrayInstances.scala | 33 ---------- .../daml/lf/data/AbstractImmArraySeq.scala | 0 .../daml/lf/data/FrontStack.scala | 17 ++++- .../digitalasset/daml/lf/data/ImmArray.scala | 52 +++++++++++++--- .../daml/lf/data/InsertOrdSet.scala | 43 ++++++++++--- .../daml/lf/data/LawlessTraversals.scala | 0 15 files changed, 94 insertions(+), 355 deletions(-) delete mode 100644 daml-lf/data/src/main/2.12/com/daml/lf/data/AbstractImmArraySeq.scala delete mode 100644 daml-lf/data/src/main/2.12/com/daml/lf/data/AbstractInsertOrdSet.scala delete mode 100644 daml-lf/data/src/main/2.12/com/daml/lf/data/FrontStackInstances.scala delete mode 100644 daml-lf/data/src/main/2.12/com/daml/lf/data/ImmArrayInstances.scala delete mode 100644 daml-lf/data/src/main/2.12/com/daml/lf/data/LawlessTraversals.scala delete mode 100644 daml-lf/data/src/main/2.12/com/daml/lf/data/MapKOps.scala delete mode 100644 daml-lf/data/src/main/2.13/com/daml/lf/data/AbstractInsertOrdSet.scala delete mode 100644 daml-lf/data/src/main/2.13/com/daml/lf/data/FrontStackInstances.scala delete mode 100644 daml-lf/data/src/main/2.13/com/daml/lf/data/ImmArrayInstances.scala rename daml-lf/data/src/main/{2.13/com => scala/com/digitalasset}/daml/lf/data/AbstractImmArraySeq.scala (100%) rename daml-lf/data/src/main/{2.13/com => scala/com/digitalasset}/daml/lf/data/LawlessTraversals.scala (100%) diff --git a/daml-lf/data/BUILD.bazel b/daml-lf/data/BUILD.bazel index 6526c7c1fce6..1aa27b8938c2 100644 --- a/daml-lf/data/BUILD.bazel +++ b/daml-lf/data/BUILD.bazel @@ -10,14 +10,11 @@ load( "lf_scalacopts_stricter", "silencer_plugin", ) -load("@scala_version//:index.bzl", "scala_major_version") da_scala_library( name = "data", srcs = - glob(["src/main/scala/**/*.scala"]) + glob([ - "src/main/{}/**/*.scala".format(scala_major_version), - ]), + glob(["src/main/scala/**/*.scala"]), plugins = [ kind_projector_plugin, silencer_plugin, diff --git a/daml-lf/data/src/main/2.12/com/daml/lf/data/AbstractImmArraySeq.scala b/daml-lf/data/src/main/2.12/com/daml/lf/data/AbstractImmArraySeq.scala deleted file mode 100644 index ec8d84e23042..000000000000 --- a/daml-lf/data/src/main/2.12/com/daml/lf/data/AbstractImmArraySeq.scala +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.daml.lf.data - -import com.daml.scalautil.Statement.discard - -import scala.annotation.unchecked.uncheckedVariance -import scala.collection.generic.{ - CanBuildFrom, - GenericCompanion, - GenericTraversableTemplate, - IndexedSeqFactory, -} -import scala.collection.{IndexedSeqLike, IndexedSeqOptimized, mutable} - -import ImmArray.ImmArraySeq - -abstract class AbstractImmArraySeq[+A](array: ImmArray[A]) - extends IndexedSeq[A] - with GenericTraversableTemplate[A, ImmArraySeq] - with IndexedSeqLike[A, ImmArraySeq[A]] - with IndexedSeqOptimized[A, ImmArraySeq[A]] { this: ImmArraySeq[A] => - - override final def companion: GenericCompanion[ImmArraySeq] = ImmArraySeq - - override final def copyToArray[B >: A](xs: Array[B], dstStart: Int, dstLen: Int): Unit = { - discard(array.copyToArray(xs, dstStart, dstLen)) - } - - override final def map[B, That]( - f: A => B - )(implicit bf: CanBuildFrom[ImmArraySeq[A], B, That]): That = - bf match { - case _: IASCanBuildFrom[B] => array.map(f).toSeq - case _ => super.map(f)(bf) - } - - override final def to[Col[_]](implicit - bf: CanBuildFrom[Nothing, A, Col[A @uncheckedVariance]] - ): Col[A @uncheckedVariance] = - bf match { - case _: IASCanBuildFrom[A] => this - case _: ImmArrayInstances.IACanBuildFrom[A] => toImmArray - case _: FrontStackInstances.FSCanBuildFrom[A] => FrontStack.from(toImmArray) - case _ => super.to(bf) - } -} - -abstract class ImmArraySeqCompanion extends IndexedSeqFactory[ImmArraySeq] { - this: ImmArraySeq.type => - - protected type Factory[A] = CanBuildFrom[Coll, A, ImmArraySeq[A]] - - protected def canBuildFrom[A]: Factory[A] = - new IASCanBuildFrom - - override final def newBuilder[A]: mutable.Builder[A, ImmArraySeq[A]] = - ImmArray.newBuilder.mapResult(_.toSeq) -} - -private[data] final class IASCanBuildFrom[A] extends ImmArraySeq.GenericCanBuildFrom[A] diff --git a/daml-lf/data/src/main/2.12/com/daml/lf/data/AbstractInsertOrdSet.scala b/daml-lf/data/src/main/2.12/com/daml/lf/data/AbstractInsertOrdSet.scala deleted file mode 100644 index 8a5b9a697c3c..000000000000 --- a/daml-lf/data/src/main/2.12/com/daml/lf/data/AbstractInsertOrdSet.scala +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.daml.lf.data - -import scala.collection.immutable.Set -import scala.collection.{SetLike, AbstractSet} -import scala.collection.generic.{ - ImmutableSetFactory, - GenericCompanion, - CanBuildFrom, - GenericSetTemplate, -} - -abstract class AbstractInsertOrdSet[T] - extends AbstractSet[T] - with Set[T] - with SetLike[T, InsertOrdSet[T]] - with GenericSetTemplate[T, InsertOrdSet] - with Serializable { this: InsertOrdSet[T] => - override final def companion: GenericCompanion[InsertOrdSet] = InsertOrdSet - - protected def incl(elem: T): InsertOrdSet[T] - protected def excl(elem: T): InsertOrdSet[T] - - override final def +(elem: T): InsertOrdSet[T] = - incl(elem) - override final def -(elem: T): InsertOrdSet[T] = - excl(elem) - -} - -abstract class InsertOrdSetCompanion extends ImmutableSetFactory[InsertOrdSet] { - this: InsertOrdSet.type => - def emptyInstance: InsertOrdSet[Any] = empty[Any] - - protected type Factory[A] = CanBuildFrom[Coll, A, InsertOrdSet[A]] - - protected def canBuildFrom[A]: Factory[A] = - setCanBuildFrom[A] - -} diff --git a/daml-lf/data/src/main/2.12/com/daml/lf/data/FrontStackInstances.scala b/daml-lf/data/src/main/2.12/com/daml/lf/data/FrontStackInstances.scala deleted file mode 100644 index 4cf14167e863..000000000000 --- a/daml-lf/data/src/main/2.12/com/daml/lf/data/FrontStackInstances.scala +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.daml.lf.data - -import ScalazEqual.{equalBy, toIterableForScalazInstances} - -import scalaz.Equal - -import scala.collection.generic.CanBuildFrom -import scala.collection.mutable - -private[data] abstract class FrontStackInstances { - - def apply[T](elements: T*): FrontStack[T] = elements.to[FrontStack] - - implicit def equalInstance[A: Equal]: Equal[FrontStack[A]] = { - import scalaz.std.iterable._ - equalBy(fs => toIterableForScalazInstances(fs.iterator), true) - } - implicit def `FrontStack canBuildFrom`[A]: CanBuildFrom[FrontStack[_], A, FrontStack[A]] = - new FrontStackInstances.FSCanBuildFrom - - import scala.language.implicitConversions - - /** Enables 2.13-style `to` calls. */ - implicit def `FS companion to CBF`[A]( - self: FrontStack.type - ): CanBuildFrom[FrontStack[_], A, FrontStack[A]] = - self.`FrontStack canBuildFrom` -} - -private[data] object FrontStackInstances { - final class FSCanBuildFrom[A] extends CanBuildFrom[FrontStack[_], A, FrontStack[A]] { - override def apply(from: FrontStack[_]): mutable.Builder[A, FrontStack[A]] = apply() - - override def apply(): mutable.Builder[A, FrontStack[A]] = - ImmArray.newBuilder[A].mapResult(FrontStack.from) - } -} diff --git a/daml-lf/data/src/main/2.12/com/daml/lf/data/ImmArrayInstances.scala b/daml-lf/data/src/main/2.12/com/daml/lf/data/ImmArrayInstances.scala deleted file mode 100644 index 91bb83843c19..000000000000 --- a/daml-lf/data/src/main/2.12/com/daml/lf/data/ImmArrayInstances.scala +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.daml.lf.data - -import scalaz.Equal - -import scala.collection.compat.immutable.ArraySeq -import scala.collection.generic.CanBuildFrom -import scala.collection.mutable -import scala.language.implicitConversions - -private[data] abstract class ImmArrayInstances { - implicit def `ImmArray canBuildFrom`[A]: CanBuildFrom[ImmArray[_], A, ImmArray[A]] = - new ImmArrayInstances.IACanBuildFrom - - implicit def immArrayEqualInstance[A: Equal]: Equal[ImmArray[A]] = - ScalazEqual.withNatural(Equal[A].equalIsNatural)(_ equalz _) - - def newBuilder[A]: mutable.Builder[A, ImmArray[A]] = - ArraySeq - .newBuilder[Any] - .asInstanceOf[mutable.Builder[A, ArraySeq[A]]] - .mapResult(ImmArray.fromArraySeq(_)) - - def apply[A](xs: A*): ImmArray[A] = xs.to(ImmArray) - - /** Enables 2.13-style `to` calls. */ - implicit def `IS companion to CBF`[A]( - self: ImmArray.type - ): CanBuildFrom[ImmArray[_], A, ImmArray[A]] = - self.`ImmArray canBuildFrom` -} - -private[data] object ImmArrayInstances { - final class IACanBuildFrom[A] extends CanBuildFrom[ImmArray[_], A, ImmArray[A]] { - override def apply(from: ImmArray[_]) = ImmArray.newBuilder[A] - override def apply() = ImmArray.newBuilder[A] - } -} diff --git a/daml-lf/data/src/main/2.12/com/daml/lf/data/LawlessTraversals.scala b/daml-lf/data/src/main/2.12/com/daml/lf/data/LawlessTraversals.scala deleted file mode 100644 index 151f7dd802e8..000000000000 --- a/daml-lf/data/src/main/2.12/com/daml/lf/data/LawlessTraversals.scala +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.daml.lf.data - -import com.daml.scalautil.Statement.discard - -import scala.collection.IterableLike -import scala.collection.generic.CanBuildFrom -import scala.annotation.tailrec - -private[daml] object LawlessTraversals { - implicit final class `Lawless iterable traversal`[A, This](private val seq: IterableLike[A, This]) - extends AnyVal { - def traverseEitherStrictly[E, B, That]( - f: A => Either[E, B] - )(implicit cbf: CanBuildFrom[This, B, That]): Either[E, That] = { - val that = cbf(seq.repr) - that.sizeHint(seq) - val i = seq.iterator - @tailrec def lp(): Either[E, That] = - if (i.hasNext) f(i.next) match { - case Left(b) => Left(b) - case Right(c) => - discard(that += c) - lp() - } - else Right(that.result) - lp() - } - } -} diff --git a/daml-lf/data/src/main/2.12/com/daml/lf/data/MapKOps.scala b/daml-lf/data/src/main/2.12/com/daml/lf/data/MapKOps.scala deleted file mode 100644 index 971759d88e27..000000000000 --- a/daml-lf/data/src/main/2.12/com/daml/lf/data/MapKOps.scala +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.daml.lf.data - -import scala.language.higherKinds -import scala.collection.GenTraversableOnce -import scala.collection.immutable.{Map, MapLike} - -/** Halfway between the *-kinded MapLike and *->*->*-kinded MapOps. */ -trait MapKOps[K, +V, +This[+TV] <: Map[K, TV] with MapKOps[K, TV, This]] - extends MapLike[K, V, This[V]] { this: This[V] => - override def updated[V1 >: V](key: K, value: V1): This[V1] = this + ((key, value)) - override def +[V1 >: V](kv: (K, V1)): This[V1] - override def +[V1 >: V](elem1: (K, V1), elem2: (K, V1), elems: (K, V1)*): This[V1] = - this + elem1 + elem2 ++ elems - override def ++[V1 >: V](xs: GenTraversableOnce[(K, V1)]): This[V1] = - xs.seq.foldLeft(this: This[V1])(_ + _) -} diff --git a/daml-lf/data/src/main/2.13/com/daml/lf/data/AbstractInsertOrdSet.scala b/daml-lf/data/src/main/2.13/com/daml/lf/data/AbstractInsertOrdSet.scala deleted file mode 100644 index e411b9bf1eb3..000000000000 --- a/daml-lf/data/src/main/2.13/com/daml/lf/data/AbstractInsertOrdSet.scala +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.daml.lf.data - -import scala.collection.{IterableFactory, IterableFactoryDefaults} -import scala.collection.immutable.{AbstractSet, StrictOptimizedSetOps} -import scala.collection.mutable.ReusableBuilder - -abstract class AbstractInsertOrdSet[T] - extends AbstractSet[T] - with IterableFactoryDefaults[T, InsertOrdSet] - with StrictOptimizedSetOps[T, InsertOrdSet, InsertOrdSet[T]] - with Serializable { this: InsertOrdSet[T] => - override final def iterableFactory: IterableFactory[InsertOrdSet] = InsertOrdSet -} - -abstract class InsertOrdSetCompanion extends IterableFactory[InsertOrdSet] { - this: InsertOrdSet.type => - protected type Factory[A] = Unit - - protected def canBuildFrom[A]: Factory[A] = () - - def from[T](it: IterableOnce[T]): InsertOrdSet[T] = { - it match { - case s: InsertOrdSet[T] => s - case _ => (newBuilder[T] ++= it).result() - } - } - - def newBuilder[T]: ReusableBuilder[T, InsertOrdSet[T]] = new InsertOrdSetBuilder[T] - - private final class InsertOrdSetBuilder[T] extends ReusableBuilder[T, InsertOrdSet[T]] { - var m: InsertOrdSet[T] = empty; - override def clear(): Unit = { - m = empty; - } - override def result(): InsertOrdSet[T] = m - override def addOne(elem: T): this.type = { - m = m.incl(elem) - this - } - } -} diff --git a/daml-lf/data/src/main/2.13/com/daml/lf/data/FrontStackInstances.scala b/daml-lf/data/src/main/2.13/com/daml/lf/data/FrontStackInstances.scala deleted file mode 100644 index 2a3cc2565c84..000000000000 --- a/daml-lf/data/src/main/2.13/com/daml/lf/data/FrontStackInstances.scala +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.daml.lf.data - -import ScalazEqual.{equalBy, toIterableForScalazInstances} - -import scalaz.Equal -import scala.collection.mutable - -abstract class FrontStackInstances extends scala.collection.IterableFactory[FrontStack] { - override def from[A](it: IterableOnce[A]): FrontStack[A] = - FrontStack.from(ImmArray.from(it)) - override def newBuilder[A]: mutable.Builder[A, FrontStack[A]] = - ImmArray.newBuilder.mapResult(FrontStack.from) - implicit def equalInstance[A: Equal]: Equal[FrontStack[A]] = { - import scalaz.std.iterable._ - equalBy(fs => toIterableForScalazInstances(fs.iterator), true) - } -} diff --git a/daml-lf/data/src/main/2.13/com/daml/lf/data/ImmArrayInstances.scala b/daml-lf/data/src/main/2.13/com/daml/lf/data/ImmArrayInstances.scala deleted file mode 100644 index 80d1f595731e..000000000000 --- a/daml-lf/data/src/main/2.13/com/daml/lf/data/ImmArrayInstances.scala +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.daml.lf.data - -import scalaz.Equal - -import scala.collection.immutable.ArraySeq -import scala.collection.mutable.Builder - -abstract class ImmArrayInstances extends scala.collection.IterableFactory[ImmArray] { - implicit def immArrayEqualInstance[A: Equal]: Equal[ImmArray[A]] = - ScalazEqual.withNatural(Equal[A].equalIsNatural)(_ equalz _) - - override def from[A](it: IterableOnce[A]): ImmArray[A] = { - it match { - case arraySeq: ImmArray.ImmArraySeq[A] => - arraySeq.toImmArray - case otherwise => - val builder = newBuilder[A] - builder.sizeHint(otherwise) - builder - .addAll(otherwise) - .result() - } - } - - override def newBuilder[A]: Builder[A, ImmArray[A]] = - ArraySeq - .newBuilder[Any] - .asInstanceOf[Builder[A, ArraySeq[A]]] - .mapResult(ImmArray.fromArraySeq(_)) -} diff --git a/daml-lf/data/src/main/2.13/com/daml/lf/data/AbstractImmArraySeq.scala b/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/AbstractImmArraySeq.scala similarity index 100% rename from daml-lf/data/src/main/2.13/com/daml/lf/data/AbstractImmArraySeq.scala rename to daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/AbstractImmArraySeq.scala diff --git a/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/FrontStack.scala b/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/FrontStack.scala index eac0ab08520f..9d74d24a4080 100644 --- a/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/FrontStack.scala +++ b/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/FrontStack.scala @@ -3,10 +3,11 @@ package com.daml.lf.data -import ScalazEqual.{orderBy, toIterableForScalazInstances} -import scalaz.{Applicative, Order, Traverse} +import ScalazEqual.{equalBy, orderBy, toIterableForScalazInstances} +import scalaz.{Applicative, Equal, Order, Traverse} import scala.annotation.tailrec +import scala.collection.mutable import scala.util.hashing.MurmurHash3 /** A stack which allows to cons, prepend, and pop in constant time, and generate an ImmArray in linear time. @@ -139,7 +140,7 @@ final class FrontStack[+A] private (fq: FrontStack.FQ[A], val length: Int) { override def toString: String = "FrontStack(" + iterator.map(_.toString).mkString(",") + ")" } -object FrontStack extends FrontStackInstances { +object FrontStack extends scala.collection.IterableFactory[FrontStack] { val Empty: FrontStack[Nothing] = new FrontStack(FQEmpty, 0) def empty[A]: FrontStack[A] = FrontStack.Empty @@ -147,8 +148,18 @@ object FrontStack extends FrontStackInstances { def from[A](xs: ImmArray[A]): FrontStack[A] = if (xs.isEmpty) Empty else new FrontStack(FQPrepend(xs, FQEmpty), length = xs.length) + override def from[A](it: IterableOnce[A]): FrontStack[A] = + FrontStack.from(ImmArray.from(it)) + def unapply[T](xs: FrontStack[T]): Boolean = xs.isEmpty + override def newBuilder[A]: mutable.Builder[A, FrontStack[A]] = + ImmArray.newBuilder.mapResult(arr => FrontStack.from(arr)) + implicit def equalInstance[A: Equal]: Equal[FrontStack[A]] = { + import scalaz.std.iterable._ + equalBy(fs => toIterableForScalazInstances(fs.iterator), true) + } + implicit val `FrontStack covariant`: Traverse[FrontStack] = new Traverse[FrontStack] { override def traverseImpl[G[_]: Applicative, A, B]( fa: FrontStack[A] diff --git a/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/ImmArray.scala b/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/ImmArray.scala index b22d244cb321..bb08c10f8821 100644 --- a/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/ImmArray.scala +++ b/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/ImmArray.scala @@ -11,7 +11,10 @@ import scalaz.syntax.applicative._ import scalaz.{Applicative, Equal, Foldable, Order, Traverse} import scala.annotation.tailrec +import scala.collection.StrictOptimizedSeqFactory import scala.collection.compat.immutable.ArraySeq +import scala.collection.immutable.{AbstractSeq, IndexedSeqOps, StrictOptimizedSeqOps} +import scala.collection.mutable.Builder import scala.reflect.ClassTag /** Simple immutable array. The intention is that all the operations have the "obvious" @@ -362,7 +365,7 @@ final class ImmArray[+A] private ( } -object ImmArray extends ImmArrayInstances { +object ImmArray extends scala.collection.IterableFactory[ImmArray] { val Empty: ImmArray[Nothing] = ImmArray.fromArraySeq(ArraySeq.empty) @@ -407,13 +410,40 @@ object ImmArray extends ImmArrayInstances { orderBy(ia => toIterableForScalazInstances(ia.iterator), true) } + implicit def immArrayEqualInstance[A: Equal]: Equal[ImmArray[A]] = + ScalazEqual.withNatural(Equal[A].equalIsNatural)(_ equalz _) + + override def from[A](it: IterableOnce[A]): ImmArray[A] = { + it match { + case arraySeq: ImmArray.ImmArraySeq[A] => + arraySeq.toImmArray + case otherwise => + val builder = newBuilder[A] + builder.sizeHint(otherwise) + builder + .addAll(otherwise) + .result() + } + } + + override def newBuilder[A]: Builder[A, ImmArray[A]] = + ArraySeq + .newBuilder[Any] + .asInstanceOf[Builder[A, ArraySeq[A]]] + .mapResult(ImmArray.fromArraySeq(_)) + /** Note: we define this purely to be able to write `toSeq`. * * However, _do not_ use it for anything but defining interface where you need * to expose a `Seq`, and you also need to use implicits that refer to the * specific types, such as the traverse instance. */ - final class ImmArraySeq[+A](array: ImmArray[A]) extends AbstractImmArraySeq[A](array) { + final class ImmArraySeq[+A](array: ImmArray[A]) + extends AbstractSeq[A] + with IndexedSeq[A] + with IndexedSeqOps[A, ImmArraySeq, ImmArraySeq[A]] + with StrictOptimizedSeqOps[A, ImmArraySeq, ImmArraySeq[A]] + with scala.collection.IterableFactoryDefaults[A, ImmArraySeq] { // TODO make this faster by implementing as many methods as possible. override def iterator: Iterator[A] = array.iterator @@ -430,10 +460,22 @@ object ImmArray extends ImmArrayInstances { override def slice(from: Int, to: Int): ImmArraySeq[A] = new ImmArraySeq(array.relaxedSlice(from, to)) + override final def iterableFactory: scala.collection.SeqFactory[ImmArraySeq] = ImmArraySeq + + override final def copyToArray[B >: A](xs: Array[B], dstStart: Int, dstLen: Int): Int = + array.copyToArray(xs, dstStart, dstLen) + def toImmArray: ImmArray[A] = array } - object ImmArraySeq extends ImmArraySeqCompanion { + object ImmArraySeq extends StrictOptimizedSeqFactory[ImmArraySeq] { + type Factory[A] = Unit + final def canBuildFrom[A]: Factory[A] = () + final def empty[A]: ImmArraySeq[Nothing] = Empty + final def from[E](it: IterableOnce[E]): ImmArraySeq[E] = + ImmArray.newBuilder.addAll(it).result().toSeq + final def newBuilder[A] = ImmArray.newBuilder.mapResult(_.toSeq) + val Empty: ImmArraySeq[Nothing] = ImmArray.Empty.toSeq implicit val `immArraySeq Traverse instance`: Traverse[ImmArraySeq] = new Traverse[ImmArraySeq] with Foldable.FromFoldr[ImmArraySeq] @@ -454,10 +496,6 @@ object ImmArray extends ImmArrayInstances { implicit def `immArraySeq Equal instance`[A: Equal]: Equal[ImmArraySeq[A]] = equalBy(_.toImmArray, true) - - // Here only for 2.12 (harmless in 2.13); placed in ImmArraySeqCompanion the - // implicit gets in an unwinnable fight with IndexedSeq's version - override implicit def canBuildFrom[A]: Factory[A] = super.canBuildFrom } } diff --git a/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/InsertOrdSet.scala b/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/InsertOrdSet.scala index 1326cd6fcbfd..567c7427509f 100644 --- a/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/InsertOrdSet.scala +++ b/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/InsertOrdSet.scala @@ -3,7 +3,9 @@ package com.daml.lf.data -import scala.collection.immutable.{HashSet, Queue} +import scala.collection.{IterableFactory, IterableFactoryDefaults} +import scala.collection.immutable.{AbstractSet, HashSet, Queue, StrictOptimizedSetOps} +import scala.collection.mutable.ReusableBuilder /** Insert-ordered Set. * @@ -14,9 +16,13 @@ import scala.collection.immutable.{HashSet, Queue} * remove: O(n) */ final class InsertOrdSet[T] private (_items: Queue[T], _hashSet: HashSet[T]) - extends AbstractInsertOrdSet[T] { + extends AbstractSet[T] + with IterableFactoryDefaults[T, InsertOrdSet] + with StrictOptimizedSetOps[T, InsertOrdSet, InsertOrdSet[T]] + with Serializable { override def empty: InsertOrdSet[T] = InsertOrdSet.empty override def size: Int = _hashSet.size + override final def iterableFactory: IterableFactory[InsertOrdSet] = InsertOrdSet def iterator: Iterator[T] = _items.reverseIterator @@ -40,16 +46,35 @@ final class InsertOrdSet[T] private (_items: Queue[T], _hashSet: HashSet[T]) ) } -object InsertOrdSet extends InsertOrdSetCompanion { +object InsertOrdSet extends IterableFactory[InsertOrdSet] { + type Factory[A] = Unit + + def canBuildFrom[A]: Factory[A] = () + + def from[T](it: IterableOnce[T]): InsertOrdSet[T] = { + it match { + case s: InsertOrdSet[T] => s + case _ => (newBuilder[T] ++= it).result() + } + } + + def newBuilder[T]: ReusableBuilder[T, InsertOrdSet[T]] = new InsertOrdSetBuilder[T] + private val Empty = new InsertOrdSet(Queue.empty, HashSet.empty) - override def empty[T] = Empty.asInstanceOf[InsertOrdSet[T]] + final def empty[T] = Empty.asInstanceOf[InsertOrdSet[T]] def fromSeq[T](s: Seq[T]): InsertOrdSet[T] = new InsertOrdSet(Queue(s.reverse: _*), HashSet(s: _*)) - // Here only for 2.12 (harmless in 2.13); placed in InsertOrdSetCompanion the - // implicit gets in an unwinnable fight with Set's version - implicit override def canBuildFrom[A]: Factory[A] = - super.canBuildFrom - + private final class InsertOrdSetBuilder[T] extends ReusableBuilder[T, InsertOrdSet[T]] { + var m: InsertOrdSet[T] = empty; + override def clear(): Unit = { + m = empty; + } + override def result(): InsertOrdSet[T] = m + override def addOne(elem: T): this.type = { + m = m.incl(elem) + this + } + } } diff --git a/daml-lf/data/src/main/2.13/com/daml/lf/data/LawlessTraversals.scala b/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/LawlessTraversals.scala similarity index 100% rename from daml-lf/data/src/main/2.13/com/daml/lf/data/LawlessTraversals.scala rename to daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/LawlessTraversals.scala From adbd785d03acd2015b374ad56e3c2da1588c481d Mon Sep 17 00:00:00 2001 From: Moritz Kiefer Date: Thu, 18 Nov 2021 09:52:31 +0100 Subject: [PATCH 2/3] Update daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/ImmArray.scala Co-authored-by: Remy --- .../src/main/scala/com/digitalasset/daml/lf/data/ImmArray.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/ImmArray.scala b/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/ImmArray.scala index bb08c10f8821..ab84f26ba2c2 100644 --- a/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/ImmArray.scala +++ b/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/ImmArray.scala @@ -12,7 +12,7 @@ import scalaz.{Applicative, Equal, Foldable, Order, Traverse} import scala.annotation.tailrec import scala.collection.StrictOptimizedSeqFactory -import scala.collection.compat.immutable.ArraySeq +import scala.collection.immutable.ArraySeq import scala.collection.immutable.{AbstractSeq, IndexedSeqOps, StrictOptimizedSeqOps} import scala.collection.mutable.Builder import scala.reflect.ClassTag From 24dc924022ce650a918ee00b24bcb9c9659b04f2 Mon Sep 17 00:00:00 2001 From: Moritz Kiefer Date: Thu, 18 Nov 2021 09:54:49 +0100 Subject: [PATCH 3/3] drop dependency on compat libraries changelog_begin changelog_end --- daml-lf/data/BUILD.bazel | 2 -- .../scala/com/digitalasset/daml/lf/data/InsertOrdSet.scala | 4 ++-- .../com/digitalasset/daml/lf/data/SortedLookupList.scala | 1 - .../scala/com/digitalasset/daml/lf/data/RelationTest.scala | 2 -- 4 files changed, 2 insertions(+), 7 deletions(-) diff --git a/daml-lf/data/BUILD.bazel b/daml-lf/data/BUILD.bazel index 1aa27b8938c2..39df12016cdc 100644 --- a/daml-lf/data/BUILD.bazel +++ b/daml-lf/data/BUILD.bazel @@ -20,7 +20,6 @@ da_scala_library( silencer_plugin, ], scala_deps = [ - "@maven//:org_scala_lang_modules_scala_collection_compat", "@maven//:org_scalaz_scalaz_core", ], scalacopts = lf_scalacopts_stricter + [ @@ -51,7 +50,6 @@ da_scala_test( "@maven//:org_scalatestplus_scalacheck_1_15", "@maven//:org_scalaz_scalaz_core", "@maven//:org_scalaz_scalaz_scalacheck_binding", - "@maven//:org_scala_lang_modules_scala_collection_compat", ], scalacopts = lf_scalacopts + [ "-P:silencer:lineContentFilters=import ImmArraySeq.Implicits._", diff --git a/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/InsertOrdSet.scala b/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/InsertOrdSet.scala index 567c7427509f..788ee76597dc 100644 --- a/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/InsertOrdSet.scala +++ b/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/InsertOrdSet.scala @@ -67,9 +67,9 @@ object InsertOrdSet extends IterableFactory[InsertOrdSet] { new InsertOrdSet(Queue(s.reverse: _*), HashSet(s: _*)) private final class InsertOrdSetBuilder[T] extends ReusableBuilder[T, InsertOrdSet[T]] { - var m: InsertOrdSet[T] = empty; + var m: InsertOrdSet[T] = empty override def clear(): Unit = { - m = empty; + m = empty } override def result(): InsertOrdSet[T] = m override def addOne(elem: T): this.type = { diff --git a/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/SortedLookupList.scala b/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/SortedLookupList.scala index e467ea72534f..8a249ad98e55 100644 --- a/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/SortedLookupList.scala +++ b/daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/SortedLookupList.scala @@ -11,7 +11,6 @@ import scalaz.std.string._ import scalaz.syntax.traverse._ import scala.collection.immutable.HashMap -import scala.collection.compat._ /** We use this container to pass around Daml-LF text maps as flat lists in various parts of the codebase. */ // Note that keys are ordered using Utf8 ordering diff --git a/daml-lf/data/src/test/scala/com/digitalasset/daml/lf/data/RelationTest.scala b/daml-lf/data/src/test/scala/com/digitalasset/daml/lf/data/RelationTest.scala index f74436f5a656..d66139b8d0cf 100644 --- a/daml-lf/data/src/test/scala/com/digitalasset/daml/lf/data/RelationTest.scala +++ b/daml-lf/data/src/test/scala/com/digitalasset/daml/lf/data/RelationTest.scala @@ -9,8 +9,6 @@ import org.scalatest.matchers.should.Matchers import org.scalatest.propspec.AnyPropSpec import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks -import scala.collection.compat._ - final class RelationTest extends AnyPropSpec with Matchers with ScalaCheckPropertyChecks { import Relation.Relation._