From b2a3a0a5f70f9593f808d8cdf783cff132761f1f Mon Sep 17 00:00:00 2001 From: Kailuo Wang Date: Wed, 26 Jun 2019 14:53:22 -0400 Subject: [PATCH 1/6] fixed all compiler warnings in non tests --- .jvmopts | 4 ++-- build.sbt | 6 ++--- core/src/main/scala/cats/Inject.scala | 2 +- core/src/main/scala/cats/InjectK.scala | 2 +- .../main/scala/cats/data/NonEmptySet.scala | 4 +++- .../main/scala/cats/data/NonEmptyVector.scala | 5 ++-- core/src/main/scala/cats/data/OneAnd.scala | 1 + core/src/main/scala/cats/data/ZipList.scala | 14 +++++++---- core/src/main/scala/cats/data/ZipStream.scala | 3 ++- core/src/main/scala/cats/data/ZipVector.scala | 11 +++++---- core/src/main/scala/cats/data/package.scala | 4 ++++ .../main/scala/cats/instances/either.scala | 18 +++++++-------- .../main/scala/cats/instances/parallel.scala | 1 + .../scala/cats/kernel/laws/LawTests.scala | 13 ++++++----- .../kernel/compat/scalaVersionSpecific.scala | 20 ++++++++++++++++ .../cats/kernel/compat/HashCompat.scala | 2 +- .../kernel/compat/scalaVersionSpecific.scala | 8 ++++++- kernel/src/main/scala/cats/kernel/Eq.scala | 14 +++++------ .../src/main/scala/cats/kernel/Monoid.scala | 4 ++-- .../main/scala/cats/kernel/PartialOrder.scala | 7 +++--- .../cats/kernel/instances/MapInstances.scala | 2 +- .../cats/kernel/instances/StaticMethods.scala | 2 +- .../kernel/instances/StringInstances.scala | 2 +- .../cats/kernel/instances/UnitInstances.scala | 2 +- .../cats/laws/discipline/Arbitrary.scala | 1 + .../BinCodecInvariantMonoidalSuite.scala | 1 + .../test/scala/cats/tests/EitherTSuite.scala | 2 +- .../test/scala/cats/tests/FoldableSuite.scala | 3 ++- .../test/scala/cats/tests/LazyListSuite.scala | 1 + .../scala/cats/tests/NonEmptyListSuite.scala | 21 +++++++++++------ .../test/scala/cats/tests/OneAndSuite.scala | 23 +++++++++++++------ .../test/scala/cats/tests/ParallelSuite.scala | 1 + .../scala/cats/tests/RegressionSuite.scala | 3 ++- .../test/scala/cats/tests/TraverseSuite.scala | 1 + 34 files changed, 138 insertions(+), 70 deletions(-) diff --git a/.jvmopts b/.jvmopts index bc6266957c..08459dfaaf 100644 --- a/.jvmopts +++ b/.jvmopts @@ -1,6 +1,6 @@ -Dfile.encoding=UTF8 -Xms1G -Xmx4G --XX:ReservedCodeCacheSize=250M +-XX:ReservedCodeCacheSize=500M -XX:+TieredCompilation --XX:+UseParallelGC \ No newline at end of file +-XX:+UseParallelGC diff --git a/build.sbt b/build.sbt index 112d0c0d19..5284ccfef1 100644 --- a/build.sbt +++ b/build.sbt @@ -816,12 +816,12 @@ def commonScalacOptions(scalaVersion: String) = "-unchecked", "-Ywarn-dead-code", "-Ywarn-numeric-widen", - "-Ywarn-value-discard" + "-Ywarn-value-discard", + "-Xfatal-warnings", + "-deprecation" ) ++ (if (priorTo2_13(scalaVersion)) Seq( "-Yno-adapted-args", - "-Xfatal-warnings", // TODO: add the following two back to 2.13 - "-deprecation", "-Xfuture" ) else diff --git a/core/src/main/scala/cats/Inject.scala b/core/src/main/scala/cats/Inject.scala index 0bce716249..3433246451 100644 --- a/core/src/main/scala/cats/Inject.scala +++ b/core/src/main/scala/cats/Inject.scala @@ -44,7 +44,7 @@ sealed abstract private[cats] class InjectInstances { new Inject[A, Either[C, B]] { val inj = (a: A) => Right(I.inj(a)) - val prj = (_: Either[C, B]).right.toOption.flatMap(I.prj) + val prj = (_: Either[C, B]).toOption.flatMap(I.prj) } } diff --git a/core/src/main/scala/cats/InjectK.scala b/core/src/main/scala/cats/InjectK.scala index a5e22ade58..26791e60c4 100644 --- a/core/src/main/scala/cats/InjectK.scala +++ b/core/src/main/scala/cats/InjectK.scala @@ -51,7 +51,7 @@ sealed abstract private[cats] class InjectKInstances { new InjectK[F, EitherK[H, G, ?]] { val inj = λ[FunctionK[G, EitherK[H, G, ?]]](EitherK.rightc(_)).compose(I.inj) - val prj = λ[FunctionK[EitherK[H, G, ?], λ[α => Option[F[α]]]]](_.run.right.toOption.flatMap(I.prj(_))) + val prj = λ[FunctionK[EitherK[H, G, ?], λ[α => Option[F[α]]]]](_.run.toOption.flatMap(I.prj(_))) } } diff --git a/core/src/main/scala/cats/data/NonEmptySet.scala b/core/src/main/scala/cats/data/NonEmptySet.scala index 2df60fd743..e5961907d6 100644 --- a/core/src/main/scala/cats/data/NonEmptySet.scala +++ b/core/src/main/scala/cats/data/NonEmptySet.scala @@ -22,6 +22,7 @@ import cats.kernel._ import cats.syntax.order._ import scala.collection.immutable._ +import kernel.compat.scalaVersionSpecific._ private[data] object NonEmptySetImpl extends NonEmptySetInstances with Newtype { @@ -48,6 +49,7 @@ private[data] object NonEmptySetImpl extends NonEmptySetInstances with Newtype { new NonEmptySetOps(value) } +@suppressUnusedImportWarningForScalaVersionSpecific sealed class NonEmptySetOps[A](val value: NonEmptySet[A]) { implicit private val ordering: Ordering[A] = toSortedSet.ordering @@ -335,7 +337,7 @@ sealed class NonEmptySetOps[A](val value: NonEmptySet[A]) { */ def zipWith[B, C](b: NonEmptySet[B])(f: (A, B) => C)(implicit C: Order[C]): NonEmptySet[C] = { implicit val cOrdering = C.toOrdering - NonEmptySetImpl.create((toSortedSet, b.toSortedSet).zipped.map(f)) + NonEmptySetImpl.create((toSortedSet.lazyZip(b.toSortedSet)).map(f)) } /** diff --git a/core/src/main/scala/cats/data/NonEmptyVector.scala b/core/src/main/scala/cats/data/NonEmptyVector.scala index 568750a91c..f2f7139776 100644 --- a/core/src/main/scala/cats/data/NonEmptyVector.scala +++ b/core/src/main/scala/cats/data/NonEmptyVector.scala @@ -5,7 +5,7 @@ import cats.data.NonEmptyVector.ZipNonEmptyVector import scala.annotation.tailrec import scala.collection.immutable.{TreeSet, VectorBuilder} import cats.instances.vector._ - +import kernel.compat.scalaVersionSpecific._ /** * A data type which represents a `Vector` guaranteed to contain at least one element. *
@@ -218,7 +218,7 @@ final class NonEmptyVector[+A] private (val toVector: Vector[A]) extends AnyVal * }}} */ def zipWith[B, C](b: NonEmptyVector[B])(f: (A, B) => C): NonEmptyVector[C] = - NonEmptyVector.fromVectorUnsafe((toVector, b.toVector).zipped.map(f)) + NonEmptyVector.fromVectorUnsafe(toVector.lazyZip(b.toVector).map(f)) def reverse: NonEmptyVector[A] = new NonEmptyVector(toVector.reverse) @@ -233,6 +233,7 @@ final class NonEmptyVector[+A] private (val toVector: Vector[A]) extends AnyVal new NonEmptyVector(toVector.sorted(AA.toOrdering)) } +@suppressUnusedImportWarningForScalaVersionSpecific sealed abstract private[data] class NonEmptyVectorInstances { implicit val catsDataInstancesForNonEmptyVector: SemigroupK[NonEmptyVector] diff --git a/core/src/main/scala/cats/data/OneAnd.scala b/core/src/main/scala/cats/data/OneAnd.scala index 9ddeb13a12..e48cc6a4bc 100644 --- a/core/src/main/scala/cats/data/OneAnd.scala +++ b/core/src/main/scala/cats/data/OneAnd.scala @@ -104,6 +104,7 @@ final case class OneAnd[F[_], A](head: A, tail: F[A]) { s"OneAnd(${A.show(head)}, ${FA.show(tail)})" } +@suppressUnusedImportWarningForScalaVersionSpecific sealed abstract private[data] class OneAndInstances extends OneAndLowPriority0 { implicit def catsDataParallelForOneAnd[A, M[_]: Alternative, F[_]: Alternative]( diff --git a/core/src/main/scala/cats/data/ZipList.scala b/core/src/main/scala/cats/data/ZipList.scala index 8bca9d7d31..5ea15a00f5 100644 --- a/core/src/main/scala/cats/data/ZipList.scala +++ b/core/src/main/scala/cats/data/ZipList.scala @@ -1,12 +1,16 @@ -package cats.data +package cats +package data + +import instances.list.catsKernelStdEqForList +import kernel.compat.scalaVersionSpecific._ -import cats.{CommutativeApply, Eq} -import cats.instances.list.catsKernelStdEqForList class ZipList[A](val value: List[A]) extends AnyVal +@suppressUnusedImportWarningForScalaVersionSpecific object ZipList { + def apply[A](value: List[A]): ZipList[A] = new ZipList(value) implicit val catsDataCommutativeApplyForZipList: CommutativeApply[ZipList] = new CommutativeApply[ZipList] { @@ -15,7 +19,7 @@ object ZipList { ZipList(fa.value.map(f)) def ap[A, B](ff: ZipList[A => B])(fa: ZipList[A]): ZipList[B] = - ZipList((ff.value, fa.value).zipped.map(_.apply(_))) + ZipList(ff.value.lazyZip(fa.value).map(_.apply(_))) override def product[A, B](fa: ZipList[A], fb: ZipList[B]): ZipList[(A, B)] = ZipList(fa.value.zip(fb.value)) @@ -24,3 +28,5 @@ object ZipList { implicit def catsDataEqForZipList[A: Eq]: Eq[ZipList[A]] = Eq.by(_.value) } + + diff --git a/core/src/main/scala/cats/data/ZipStream.scala b/core/src/main/scala/cats/data/ZipStream.scala index 832af5ce2e..d729434857 100644 --- a/core/src/main/scala/cats/data/ZipStream.scala +++ b/core/src/main/scala/cats/data/ZipStream.scala @@ -6,6 +6,7 @@ import kernel.compat.scalaVersionSpecific._ class ZipStream[A](val value: LazyList[A]) extends AnyVal +@suppressUnusedImportWarningForScalaVersionSpecific object ZipStream { def apply[A](value: LazyList[A]): ZipStream[A] = new ZipStream(value) @@ -18,7 +19,7 @@ object ZipStream { ZipStream(fa.value.map(f)) def ap[A, B](ff: ZipStream[A => B])(fa: ZipStream[A]): ZipStream[B] = - ZipStream((ff.value, fa.value).zipped.map(_.apply(_))) + ZipStream(ff.value.lazyZip(fa.value).map(_.apply(_))) override def product[A, B](fa: ZipStream[A], fb: ZipStream[B]): ZipStream[(A, B)] = ZipStream(fa.value.zip(fb.value)) diff --git a/core/src/main/scala/cats/data/ZipVector.scala b/core/src/main/scala/cats/data/ZipVector.scala index 02153b4973..8aef78d12e 100644 --- a/core/src/main/scala/cats/data/ZipVector.scala +++ b/core/src/main/scala/cats/data/ZipVector.scala @@ -1,10 +1,13 @@ -package cats.data +package cats +package data + +import instances.vector._ +import kernel.compat.scalaVersionSpecific._ -import cats.{CommutativeApply, Eq} -import cats.instances.vector._ class ZipVector[A](val value: Vector[A]) extends AnyVal +@suppressUnusedImportWarningForScalaVersionSpecific object ZipVector { def apply[A](value: Vector[A]): ZipVector[A] = new ZipVector(value) @@ -14,7 +17,7 @@ object ZipVector { override def map[A, B](fa: ZipVector[A])(f: (A) => B): ZipVector[B] = ZipVector(fa.value.map(f)) def ap[A, B](ff: ZipVector[A => B])(fa: ZipVector[A]): ZipVector[B] = - ZipVector((ff.value, fa.value).zipped.map(_.apply(_))) + ZipVector(ff.value.lazyZip(fa.value).map(_.apply(_))) } diff --git a/core/src/main/scala/cats/data/package.scala b/core/src/main/scala/cats/data/package.scala index 09473cbba7..bbda18e522 100644 --- a/core/src/main/scala/cats/data/package.scala +++ b/core/src/main/scala/cats/data/package.scala @@ -1,6 +1,8 @@ package cats import kernel.compat.scalaVersionSpecific._ import compat.lazyList.toLazyList + + package object data { type NonEmptyStream[A] = OneAnd[LazyList, A] @@ -15,6 +17,8 @@ package object data { def NonEmptyStream[A](head: A, tail: LazyList[A] = LazyList.empty): NonEmptyStream[A] = OneAnd(head, tail) + + @suppressUnusedImportWarningForScalaVersionSpecific def NonEmptyStream[A](head: A, tail: A*): NonEmptyStream[A] = OneAnd(head, toLazyList(tail)) diff --git a/core/src/main/scala/cats/instances/either.scala b/core/src/main/scala/cats/instances/either.scala index 79ae373c68..b6eabe0c05 100644 --- a/core/src/main/scala/cats/instances/either.scala +++ b/core/src/main/scala/cats/instances/either.scala @@ -35,7 +35,7 @@ trait EitherInstances extends cats.kernel.instances.EitherInstances { def pure[B](b: B): Either[A, B] = Right(b) def flatMap[B, C](fa: Either[A, B])(f: B => Either[A, C]): Either[A, C] = - fa.right.flatMap(f) + fa.flatMap(f) def handleErrorWith[B](fea: Either[A, B])(f: A => Either[A, B]): Either[A, B] = fea match { @@ -46,7 +46,7 @@ trait EitherInstances extends cats.kernel.instances.EitherInstances { def raiseError[B](e: A): Either[A, B] = Left(e) override def map[B, C](fa: Either[A, B])(f: B => C): Either[A, C] = - fa.right.map(f) + fa.map(f) @tailrec def tailRecM[B, C](b: B)(f: B => Either[A, Either[B, C]]): Either[A, C] = @@ -63,7 +63,7 @@ trait EitherInstances extends cats.kernel.instances.EitherInstances { override def map2Eval[B, C, Z](fb: Either[A, B], fc: Eval[Either[A, C]])(f: (B, C) => Z): Eval[Either[A, Z]] = fb match { case l @ Left(_) => Now(EitherUtil.rightCast(l)) - case Right(b) => fc.map(_.right.map(f(b, _))) + case Right(b) => fc.map(_.map(f(b, _))) } def traverse[F[_], B, C](fa: Either[A, B])(f: B => F[C])(implicit F: Applicative[F]): F[Either[A, C]] = @@ -103,18 +103,18 @@ trait EitherInstances extends cats.kernel.instances.EitherInstances { fab.ensureOr(error)(predicate) override def reduceLeftToOption[B, C](fab: Either[A, B])(f: B => C)(g: (C, B) => C): Option[C] = - fab.right.map(f).toOption + fab.map(f).toOption override def reduceRightToOption[B, C]( fab: Either[A, B] )(f: B => C)(g: (B, Eval[C]) => Eval[C]): Eval[Option[C]] = - Now(fab.right.map(f).toOption) + Now(fab.map(f).toOption) override def reduceLeftOption[B](fab: Either[A, B])(f: (B, B) => B): Option[B] = - fab.right.toOption + fab.toOption override def reduceRightOption[B](fab: Either[A, B])(f: (B, Eval[B]) => Eval[B]): Eval[Option[B]] = - Now(fab.right.toOption) + Now(fab.toOption) override def size[B](fab: Either[A, B]): Long = fab.fold(_ => 0L, _ => 1L) @@ -129,10 +129,10 @@ trait EitherInstances extends cats.kernel.instances.EitherInstances { fab.fold(_ => None, r => if (f(r)) Some(r) else None) override def exists[B](fab: Either[A, B])(p: B => Boolean): Boolean = - fab.right.exists(p) + fab.exists(p) override def forall[B](fab: Either[A, B])(p: B => Boolean): Boolean = - fab.right.forall(p) + fab.forall(p) override def toList[B](fab: Either[A, B]): List[B] = fab.fold(_ => Nil, _ :: Nil) diff --git a/core/src/main/scala/cats/instances/parallel.scala b/core/src/main/scala/cats/instances/parallel.scala index d1a5547ca3..1100d22c84 100644 --- a/core/src/main/scala/cats/instances/parallel.scala +++ b/core/src/main/scala/cats/instances/parallel.scala @@ -7,6 +7,7 @@ import cats.syntax.either._ import cats.{~>, Applicative, Apply, FlatMap, Functor, Monad, NonEmptyParallel, Parallel} import kernel.compat.scalaVersionSpecific._ +@suppressUnusedImportWarningForScalaVersionSpecific trait ParallelInstances extends ParallelInstances1 { implicit def catsParallelForEitherValidated[E: Semigroup]: Parallel[Either[E, ?], Validated[E, ?]] = new Parallel[Either[E, ?], Validated[E, ?]] { diff --git a/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala b/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala index 61778d1b90..eeaae161e4 100644 --- a/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala +++ b/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala @@ -17,6 +17,7 @@ import scala.util.Random import compat.scalaVersionSpecific._ import java.util.UUID import java.util.concurrent.TimeUnit.{DAYS, HOURS, MICROSECONDS, MILLISECONDS, MINUTES, NANOSECONDS, SECONDS} +import compat.scalaVersionSpecific._ object KernelCheck { @@ -319,19 +320,19 @@ class Tests extends AnyFunSuiteLike with Discipline { eqv.eqv(po.partialComparison(Set(1, 2), Set(2, 3)), None) } - test("signum . toInt . comparison = signum . compare") { + test("sign . toInt . comparison = sign . compare") { check { (i: Int, j: Int) => val found = Order[Int].comparison(i, j) val expected = Order[Int].compare(i, j) - Eq[Int].eqv(found.toInt.signum, expected.signum) + Eq[Int].eqv(found.toInt.sign, expected.sign) } } - test("signum . toDouble . partialComparison = signum . partialCompare") { + test("sign . toDouble . partialComparison = sign . partialCompare") { check { (x: Set[Int], y: Set[Int]) => - val found = subsetPartialOrder[Int].partialComparison(x, y).map(_.toDouble.signum) - val expected = Some(subsetPartialOrder[Int].partialCompare(x, y)).filter(d => !d.isNaN).map(_.signum) - Eq[Option[Int]].eqv(found, expected) + val found = subsetPartialOrder[Int].partialComparison(x, y).map(_.toDouble.sign) + val expected = Some(subsetPartialOrder[Int].partialCompare(x, y)).filter(d => !d.isNaN).map(_.sign) + Eq[Option[Double]].eqv(found, expected) } } diff --git a/kernel/src/main/scala-2.12-/cats/kernel/compat/scalaVersionSpecific.scala b/kernel/src/main/scala-2.12-/cats/kernel/compat/scalaVersionSpecific.scala index 912c3f21f7..f1593c92d1 100644 --- a/kernel/src/main/scala-2.12-/cats/kernel/compat/scalaVersionSpecific.scala +++ b/kernel/src/main/scala-2.12-/cats/kernel/compat/scalaVersionSpecific.scala @@ -1,11 +1,31 @@ package cats.kernel.compat +import scala.annotation.{Annotation, StaticAnnotation} +import scala.collection.{IterableLike, TraversableLike} private[cats] object scalaVersionSpecific { + + class suppressUnusedImportWarningForScalaVersionSpecific extends Annotation with StaticAnnotation + type LazyList[+A] = Stream[A] val LazyList = Stream type IterableOnce[+A] = TraversableOnce[A] implicit class traversableOnceExtension[A](private val to: TraversableOnce[A]) extends AnyVal { def iterator: Iterator[A] = to.toIterator + + } + + implicit class doubleExtension(private val double: Double) extends AnyVal { + def sign: Double = if(double.isNaN) Double.NaN else double.signum.toDouble + } + implicit class intExtension(private val i: Int) extends AnyVal { + def sign: Int = i.signum + } + + implicit class lazyZipExtension[A](private val a: A) extends AnyVal { + def lazyZip[El1, Repr1, El2, Repr2, T](that: T) + (implicit w1: A => TraversableLike[El1, Repr1], + w2: T => IterableLike[El2, Repr2]) = (a, that).zipped + } } diff --git a/kernel/src/main/scala-2.13+/cats/kernel/compat/HashCompat.scala b/kernel/src/main/scala-2.13+/cats/kernel/compat/HashCompat.scala index 36b10dcce5..b941d71b58 100644 --- a/kernel/src/main/scala-2.13+/cats/kernel/compat/HashCompat.scala +++ b/kernel/src/main/scala-2.13+/cats/kernel/compat/HashCompat.scala @@ -60,7 +60,7 @@ private[kernel] class HashCompat { } // adapted from scala.util.hashing.MurmurHash3 - def orderedHash[A](xs: TraversableOnce[A])(implicit A: Hash[A]): Int = { + def orderedHash[A](xs: IterableOnce[A])(implicit A: Hash[A]): Int = { import scala.util.hashing.MurmurHash3.{finalizeHash, mix, seqSeed} val it = xs.iterator diff --git a/kernel/src/main/scala-2.13+/cats/kernel/compat/scalaVersionSpecific.scala b/kernel/src/main/scala-2.13+/cats/kernel/compat/scalaVersionSpecific.scala index ef8a0ffd5c..21b74ad025 100644 --- a/kernel/src/main/scala-2.13+/cats/kernel/compat/scalaVersionSpecific.scala +++ b/kernel/src/main/scala-2.13+/cats/kernel/compat/scalaVersionSpecific.scala @@ -2,9 +2,15 @@ package cats package kernel package compat +import scala.annotation.{Annotation, StaticAnnotation} private[cats] object scalaVersionSpecific { - type LazyList[+A] = scala.LazyList[A] //this is needed only to avoid unused import warnings on Scala 2.13 + + /** + * a trick to suppress unused import warning for this object + */ + class suppressUnusedImportWarningForScalaVersionSpecific extends Annotation with StaticAnnotation + type IterableOnce[+A] = scala.IterableOnce[A] //this is needed only to avoid unused import warnings on Scala 2.13 implicit class iterableOnceExtension[A](private val io: IterableOnce[A]) extends AnyVal { diff --git a/kernel/src/main/scala/cats/kernel/Eq.scala b/kernel/src/main/scala/cats/kernel/Eq.scala index bbc8110ffa..b7b1545363 100644 --- a/kernel/src/main/scala/cats/kernel/Eq.scala +++ b/kernel/src/main/scala/cats/kernel/Eq.scala @@ -3,8 +3,7 @@ package cats.kernel import scala.{specialized => sp} import scala.math.Equiv -import compat.scalaVersionSpecific._ - +//import compat.scalaVersionSpecific._ /** * A type class used to determine equality between 2 instances of the same * type. Any 2 instances `x` and `y` are equal if `eqv(x, y)` is `true`. @@ -24,7 +23,6 @@ trait Eq[@sp A] extends Any with Serializable { self => } abstract class EqFunctions[E[T] <: Eq[T]] { - def eqv[@sp A](x: A, y: A)(implicit ev: E[A]): Boolean = ev.eqv(x, y) @@ -33,8 +31,8 @@ abstract class EqFunctions[E[T] <: Eq[T]] { } +//@suppressUnusedImportWarning trait EqToEquivConversion { - /** * Implicitly derive a `scala.math.Equiv[A]` from a `Eq[A]` * instance. @@ -112,9 +110,9 @@ object Eq extends EqFunctions[Eq] with EqToEquivConversion { def empty = allEqual[A] def combine(e1: Eq[A], e2: Eq[A]): Eq[A] = Eq.and(e1, e2) override def combineAllOption(es: IterableOnce[Eq[A]]): Option[Eq[A]] = - if (es.isEmpty) None + if (es.iterator.isEmpty) None else { - val materialized = es.toVector + val materialized = es.iterator.toVector Some(new Eq[A] { def eqv(x: A, y: A) = materialized.forall(_.eqv(x, y)) }) @@ -128,9 +126,9 @@ object Eq extends EqFunctions[Eq] with EqToEquivConversion { def anyEqualSemilattice[A]: Semilattice[Eq[A]] = new Semilattice[Eq[A]] { def combine(e1: Eq[A], e2: Eq[A]): Eq[A] = Eq.or(e1, e2) override def combineAllOption(es: IterableOnce[Eq[A]]): Option[Eq[A]] = - if (es.isEmpty) None + if (es.iterator.isEmpty) None else { - val materialized = es.toVector + val materialized = es.iterator.toVector Some(new Eq[A] { def eqv(x: A, y: A) = materialized.exists(_.eqv(x, y)) }) diff --git a/kernel/src/main/scala/cats/kernel/Monoid.scala b/kernel/src/main/scala/cats/kernel/Monoid.scala index 8fc0c49923..5ea82786a2 100644 --- a/kernel/src/main/scala/cats/kernel/Monoid.scala +++ b/kernel/src/main/scala/cats/kernel/Monoid.scala @@ -79,10 +79,10 @@ trait Monoid[@sp(Int, Long, Float, Double) A] extends Any with Semigroup[A] { * }}} */ def combineAll(as: IterableOnce[A]): A = - as.foldLeft(empty)(combine) + as.iterator.foldLeft(empty)(combine) override def combineAllOption(as: IterableOnce[A]): Option[A] = - if (as.isEmpty) None else Some(combineAll(as)) + if (as.iterator.isEmpty) None else Some(combineAll(as)) } abstract class MonoidFunctions[M[T] <: Monoid[T]] extends SemigroupFunctions[M] { diff --git a/kernel/src/main/scala/cats/kernel/PartialOrder.scala b/kernel/src/main/scala/cats/kernel/PartialOrder.scala index 565990929f..9eb7df1404 100644 --- a/kernel/src/main/scala/cats/kernel/PartialOrder.scala +++ b/kernel/src/main/scala/cats/kernel/PartialOrder.scala @@ -2,7 +2,7 @@ package cats.kernel import java.lang.Double.isNaN import scala.{specialized => sp} - +import compat.scalaVersionSpecific._ /** * The `PartialOrder` type class is used to define a partial ordering on some type `A`. * @@ -50,8 +50,8 @@ trait PartialOrder[@sp A] extends Any with Eq[A] { self => * - positive iff `x > y` */ def tryCompare(x: A, y: A): Option[Int] = { - val c = partialCompare(x, y) - if (isNaN(c)) None else Some(c.signum) + val c = partialCompare(x, y).sign + if (isNaN(c)) None else Some(c.toInt) } /** @@ -124,6 +124,7 @@ abstract class PartialOrderFunctions[P[T] <: PartialOrder[T]] extends EqFunction ev.gt(x, y) } +@suppressUnusedImportWarningForScalaVersionSpecific object PartialOrder extends PartialOrderFunctions[PartialOrder] with PartialOrderToPartialOrderingConversion { /** diff --git a/kernel/src/main/scala/cats/kernel/instances/MapInstances.scala b/kernel/src/main/scala/cats/kernel/instances/MapInstances.scala index 436f446cd3..f2e38eeb8f 100644 --- a/kernel/src/main/scala/cats/kernel/instances/MapInstances.scala +++ b/kernel/src/main/scala/cats/kernel/instances/MapInstances.scala @@ -75,7 +75,7 @@ class MapMonoid[K, V](implicit V: Semigroup[V]) extends Monoid[Map[K, V]] { override def combineAll(xss: IterableOnce[Map[K, V]]): Map[K, V] = { val acc = mutable.Map.empty[K, V] - xss.foreach { m => + xss.iterator.foreach { m => val it = m.iterator while (it.hasNext) { val (k, v) = it.next diff --git a/kernel/src/main/scala/cats/kernel/instances/StaticMethods.scala b/kernel/src/main/scala/cats/kernel/instances/StaticMethods.scala index b27c441c69..7578463229 100644 --- a/kernel/src/main/scala/cats/kernel/instances/StaticMethods.scala +++ b/kernel/src/main/scala/cats/kernel/instances/StaticMethods.scala @@ -77,7 +77,7 @@ object StaticMethods extends cats.kernel.compat.HashCompat { } def combineAllIterable[A, R](b: mutable.Builder[A, R], xs: IterableOnce[Iterable[A]]): R = { - xs.foreach(b ++= _) + xs.iterator.foreach(b ++= _) b.result } diff --git a/kernel/src/main/scala/cats/kernel/instances/StringInstances.scala b/kernel/src/main/scala/cats/kernel/instances/StringInstances.scala index 2919b29e4a..28feead4fc 100644 --- a/kernel/src/main/scala/cats/kernel/instances/StringInstances.scala +++ b/kernel/src/main/scala/cats/kernel/instances/StringInstances.scala @@ -23,7 +23,7 @@ class StringMonoid extends Monoid[String] { override def combineAll(xs: IterableOnce[String]): String = { val sb = new StringBuilder - xs.foreach(sb.append) + xs.iterator.foreach(sb.append) sb.toString } } diff --git a/kernel/src/main/scala/cats/kernel/instances/UnitInstances.scala b/kernel/src/main/scala/cats/kernel/instances/UnitInstances.scala index f8f58a8881..443f02a9ee 100644 --- a/kernel/src/main/scala/cats/kernel/instances/UnitInstances.scala +++ b/kernel/src/main/scala/cats/kernel/instances/UnitInstances.scala @@ -33,5 +33,5 @@ class UnitAlgebra extends BoundedSemilattice[Unit] with CommutativeGroup[Unit] { def inverse(x: Unit): Unit = () override protected[this] def repeatedCombineN(a: Unit, n: Int): Unit = () override def combineAllOption(as: IterableOnce[Unit]): Option[Unit] = - if (as.isEmpty) None else Some(()) + if (as.iterator.isEmpty) None else Some(()) } diff --git a/laws/src/main/scala/cats/laws/discipline/Arbitrary.scala b/laws/src/main/scala/cats/laws/discipline/Arbitrary.scala index 78d0ccf209..d805e72ad6 100644 --- a/laws/src/main/scala/cats/laws/discipline/Arbitrary.scala +++ b/laws/src/main/scala/cats/laws/discipline/Arbitrary.scala @@ -13,6 +13,7 @@ import org.scalacheck.Arbitrary.{arbitrary => getArbitrary} /** * Arbitrary instances for cats.data */ +@suppressUnusedImportWarningForScalaVersionSpecific object arbitrary extends ArbitraryInstances0 { // this instance is not available in ScalaCheck 1.13.2. diff --git a/tests/src/test/scala/cats/tests/BinCodecInvariantMonoidalSuite.scala b/tests/src/test/scala/cats/tests/BinCodecInvariantMonoidalSuite.scala index 3ab0551935..b14ca1443f 100644 --- a/tests/src/test/scala/cats/tests/BinCodecInvariantMonoidalSuite.scala +++ b/tests/src/test/scala/cats/tests/BinCodecInvariantMonoidalSuite.scala @@ -10,6 +10,7 @@ import cats.kernel.laws.discipline.{MonoidTests, SemigroupTests} import org.scalacheck.{Arbitrary, Gen} import kernel.compat.scalaVersionSpecific._ +@suppressUnusedImportWarningForScalaVersionSpecific object BinCodecInvariantMonoidalSuite { final case class MiniList[+A] private (val toList: List[A]) extends AnyVal { import MiniList.truncated diff --git a/tests/src/test/scala/cats/tests/EitherTSuite.scala b/tests/src/test/scala/cats/tests/EitherTSuite.scala index 1cef0fea24..03919d11a9 100644 --- a/tests/src/test/scala/cats/tests/EitherTSuite.scala +++ b/tests/src/test/scala/cats/tests/EitherTSuite.scala @@ -445,7 +445,7 @@ class EitherTSuite extends CatsSuite { test("toEither consistent with toOption") { forAll { (x: EitherT[List, String, Int]) => - x.value.map(_.right.toOption) should ===(x.toOption.value) + x.value.map(_.toOption) should ===(x.toOption.value) } } diff --git a/tests/src/test/scala/cats/tests/FoldableSuite.scala b/tests/src/test/scala/cats/tests/FoldableSuite.scala index 695340b392..9a5010670b 100644 --- a/tests/src/test/scala/cats/tests/FoldableSuite.scala +++ b/tests/src/test/scala/cats/tests/FoldableSuite.scala @@ -10,6 +10,7 @@ import cats.laws.discipline.arbitrary._ import kernel.compat.scalaVersionSpecific._ import compat.lazyList.toLazyList +@suppressUnusedImportWarningForScalaVersionSpecific abstract class FoldableSuite[F[_]: Foldable](name: String)(implicit ArbFInt: Arbitrary[F[Int]], ArbFString: Arbitrary[F[String]]) extends CatsSuite { @@ -477,7 +478,7 @@ class FoldableOptionSuite extends FoldableSuite[Option]("option") { } class FoldableEitherSuite extends FoldableSuite[Either[Int, ?]]("either") { - def iterator[T](either: Either[Int, T]): Iterator[T] = either.right.toOption.iterator + def iterator[T](either: Either[Int, T]): Iterator[T] = either.toOption.iterator } class FoldableValidatedSuite extends FoldableSuite[Validated[String, ?]]("validated") { diff --git a/tests/src/test/scala/cats/tests/LazyListSuite.scala b/tests/src/test/scala/cats/tests/LazyListSuite.scala index fcfd99dd4b..c1581eb7df 100644 --- a/tests/src/test/scala/cats/tests/LazyListSuite.scala +++ b/tests/src/test/scala/cats/tests/LazyListSuite.scala @@ -16,6 +16,7 @@ import cats.laws.discipline.arbitrary._ import kernel.compat.scalaVersionSpecific._ import compat.lazyList.lazyListString +@suppressUnusedImportWarningForScalaVersionSpecific class LazyListSuite extends CatsSuite { checkAll("LazyList[Int]", SemigroupalTests[LazyList].semigroupal[Int, Int, Int]) checkAll("Semigroupal[LazyList]", SerializableTests.serializable(Semigroupal[LazyList])) diff --git a/tests/src/test/scala/cats/tests/NonEmptyListSuite.scala b/tests/src/test/scala/cats/tests/NonEmptyListSuite.scala index e3d21e049c..d15198da58 100644 --- a/tests/src/test/scala/cats/tests/NonEmptyListSuite.scala +++ b/tests/src/test/scala/cats/tests/NonEmptyListSuite.scala @@ -152,9 +152,12 @@ class NonEmptyListSuite extends CatsSuite { test("reduceRight consistent with foldRight") { forAll { (nel: NonEmptyList[Int], f: (Int, Eval[Int]) => Eval[Int]) => val got = nel.reduceRight(f).value - val last :: rev = nel.toList.reverse - val expected = rev.reverse.foldRight(last)((a, b) => f(a, Now(b)).value) - got should ===(expected) + nel.toList.reverse match { + case last :: rev => val expected = rev.reverse.foldRight(last)((a, b) => f(a, Now(b)).value) + got should ===(expected) + case _ => fail("nonempty turns out to be empty") + } + } } @@ -182,11 +185,15 @@ class NonEmptyListSuite extends CatsSuite { test("reduceRightToOption consistent with foldRight + Option") { forAll { (nel: NonEmptyList[Int], f: Int => String, g: (Int, Eval[String]) => Eval[String]) => val got = nel.reduceRightToOption(f)(g).value - val last :: rev = nel.toList.reverse - val expected = rev.reverse.foldRight(Option(f(last))) { (i, opt) => - opt.map(s => g(i, Now(s)).value) + nel.toList.reverse match { + case last :: rev => + val expected = rev.reverse.foldRight(Option(f(last))) { (i, opt) => + opt.map(s => g(i, Now(s)).value) + } + got should ===(expected) + case _ => fail("nonempty turns out to be empty") } - got should ===(expected) + } } diff --git a/tests/src/test/scala/cats/tests/OneAndSuite.scala b/tests/src/test/scala/cats/tests/OneAndSuite.scala index fa16fbb826..1d0d80031e 100644 --- a/tests/src/test/scala/cats/tests/OneAndSuite.scala +++ b/tests/src/test/scala/cats/tests/OneAndSuite.scala @@ -21,6 +21,8 @@ import cats.laws.discipline.{ import cats.laws.discipline.arbitrary._ import kernel.compat.scalaVersionSpecific._ import compat.lazyList.toLazyList + +@suppressUnusedImportWarningForScalaVersionSpecific class OneAndSuite extends CatsSuite { // Lots of collections here.. telling ScalaCheck to calm down a bit implicit override val generatorDrivenConfig: PropertyCheckConfiguration = @@ -172,9 +174,13 @@ class OneAndSuite extends CatsSuite { test("reduceRight consistent with foldRight") { forAll { (nel: NonEmptyStream[Int], f: (Int, Eval[Int]) => Eval[Int]) => val got = nel.reduceRight(f).value - val last :: rev = nel.unwrap.toList.reverse - val expected = rev.reverse.foldRight(last)((a, b) => f(a, Now(b)).value) - got should ===(expected) + nel.unwrap.toList.reverse match { + case last :: rev => + val expected = rev.reverse.foldRight(last)((a, b) => f(a, Now(b)).value) + got should ===(expected) + case _ => fail("nonempty turns out to be empty") + } + } } @@ -202,11 +208,14 @@ class OneAndSuite extends CatsSuite { test("reduceRightToOption consistent with foldRight + Option") { forAll { (nel: NonEmptyStream[Int], f: Int => String, g: (Int, Eval[String]) => Eval[String]) => val got = nel.reduceRightToOption(f)(g).value - val last :: rev = nel.unwrap.toList.reverse - val expected = rev.reverse.foldRight(Option(f(last))) { (i, opt) => - opt.map(s => g(i, Now(s)).value) + nel.unwrap.toList.reverse match { + case last :: rev => + val expected = rev.reverse.foldRight(Option(f(last))) { (i, opt) => + opt.map(s => g(i, Now(s)).value) + } + got should ===(expected) + case _ => fail("nonempty turns out to be empty") } - got should ===(expected) } } diff --git a/tests/src/test/scala/cats/tests/ParallelSuite.scala b/tests/src/test/scala/cats/tests/ParallelSuite.scala index ef16ea2c3a..bbfcb27d5a 100644 --- a/tests/src/test/scala/cats/tests/ParallelSuite.scala +++ b/tests/src/test/scala/cats/tests/ParallelSuite.scala @@ -13,6 +13,7 @@ import org.typelevel.discipline.scalatest.Discipline import scala.collection.immutable.SortedSet import kernel.compat.scalaVersionSpecific._ +@suppressUnusedImportWarningForScalaVersionSpecific class ParallelSuite extends CatsSuite with ApplicativeErrorForEitherTest { test("ParSequence Either should accumulate errors") { diff --git a/tests/src/test/scala/cats/tests/RegressionSuite.scala b/tests/src/test/scala/cats/tests/RegressionSuite.scala index b90cd7226d..b5219b1199 100644 --- a/tests/src/test/scala/cats/tests/RegressionSuite.scala +++ b/tests/src/test/scala/cats/tests/RegressionSuite.scala @@ -6,6 +6,7 @@ import scala.collection.mutable import scala.collection.immutable.SortedMap import kernel.compat.scalaVersionSpecific._ +@suppressUnusedImportWarningForScalaVersionSpecific class RegressionSuite extends CatsSuite { // toy state class @@ -161,6 +162,6 @@ class RegressionSuite extends CatsSuite { test("#2809 MonadErrorOps.reject runs effects only once") { val program = StateT.modify[Either[Throwable, ?], Int](_ + 1).reject { case _ if false => new Throwable } - program.runS(0).right.get should ===(1) + program.runS(0).toOption should ===(Some(1)) } } diff --git a/tests/src/test/scala/cats/tests/TraverseSuite.scala b/tests/src/test/scala/cats/tests/TraverseSuite.scala index 4a2612926c..d2f65ae8ae 100644 --- a/tests/src/test/scala/cats/tests/TraverseSuite.scala +++ b/tests/src/test/scala/cats/tests/TraverseSuite.scala @@ -7,6 +7,7 @@ import cats.instances.all._ import kernel.compat.scalaVersionSpecific._ import compat.lazyList.toLazyList +@suppressUnusedImportWarningForScalaVersionSpecific abstract class TraverseSuite[F[_]: Traverse](name: String)(implicit ArbFInt: Arbitrary[F[Int]]) extends CatsSuite { test(s"Traverse[$name].zipWithIndex") { From bc84daace269da5bc039ce970cc1571378c5dc6f Mon Sep 17 00:00:00 2001 From: Kailuo Wang Date: Wed, 26 Jun 2019 15:02:23 -0400 Subject: [PATCH 2/6] use a trick to remove unused import warnings --- core/src/main/scala/cats/Invariant.scala | 1 + .../cats/kernel/compat/scalaVersionSpecific.scala | 4 +++- .../cats/kernel/compat/scalaVersionSpecific.scala | 2 -- kernel/src/main/scala/cats/kernel/Eq.scala | 6 +++--- kernel/src/main/scala/cats/kernel/Monoid.scala | 1 + .../main/scala/cats/kernel/instances/ListInstances.scala | 1 + .../src/main/scala/cats/kernel/instances/MapInstances.scala | 1 + .../main/scala/cats/kernel/instances/QueueInstances.scala | 1 + .../main/scala/cats/kernel/instances/StaticMethods.scala | 2 ++ .../main/scala/cats/kernel/instances/StreamInstances.scala | 2 ++ .../main/scala/cats/kernel/instances/StringInstances.scala | 2 ++ .../main/scala/cats/kernel/instances/UnitInstances.scala | 1 + .../main/scala/cats/kernel/instances/VectorInstances.scala | 1 + 13 files changed, 19 insertions(+), 6 deletions(-) diff --git a/core/src/main/scala/cats/Invariant.scala b/core/src/main/scala/cats/Invariant.scala index 313d8dad35..0c391c1e88 100644 --- a/core/src/main/scala/cats/Invariant.scala +++ b/core/src/main/scala/cats/Invariant.scala @@ -44,6 +44,7 @@ import cats.kernel.compat.scalaVersionSpecific._ } } +@suppressUnusedImportWarningForScalaVersionSpecific object Invariant { implicit val catsInvariantMonoid: Invariant[Monoid] = new Invariant[Monoid] { diff --git a/kernel/src/main/scala-2.12-/cats/kernel/compat/scalaVersionSpecific.scala b/kernel/src/main/scala-2.12-/cats/kernel/compat/scalaVersionSpecific.scala index f1593c92d1..aeb0c3999b 100644 --- a/kernel/src/main/scala-2.12-/cats/kernel/compat/scalaVersionSpecific.scala +++ b/kernel/src/main/scala-2.12-/cats/kernel/compat/scalaVersionSpecific.scala @@ -3,7 +3,9 @@ import scala.annotation.{Annotation, StaticAnnotation} import scala.collection.{IterableLike, TraversableLike} private[cats] object scalaVersionSpecific { - + /** + * a trick to suppress unused import warning for this object + */ class suppressUnusedImportWarningForScalaVersionSpecific extends Annotation with StaticAnnotation type LazyList[+A] = Stream[A] diff --git a/kernel/src/main/scala-2.13+/cats/kernel/compat/scalaVersionSpecific.scala b/kernel/src/main/scala-2.13+/cats/kernel/compat/scalaVersionSpecific.scala index 21b74ad025..ed2415a255 100644 --- a/kernel/src/main/scala-2.13+/cats/kernel/compat/scalaVersionSpecific.scala +++ b/kernel/src/main/scala-2.13+/cats/kernel/compat/scalaVersionSpecific.scala @@ -11,8 +11,6 @@ private[cats] object scalaVersionSpecific { */ class suppressUnusedImportWarningForScalaVersionSpecific extends Annotation with StaticAnnotation - type IterableOnce[+A] = scala.IterableOnce[A] //this is needed only to avoid unused import warnings on Scala 2.13 - implicit class iterableOnceExtension[A](private val io: IterableOnce[A]) extends AnyVal { def reduceOption(f: (A, A) => A): Option[A] = io.iterator.reduceOption(f) } diff --git a/kernel/src/main/scala/cats/kernel/Eq.scala b/kernel/src/main/scala/cats/kernel/Eq.scala index b7b1545363..e124b4bba5 100644 --- a/kernel/src/main/scala/cats/kernel/Eq.scala +++ b/kernel/src/main/scala/cats/kernel/Eq.scala @@ -1,9 +1,9 @@ package cats.kernel import scala.{specialized => sp} - import scala.math.Equiv -//import compat.scalaVersionSpecific._ +import compat.scalaVersionSpecific._ + /** * A type class used to determine equality between 2 instances of the same * type. Any 2 instances `x` and `y` are equal if `eqv(x, y)` is `true`. @@ -31,7 +31,6 @@ abstract class EqFunctions[E[T] <: Eq[T]] { } -//@suppressUnusedImportWarning trait EqToEquivConversion { /** * Implicitly derive a `scala.math.Equiv[A]` from a `Eq[A]` @@ -42,6 +41,7 @@ trait EqToEquivConversion { } } +@suppressUnusedImportWarningForScalaVersionSpecific object Eq extends EqFunctions[Eq] with EqToEquivConversion { /** diff --git a/kernel/src/main/scala/cats/kernel/Monoid.scala b/kernel/src/main/scala/cats/kernel/Monoid.scala index 5ea82786a2..b89aa37400 100644 --- a/kernel/src/main/scala/cats/kernel/Monoid.scala +++ b/kernel/src/main/scala/cats/kernel/Monoid.scala @@ -85,6 +85,7 @@ trait Monoid[@sp(Int, Long, Float, Double) A] extends Any with Semigroup[A] { if (as.iterator.isEmpty) None else Some(combineAll(as)) } +@suppressUnusedImportWarningForScalaVersionSpecific abstract class MonoidFunctions[M[T] <: Monoid[T]] extends SemigroupFunctions[M] { def empty[@sp(Int, Long, Float, Double) A](implicit ev: M[A]): A = ev.empty diff --git a/kernel/src/main/scala/cats/kernel/instances/ListInstances.scala b/kernel/src/main/scala/cats/kernel/instances/ListInstances.scala index 7712539d03..ddc79a4f13 100644 --- a/kernel/src/main/scala/cats/kernel/instances/ListInstances.scala +++ b/kernel/src/main/scala/cats/kernel/instances/ListInstances.scala @@ -4,6 +4,7 @@ package instances import scala.annotation.tailrec import compat.scalaVersionSpecific._ +@suppressUnusedImportWarningForScalaVersionSpecific trait ListInstances extends ListInstances1 { implicit def catsKernelStdOrderForList[A: Order]: Order[List[A]] = new ListOrder[A] diff --git a/kernel/src/main/scala/cats/kernel/instances/MapInstances.scala b/kernel/src/main/scala/cats/kernel/instances/MapInstances.scala index f2e38eeb8f..7d22ab29c1 100644 --- a/kernel/src/main/scala/cats/kernel/instances/MapInstances.scala +++ b/kernel/src/main/scala/cats/kernel/instances/MapInstances.scala @@ -4,6 +4,7 @@ package instances import scala.collection.mutable import compat.scalaVersionSpecific._ +@suppressUnusedImportWarningForScalaVersionSpecific trait MapInstances extends MapInstances1 { implicit def catsKernelStdHashForMap[K: Hash, V: Hash]: Hash[Map[K, V]] = new MapHash[K, V] diff --git a/kernel/src/main/scala/cats/kernel/instances/QueueInstances.scala b/kernel/src/main/scala/cats/kernel/instances/QueueInstances.scala index f36e87ec94..dfb0aede60 100644 --- a/kernel/src/main/scala/cats/kernel/instances/QueueInstances.scala +++ b/kernel/src/main/scala/cats/kernel/instances/QueueInstances.scala @@ -4,6 +4,7 @@ package instances import scala.collection.immutable.Queue import compat.scalaVersionSpecific._ +@suppressUnusedImportWarningForScalaVersionSpecific trait QueueInstances extends QueueInstances1 { implicit def catsKernelStdOrderForQueue[A: Order]: Order[Queue[A]] = new QueueOrder[A] diff --git a/kernel/src/main/scala/cats/kernel/instances/StaticMethods.scala b/kernel/src/main/scala/cats/kernel/instances/StaticMethods.scala index 7578463229..1504b83ebd 100644 --- a/kernel/src/main/scala/cats/kernel/instances/StaticMethods.scala +++ b/kernel/src/main/scala/cats/kernel/instances/StaticMethods.scala @@ -5,6 +5,8 @@ package instances import scala.collection.mutable import compat.scalaVersionSpecific._ + +@suppressUnusedImportWarningForScalaVersionSpecific object StaticMethods extends cats.kernel.compat.HashCompat { def wrapMutableMap[K, V](m: mutable.Map[K, V]): Map[K, V] = diff --git a/kernel/src/main/scala/cats/kernel/instances/StreamInstances.scala b/kernel/src/main/scala/cats/kernel/instances/StreamInstances.scala index 2ea3a2a37e..3e648128fb 100644 --- a/kernel/src/main/scala/cats/kernel/instances/StreamInstances.scala +++ b/kernel/src/main/scala/cats/kernel/instances/StreamInstances.scala @@ -2,6 +2,8 @@ package cats.kernel package instances import compat.scalaVersionSpecific._ + +@suppressUnusedImportWarningForScalaVersionSpecific trait StreamInstances extends StreamInstances1 { implicit def catsKernelStdOrderForStream[A: Order]: Order[LazyList[A]] = new StreamOrder[A] diff --git a/kernel/src/main/scala/cats/kernel/instances/StringInstances.scala b/kernel/src/main/scala/cats/kernel/instances/StringInstances.scala index 28feead4fc..b32af648cd 100644 --- a/kernel/src/main/scala/cats/kernel/instances/StringInstances.scala +++ b/kernel/src/main/scala/cats/kernel/instances/StringInstances.scala @@ -2,6 +2,8 @@ package cats.kernel package instances import compat.scalaVersionSpecific._ + +@suppressUnusedImportWarningForScalaVersionSpecific trait StringInstances { implicit val catsKernelStdOrderForString: Order[String] with Hash[String] = new StringOrder implicit val catsKernelStdMonoidForString: Monoid[String] = new StringMonoid diff --git a/kernel/src/main/scala/cats/kernel/instances/UnitInstances.scala b/kernel/src/main/scala/cats/kernel/instances/UnitInstances.scala index 443f02a9ee..24e061194b 100644 --- a/kernel/src/main/scala/cats/kernel/instances/UnitInstances.scala +++ b/kernel/src/main/scala/cats/kernel/instances/UnitInstances.scala @@ -2,6 +2,7 @@ package cats.kernel package instances import compat.scalaVersionSpecific._ +@suppressUnusedImportWarningForScalaVersionSpecific trait UnitInstances { implicit val catsKernelStdOrderForUnit: Order[Unit] with Hash[Unit] = new UnitOrder diff --git a/kernel/src/main/scala/cats/kernel/instances/VectorInstances.scala b/kernel/src/main/scala/cats/kernel/instances/VectorInstances.scala index e074228249..acb9cf8673 100644 --- a/kernel/src/main/scala/cats/kernel/instances/VectorInstances.scala +++ b/kernel/src/main/scala/cats/kernel/instances/VectorInstances.scala @@ -2,6 +2,7 @@ package cats.kernel package instances import compat.scalaVersionSpecific._ +@suppressUnusedImportWarningForScalaVersionSpecific trait VectorInstances extends VectorInstances1 { implicit def catsKernelStdOrderForVector[A: Order]: Order[Vector[A]] = new VectorOrder[A] From dbe79eb522504b9f9719f8dafde9f7784a0e828d Mon Sep 17 00:00:00 2001 From: Kailuo Wang Date: Wed, 26 Jun 2019 19:44:25 -0400 Subject: [PATCH 3/6] renable fatal warning on 2.13 --- .jvmopts | 2 +- .../alleycats/compat/scalaVersionSpecific.scala | 14 ++++++++++++++ .../alleycats/compat/scalaVersionSpecific.scala | 12 ++++++++++++ .../src/main/scala/alleycats/std/set.scala | 7 +++++-- core/src/main/scala/cats/Inject.scala | 3 +++ core/src/main/scala/cats/InjectK.scala | 2 ++ .../main/scala/cats/data/NonEmptyVector.scala | 1 + core/src/main/scala/cats/data/ZipList.scala | 4 ---- core/src/main/scala/cats/data/ZipVector.scala | 1 - core/src/main/scala/cats/data/package.scala | 1 - .../kernel/compat/scalaVersionMoreSpecific.scala | 16 ++++++++++++++++ .../kernel/compat/scalaVersionSpecific.scala | 10 ++++------ .../kernel/compat/scalaVersionMoreSpecific.scala | 10 ++++++++++ .../kernel/compat/scalaVersionMoreSpecific.scala | 9 +++++++++ kernel/src/main/scala/cats/kernel/Eq.scala | 1 + .../main/scala/cats/kernel/PartialOrder.scala | 1 + .../cats/kernel/instances/StaticMethods.scala | 2 -- .../cats/kernel/instances/StreamInstances.scala | 2 -- .../cats/kernel/instances/StringInstances.scala | 2 -- .../scala/cats/tests/NonEmptyListSuite.scala | 3 ++- 20 files changed, 81 insertions(+), 22 deletions(-) create mode 100644 alleycats-core/src/main/scala-2.12-/alleycats/compat/scalaVersionSpecific.scala create mode 100644 alleycats-core/src/main/scala-2.13+/alleycats/compat/scalaVersionSpecific.scala create mode 100644 kernel/src/main/scala-2.11/cats/kernel/compat/scalaVersionMoreSpecific.scala create mode 100644 kernel/src/main/scala-2.12/cats/kernel/compat/scalaVersionMoreSpecific.scala create mode 100644 kernel/src/main/scala-2.13+/cats/kernel/compat/scalaVersionMoreSpecific.scala diff --git a/.jvmopts b/.jvmopts index 08459dfaaf..afbee1f086 100644 --- a/.jvmopts +++ b/.jvmopts @@ -1,6 +1,6 @@ -Dfile.encoding=UTF8 -Xms1G --Xmx4G +-Xmx5G -XX:ReservedCodeCacheSize=500M -XX:+TieredCompilation -XX:+UseParallelGC diff --git a/alleycats-core/src/main/scala-2.12-/alleycats/compat/scalaVersionSpecific.scala b/alleycats-core/src/main/scala-2.12-/alleycats/compat/scalaVersionSpecific.scala new file mode 100644 index 0000000000..13db1ea447 --- /dev/null +++ b/alleycats-core/src/main/scala-2.12-/alleycats/compat/scalaVersionSpecific.scala @@ -0,0 +1,14 @@ +package alleycats.compat +import scala.annotation.{Annotation, StaticAnnotation} + +private[alleycats] object scalaVersionSpecific { + + /** + * a trick to suppress unused import warning for this object + */ + class suppressUnusedImportWarningForScalaVersionSpecific extends Annotation with StaticAnnotation + + implicit class traversableOnceExtension[A](private val to: TraversableOnce[A]) extends AnyVal { + def iterator: Iterator[A] = to.toIterator + } +} diff --git a/alleycats-core/src/main/scala-2.13+/alleycats/compat/scalaVersionSpecific.scala b/alleycats-core/src/main/scala-2.13+/alleycats/compat/scalaVersionSpecific.scala new file mode 100644 index 0000000000..18e1715db3 --- /dev/null +++ b/alleycats-core/src/main/scala-2.13+/alleycats/compat/scalaVersionSpecific.scala @@ -0,0 +1,12 @@ +package alleycats +package compat +import scala.annotation.{Annotation, StaticAnnotation} + +private[alleycats] object scalaVersionSpecific { + + /** + * a trick to suppress unused import warning for this object + */ + class suppressUnusedImportWarningForScalaVersionSpecific extends Annotation with StaticAnnotation + +} diff --git a/alleycats-core/src/main/scala/alleycats/std/set.scala b/alleycats-core/src/main/scala/alleycats/std/set.scala index 7eb46a9eac..c4bd0b57dc 100644 --- a/alleycats-core/src/main/scala/alleycats/std/set.scala +++ b/alleycats-core/src/main/scala/alleycats/std/set.scala @@ -1,11 +1,14 @@ -package alleycats.std +package alleycats +package std import cats.{Applicative, Eval, Foldable, Monad, Monoid, Traverse, TraverseFilter} import scala.annotation.tailrec +import compat.scalaVersionSpecific._ object set extends SetInstances +@suppressUnusedImportWarningForScalaVersionSpecific trait SetInstances { // This method advertises parametricity, but relies on using // universal hash codes and equality, which hurts our ability to @@ -86,7 +89,7 @@ trait SetInstances { go(idx - 1, it) } } else None - if (idx < Int.MaxValue && idx >= 0L) go(idx.toInt, fa.toIterator) else None + if (idx < Int.MaxValue && idx >= 0L) go(idx.toInt, fa.iterator) else None } override def size[A](fa: Set[A]): Long = fa.size.toLong diff --git a/core/src/main/scala/cats/Inject.scala b/core/src/main/scala/cats/Inject.scala index 3433246451..3fc892395b 100644 --- a/core/src/main/scala/cats/Inject.scala +++ b/core/src/main/scala/cats/Inject.scala @@ -1,5 +1,7 @@ package cats +import kernel.compat.scalaVersionMoreSpecific._ + /** * Inject is a type class providing an injection from type `A` into * type `B`. An injection is a function `inj` which does not destroy @@ -25,6 +27,7 @@ abstract class Inject[A, B] { final def unapply(b: B): Option[A] = prj(b) } +@suppressUnusedImportWarningForScalaVersionMoreSpecific sealed abstract private[cats] class InjectInstances { implicit def catsReflexiveInjectInstance[A]: Inject[A, A] = new Inject[A, A] { diff --git a/core/src/main/scala/cats/InjectK.scala b/core/src/main/scala/cats/InjectK.scala index 26791e60c4..180949b297 100644 --- a/core/src/main/scala/cats/InjectK.scala +++ b/core/src/main/scala/cats/InjectK.scala @@ -2,6 +2,7 @@ package cats import cats.arrow.FunctionK import cats.data.EitherK +import kernel.compat.scalaVersionMoreSpecific._ /** * InjectK is a type class providing an injection from type @@ -32,6 +33,7 @@ abstract class InjectK[F[_], G[_]] { final def unapply[A](ga: G[A]): Option[F[A]] = prj(ga) } +@suppressUnusedImportWarningForScalaVersionMoreSpecific sealed abstract private[cats] class InjectKInstances { implicit def catsReflexiveInjectKInstance[F[_]]: InjectK[F, F] = new InjectK[F, F] { diff --git a/core/src/main/scala/cats/data/NonEmptyVector.scala b/core/src/main/scala/cats/data/NonEmptyVector.scala index f2f7139776..eda86274f5 100644 --- a/core/src/main/scala/cats/data/NonEmptyVector.scala +++ b/core/src/main/scala/cats/data/NonEmptyVector.scala @@ -6,6 +6,7 @@ import scala.annotation.tailrec import scala.collection.immutable.{TreeSet, VectorBuilder} import cats.instances.vector._ import kernel.compat.scalaVersionSpecific._ + /** * A data type which represents a `Vector` guaranteed to contain at least one element. *
diff --git a/core/src/main/scala/cats/data/ZipList.scala b/core/src/main/scala/cats/data/ZipList.scala index 5ea15a00f5..66a491c736 100644 --- a/core/src/main/scala/cats/data/ZipList.scala +++ b/core/src/main/scala/cats/data/ZipList.scala @@ -4,13 +4,11 @@ package data import instances.list.catsKernelStdEqForList import kernel.compat.scalaVersionSpecific._ - class ZipList[A](val value: List[A]) extends AnyVal @suppressUnusedImportWarningForScalaVersionSpecific object ZipList { - def apply[A](value: List[A]): ZipList[A] = new ZipList(value) implicit val catsDataCommutativeApplyForZipList: CommutativeApply[ZipList] = new CommutativeApply[ZipList] { @@ -28,5 +26,3 @@ object ZipList { implicit def catsDataEqForZipList[A: Eq]: Eq[ZipList[A]] = Eq.by(_.value) } - - diff --git a/core/src/main/scala/cats/data/ZipVector.scala b/core/src/main/scala/cats/data/ZipVector.scala index 8aef78d12e..ebb7e1a4cc 100644 --- a/core/src/main/scala/cats/data/ZipVector.scala +++ b/core/src/main/scala/cats/data/ZipVector.scala @@ -4,7 +4,6 @@ package data import instances.vector._ import kernel.compat.scalaVersionSpecific._ - class ZipVector[A](val value: Vector[A]) extends AnyVal @suppressUnusedImportWarningForScalaVersionSpecific diff --git a/core/src/main/scala/cats/data/package.scala b/core/src/main/scala/cats/data/package.scala index bbda18e522..2257e965e3 100644 --- a/core/src/main/scala/cats/data/package.scala +++ b/core/src/main/scala/cats/data/package.scala @@ -2,7 +2,6 @@ package cats import kernel.compat.scalaVersionSpecific._ import compat.lazyList.toLazyList - package object data { type NonEmptyStream[A] = OneAnd[LazyList, A] diff --git a/kernel/src/main/scala-2.11/cats/kernel/compat/scalaVersionMoreSpecific.scala b/kernel/src/main/scala-2.11/cats/kernel/compat/scalaVersionMoreSpecific.scala new file mode 100644 index 0000000000..3632c6036e --- /dev/null +++ b/kernel/src/main/scala-2.11/cats/kernel/compat/scalaVersionMoreSpecific.scala @@ -0,0 +1,16 @@ +package cats.kernel.compat +import scala.annotation.{Annotation, StaticAnnotation} + +private[cats] object scalaVersionMoreSpecific { + implicit def toEitherExtensionOps[A, B](either: Either[A, B]): EitherExtensionOps[A, B] = + new EitherExtensionOps(either) + + class EitherExtensionOps[A, B](val either: Either[A, B]) extends AnyVal { + @inline def toOption: Option[B] = either.right.toOption + } + + /** + * a trick to suppress unused import warning for this object + */ + class suppressUnusedImportWarningForScalaVersionMoreSpecific extends Annotation with StaticAnnotation +} diff --git a/kernel/src/main/scala-2.12-/cats/kernel/compat/scalaVersionSpecific.scala b/kernel/src/main/scala-2.12-/cats/kernel/compat/scalaVersionSpecific.scala index aeb0c3999b..a6a1f8058d 100644 --- a/kernel/src/main/scala-2.12-/cats/kernel/compat/scalaVersionSpecific.scala +++ b/kernel/src/main/scala-2.12-/cats/kernel/compat/scalaVersionSpecific.scala @@ -3,6 +3,7 @@ import scala.annotation.{Annotation, StaticAnnotation} import scala.collection.{IterableLike, TraversableLike} private[cats] object scalaVersionSpecific { + /** * a trick to suppress unused import warning for this object */ @@ -14,20 +15,17 @@ private[cats] object scalaVersionSpecific { implicit class traversableOnceExtension[A](private val to: TraversableOnce[A]) extends AnyVal { def iterator: Iterator[A] = to.toIterator - } implicit class doubleExtension(private val double: Double) extends AnyVal { - def sign: Double = if(double.isNaN) Double.NaN else double.signum.toDouble + def sign: Double = if (double.isNaN) Double.NaN else double.signum.toDouble } implicit class intExtension(private val i: Int) extends AnyVal { def sign: Int = i.signum } implicit class lazyZipExtension[A](private val a: A) extends AnyVal { - def lazyZip[El1, Repr1, El2, Repr2, T](that: T) - (implicit w1: A => TraversableLike[El1, Repr1], - w2: T => IterableLike[El2, Repr2]) = (a, that).zipped - + def lazyZip[El1, Repr1, El2, Repr2, T](that: T)(implicit w1: A => TraversableLike[El1, Repr1], + w2: T => IterableLike[El2, Repr2]) = (a, that).zipped } } diff --git a/kernel/src/main/scala-2.12/cats/kernel/compat/scalaVersionMoreSpecific.scala b/kernel/src/main/scala-2.12/cats/kernel/compat/scalaVersionMoreSpecific.scala new file mode 100644 index 0000000000..5f8c6aec2a --- /dev/null +++ b/kernel/src/main/scala-2.12/cats/kernel/compat/scalaVersionMoreSpecific.scala @@ -0,0 +1,10 @@ +package cats.kernel.compat +import scala.annotation.{Annotation, StaticAnnotation} + +private[cats] object scalaVersionMoreSpecific { + + /** + * a trick to suppress unused import warning for this object + */ + class suppressUnusedImportWarningForScalaVersionMoreSpecific extends Annotation with StaticAnnotation +} diff --git a/kernel/src/main/scala-2.13+/cats/kernel/compat/scalaVersionMoreSpecific.scala b/kernel/src/main/scala-2.13+/cats/kernel/compat/scalaVersionMoreSpecific.scala new file mode 100644 index 0000000000..f8b39f6853 --- /dev/null +++ b/kernel/src/main/scala-2.13+/cats/kernel/compat/scalaVersionMoreSpecific.scala @@ -0,0 +1,9 @@ +package cats.kernel.compat +import scala.annotation.{Annotation, StaticAnnotation} + +private[cats] object scalaVersionMoreSpecific { + /** + * a trick to suppress unused import warning for this object + */ + class suppressUnusedImportWarningForScalaVersionMoreSpecific extends Annotation with StaticAnnotation +} diff --git a/kernel/src/main/scala/cats/kernel/Eq.scala b/kernel/src/main/scala/cats/kernel/Eq.scala index e124b4bba5..94c6486589 100644 --- a/kernel/src/main/scala/cats/kernel/Eq.scala +++ b/kernel/src/main/scala/cats/kernel/Eq.scala @@ -32,6 +32,7 @@ abstract class EqFunctions[E[T] <: Eq[T]] { } trait EqToEquivConversion { + /** * Implicitly derive a `scala.math.Equiv[A]` from a `Eq[A]` * instance. diff --git a/kernel/src/main/scala/cats/kernel/PartialOrder.scala b/kernel/src/main/scala/cats/kernel/PartialOrder.scala index 9eb7df1404..63b5d560a6 100644 --- a/kernel/src/main/scala/cats/kernel/PartialOrder.scala +++ b/kernel/src/main/scala/cats/kernel/PartialOrder.scala @@ -3,6 +3,7 @@ package cats.kernel import java.lang.Double.isNaN import scala.{specialized => sp} import compat.scalaVersionSpecific._ + /** * The `PartialOrder` type class is used to define a partial ordering on some type `A`. * diff --git a/kernel/src/main/scala/cats/kernel/instances/StaticMethods.scala b/kernel/src/main/scala/cats/kernel/instances/StaticMethods.scala index 1504b83ebd..7edba34c1c 100644 --- a/kernel/src/main/scala/cats/kernel/instances/StaticMethods.scala +++ b/kernel/src/main/scala/cats/kernel/instances/StaticMethods.scala @@ -4,8 +4,6 @@ package instances import scala.collection.mutable import compat.scalaVersionSpecific._ - - @suppressUnusedImportWarningForScalaVersionSpecific object StaticMethods extends cats.kernel.compat.HashCompat { diff --git a/kernel/src/main/scala/cats/kernel/instances/StreamInstances.scala b/kernel/src/main/scala/cats/kernel/instances/StreamInstances.scala index 3e648128fb..dcdde43a8e 100644 --- a/kernel/src/main/scala/cats/kernel/instances/StreamInstances.scala +++ b/kernel/src/main/scala/cats/kernel/instances/StreamInstances.scala @@ -1,8 +1,6 @@ package cats.kernel package instances import compat.scalaVersionSpecific._ - - @suppressUnusedImportWarningForScalaVersionSpecific trait StreamInstances extends StreamInstances1 { implicit def catsKernelStdOrderForStream[A: Order]: Order[LazyList[A]] = diff --git a/kernel/src/main/scala/cats/kernel/instances/StringInstances.scala b/kernel/src/main/scala/cats/kernel/instances/StringInstances.scala index b32af648cd..c7c2c0168a 100644 --- a/kernel/src/main/scala/cats/kernel/instances/StringInstances.scala +++ b/kernel/src/main/scala/cats/kernel/instances/StringInstances.scala @@ -1,8 +1,6 @@ package cats.kernel package instances import compat.scalaVersionSpecific._ - - @suppressUnusedImportWarningForScalaVersionSpecific trait StringInstances { implicit val catsKernelStdOrderForString: Order[String] with Hash[String] = new StringOrder diff --git a/tests/src/test/scala/cats/tests/NonEmptyListSuite.scala b/tests/src/test/scala/cats/tests/NonEmptyListSuite.scala index d15198da58..b26c6ba674 100644 --- a/tests/src/test/scala/cats/tests/NonEmptyListSuite.scala +++ b/tests/src/test/scala/cats/tests/NonEmptyListSuite.scala @@ -153,7 +153,8 @@ class NonEmptyListSuite extends CatsSuite { forAll { (nel: NonEmptyList[Int], f: (Int, Eval[Int]) => Eval[Int]) => val got = nel.reduceRight(f).value nel.toList.reverse match { - case last :: rev => val expected = rev.reverse.foldRight(last)((a, b) => f(a, Now(b)).value) + case last :: rev => + val expected = rev.reverse.foldRight(last)((a, b) => f(a, Now(b)).value) got should ===(expected) case _ => fail("nonempty turns out to be empty") } From d406c4e63def588a00438b18cc1b1963dae93fca Mon Sep 17 00:00:00 2001 From: Kailuo Wang Date: Wed, 26 Jun 2019 20:15:24 -0400 Subject: [PATCH 4/6] fix one more warning --- .../shared/src/test/scala/cats/kernel/laws/LawTests.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala b/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala index eeaae161e4..c5cfe95395 100644 --- a/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala +++ b/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala @@ -14,11 +14,11 @@ import org.scalatest.funsuite.AnyFunSuiteLike import scala.concurrent.duration.{Duration, FiniteDuration} import scala.collection.immutable.{BitSet, Queue} import scala.util.Random -import compat.scalaVersionSpecific._ import java.util.UUID import java.util.concurrent.TimeUnit.{DAYS, HOURS, MICROSECONDS, MILLISECONDS, MINUTES, NANOSECONDS, SECONDS} import compat.scalaVersionSpecific._ +@suppressUnusedImportWarningForScalaVersionSpecific object KernelCheck { implicit val arbitraryBitSet: Arbitrary[BitSet] = From 7010219caddc59596527ed4a06aacf8d4b19c2ac Mon Sep 17 00:00:00 2001 From: Kailuo Wang Date: Wed, 26 Jun 2019 20:59:43 -0400 Subject: [PATCH 5/6] missed one warning in free --- free/src/main/scala/cats/free/FreeApplicative.scala | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/free/src/main/scala/cats/free/FreeApplicative.scala b/free/src/main/scala/cats/free/FreeApplicative.scala index 1103b3240d..fe63b262b5 100644 --- a/free/src/main/scala/cats/free/FreeApplicative.scala +++ b/free/src/main/scala/cats/free/FreeApplicative.scala @@ -170,12 +170,10 @@ object FreeApplicative { // Internal helper function for foldMap, it folds only Pure and Lift nodes private[free] def foldArg[F[_], G[_], A](node: FA[F, A], f: F ~> G)(implicit G: Applicative[G]): G[A] = - if (node.isInstanceOf[Pure[F, A]]) { - val Pure(x) = node - G.pure(x) - } else { - val Lift(fa) = node - f(fa) + node match { + case Pure(x) => G.pure(x) + case Lift(fa) => f(fa) + case x => throw new RuntimeException(s"Impossible for a $x to reach here") } /** Represents a curried function `F[A => B => C => ...]` From 60dc3adc1bd98c19406876ac71ff55fa51ac5979 Mon Sep 17 00:00:00 2001 From: Kailuo Wang Date: Wed, 26 Jun 2019 21:07:09 -0400 Subject: [PATCH 6/6] reformat --- free/src/main/scala/cats/free/FreeApplicative.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/free/src/main/scala/cats/free/FreeApplicative.scala b/free/src/main/scala/cats/free/FreeApplicative.scala index fe63b262b5..ba040032c4 100644 --- a/free/src/main/scala/cats/free/FreeApplicative.scala +++ b/free/src/main/scala/cats/free/FreeApplicative.scala @@ -171,9 +171,9 @@ object FreeApplicative { // Internal helper function for foldMap, it folds only Pure and Lift nodes private[free] def foldArg[F[_], G[_], A](node: FA[F, A], f: F ~> G)(implicit G: Applicative[G]): G[A] = node match { - case Pure(x) => G.pure(x) + case Pure(x) => G.pure(x) case Lift(fa) => f(fa) - case x => throw new RuntimeException(s"Impossible for a $x to reach here") + case x => throw new RuntimeException(s"Impossible for a $x to reach here") } /** Represents a curried function `F[A => B => C => ...]`