From 9b5aad942f659016a52fac0f8439c56e5c15fbd0 Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Mon, 12 Jul 2021 18:32:01 +0200 Subject: [PATCH] Share source directory between Scala 2.13 and Scala 3 (#30384) --- .../akka/compat/Future.scala | 0 .../akka/compat/PartialFunction.scala | 0 .../internal/SameThreadExecutionContext.scala | 0 .../util/ccompat/ccompatUsedUntil213.scala | 0 .../akka/util/ccompat/package.scala | 0 .../src/main/scala-3/akka/compat/Future.scala | 50 ------------------- .../scala-3/akka/compat/PartialFunction.scala | 23 --------- .../internal/SameThreadExecutionContext.scala | 19 ------- .../util/ccompat/ccompatUsedUntil213.scala | 18 ------- .../scala-3/akka/util/ccompat/package.scala | 23 --------- project/AkkaBuild.scala | 10 ++++ 11 files changed, 10 insertions(+), 133 deletions(-) rename akka-actor/src/main/{scala-2.13 => scala-2.13+}/akka/compat/Future.scala (100%) rename akka-actor/src/main/{scala-2.13 => scala-2.13+}/akka/compat/PartialFunction.scala (100%) rename akka-actor/src/main/{scala-2.13 => scala-2.13+}/akka/dispatch/internal/SameThreadExecutionContext.scala (100%) rename akka-actor/src/main/{scala-2.13 => scala-2.13+}/akka/util/ccompat/ccompatUsedUntil213.scala (100%) rename akka-actor/src/main/{scala-2.13 => scala-2.13+}/akka/util/ccompat/package.scala (100%) delete mode 100644 akka-actor/src/main/scala-3/akka/compat/Future.scala delete mode 100644 akka-actor/src/main/scala-3/akka/compat/PartialFunction.scala delete mode 100644 akka-actor/src/main/scala-3/akka/dispatch/internal/SameThreadExecutionContext.scala delete mode 100644 akka-actor/src/main/scala-3/akka/util/ccompat/ccompatUsedUntil213.scala delete mode 100644 akka-actor/src/main/scala-3/akka/util/ccompat/package.scala diff --git a/akka-actor/src/main/scala-2.13/akka/compat/Future.scala b/akka-actor/src/main/scala-2.13+/akka/compat/Future.scala similarity index 100% rename from akka-actor/src/main/scala-2.13/akka/compat/Future.scala rename to akka-actor/src/main/scala-2.13+/akka/compat/Future.scala diff --git a/akka-actor/src/main/scala-2.13/akka/compat/PartialFunction.scala b/akka-actor/src/main/scala-2.13+/akka/compat/PartialFunction.scala similarity index 100% rename from akka-actor/src/main/scala-2.13/akka/compat/PartialFunction.scala rename to akka-actor/src/main/scala-2.13+/akka/compat/PartialFunction.scala diff --git a/akka-actor/src/main/scala-2.13/akka/dispatch/internal/SameThreadExecutionContext.scala b/akka-actor/src/main/scala-2.13+/akka/dispatch/internal/SameThreadExecutionContext.scala similarity index 100% rename from akka-actor/src/main/scala-2.13/akka/dispatch/internal/SameThreadExecutionContext.scala rename to akka-actor/src/main/scala-2.13+/akka/dispatch/internal/SameThreadExecutionContext.scala diff --git a/akka-actor/src/main/scala-2.13/akka/util/ccompat/ccompatUsedUntil213.scala b/akka-actor/src/main/scala-2.13+/akka/util/ccompat/ccompatUsedUntil213.scala similarity index 100% rename from akka-actor/src/main/scala-2.13/akka/util/ccompat/ccompatUsedUntil213.scala rename to akka-actor/src/main/scala-2.13+/akka/util/ccompat/ccompatUsedUntil213.scala diff --git a/akka-actor/src/main/scala-2.13/akka/util/ccompat/package.scala b/akka-actor/src/main/scala-2.13+/akka/util/ccompat/package.scala similarity index 100% rename from akka-actor/src/main/scala-2.13/akka/util/ccompat/package.scala rename to akka-actor/src/main/scala-2.13+/akka/util/ccompat/package.scala diff --git a/akka-actor/src/main/scala-3/akka/compat/Future.scala b/akka-actor/src/main/scala-3/akka/compat/Future.scala deleted file mode 100644 index c58f650797..0000000000 --- a/akka-actor/src/main/scala-3/akka/compat/Future.scala +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2009-2021 Lightbend Inc. - */ - -package akka.compat - -import scala.collection.immutable -import scala.concurrent.{ ExecutionContext, Future => SFuture } - -import akka.annotation.InternalApi - -/** - * INTERNAL API - * - * Compatibility wrapper for `scala.concurrent.Future` to be able to compile the same code - * against Scala 2.12, 2.13 - * - * Remove these classes as soon as support for Scala 2.12 is dropped! - */ -@InternalApi private[akka] object Future { - def fold[T, R](futures: IterableOnce[SFuture[T]])(zero: R)(op: (R, T) => R)( - implicit executor: ExecutionContext): SFuture[R] = { - // This will have performance implications since the elements are copied to a Vector - SFuture.foldLeft[T, R](futures.iterator.to(immutable.Iterable))(zero)(op)(executor) - } - - def fold[T, R](futures: immutable.Iterable[SFuture[T]])(zero: R)(op: (R, T) => R)( - implicit executor: ExecutionContext): SFuture[R] = - SFuture.foldLeft[T, R](futures)(zero)(op)(executor) - - def reduce[T, R >: T](futures: IterableOnce[SFuture[T]])(op: (R, T) => R)( - implicit executor: ExecutionContext): SFuture[R] = { - // This will have performance implications since the elements are copied to a Vector - SFuture.reduceLeft[T, R](futures.iterator.to(immutable.Iterable))(op)(executor) - } - - def reduce[T, R >: T](futures: immutable.Iterable[SFuture[T]])(op: (R, T) => R)( - implicit executor: ExecutionContext): SFuture[R] = - SFuture.reduceLeft[T, R](futures)(op)(executor) - - def find[T](futures: IterableOnce[SFuture[T]])(p: T => Boolean)( - implicit executor: ExecutionContext): SFuture[Option[T]] = { - // This will have performance implications since the elements are copied to a Vector - SFuture.find[T](futures.iterator.to(immutable.Iterable))(p)(executor) - } - - def find[T](futures: immutable.Iterable[SFuture[T]])(p: T => Boolean)( - implicit executor: ExecutionContext): SFuture[Option[T]] = - SFuture.find[T](futures)(p)(executor) -} diff --git a/akka-actor/src/main/scala-3/akka/compat/PartialFunction.scala b/akka-actor/src/main/scala-3/akka/compat/PartialFunction.scala deleted file mode 100644 index 9ca20d6667..0000000000 --- a/akka-actor/src/main/scala-3/akka/compat/PartialFunction.scala +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (C) 2009-2021 Lightbend Inc. - */ - -package akka.compat - -import akka.annotation.InternalApi - -/** - * INTERNAL API - * - * Compatibility wrapper for `scala.PartialFunction` to be able to compile the same code - * against Scala 2.12, 2.13, 3.0 - * - * Remove these classes as soon as support for Scala 2.12 is dropped! - */ -@InternalApi private[akka] object PartialFunction { - - def fromFunction[A, B](f: (A) => B): scala.PartialFunction[A, B] = { - scala.PartialFunction.fromFunction(f) - } - -} diff --git a/akka-actor/src/main/scala-3/akka/dispatch/internal/SameThreadExecutionContext.scala b/akka-actor/src/main/scala-3/akka/dispatch/internal/SameThreadExecutionContext.scala deleted file mode 100644 index 62811d65ae..0000000000 --- a/akka-actor/src/main/scala-3/akka/dispatch/internal/SameThreadExecutionContext.scala +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (C) 2009-2021 Lightbend Inc. - */ - -package akka.dispatch.internal - -import scala.concurrent.ExecutionContext - -import akka.annotation.InternalApi - -/** - * Factory to create same thread ec. Not intended to be called from any other site than to create [[akka.dispatch.ExecutionContexts#parasitic]] - * - * INTERNAL API - */ -@InternalApi -private[dispatch] object SameThreadExecutionContext { - def apply(): ExecutionContext = ExecutionContext.parasitic -} diff --git a/akka-actor/src/main/scala-3/akka/util/ccompat/ccompatUsedUntil213.scala b/akka-actor/src/main/scala-3/akka/util/ccompat/ccompatUsedUntil213.scala deleted file mode 100644 index 76478b1985..0000000000 --- a/akka-actor/src/main/scala-3/akka/util/ccompat/ccompatUsedUntil213.scala +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright (C) 2019-2021 Lightbend Inc. - */ - -package akka.util.ccompat - -import scala.annotation.Annotation - -import akka.annotation.InternalApi - -/** - * INTERNAL API - * - * Annotation to mark files that need ccompat to be imported for Scala 2.11 and/or 2.12, - * but not 2.13. Gets rid of the 'unused import' warning on 2.13. - */ -@InternalApi -private[akka] class ccompatUsedUntil213 extends Annotation diff --git a/akka-actor/src/main/scala-3/akka/util/ccompat/package.scala b/akka-actor/src/main/scala-3/akka/util/ccompat/package.scala deleted file mode 100644 index 4ade2b5f80..0000000000 --- a/akka-actor/src/main/scala-3/akka/util/ccompat/package.scala +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (C) 2018-2021 Lightbend Inc. - */ - -package akka.util - -/** - * INTERNAL API - * - * Based on https://github.com/scala/scala-collection-compat/blob/master/compat/src/main/scala-2.13/scala/collection/compat/package.scala - * but reproduced here so we don't need to add a dependency on this library. It contains much more than we need right now, and is - * not promising binary compatibility yet at the time of writing. - */ -package object ccompat { - private[akka] type Factory[-A, +C] = scala.collection.Factory[A, C] - private[akka] val Factory = scala.collection.Factory - - // When we drop support for 2.12 we can delete this concept - // and import scala.jdk.CollectionConverters.Ops._ instead - object JavaConverters - extends scala.collection.convert.AsJavaExtensions - with scala.collection.convert.AsScalaExtensions -} diff --git a/project/AkkaBuild.scala b/project/AkkaBuild.scala index 9211a4383a..b84b48b814 100644 --- a/project/AkkaBuild.scala +++ b/project/AkkaBuild.scala @@ -130,6 +130,16 @@ object AkkaBuild { Compile / javacOptions ++= (if (allWarnings) Seq("-Xlint:deprecation") else Nil), doc / javacOptions := Seq(), crossVersion := CrossVersion.binary, + // Adds a `src/main/scala-2.13+` source directory for code shared + // between Scala 2.13 and Scala 3 + unmanagedSourceDirectories in Compile ++= { + val sourceDir = (sourceDirectory in Compile).value + CrossVersion.partialVersion(scalaVersion.value) match { + case Some((3, n)) => Seq(sourceDir / "scala-2.13+") + case Some((2, n)) if n >= 13 => Seq(sourceDir / "scala-2.13+") + case _ => Nil + } + }, ThisBuild / ivyLoggingLevel := UpdateLogging.Quiet, licenses := Seq(("Apache-2.0", url("https://www.apache.org/licenses/LICENSE-2.0.html"))), homepage := Some(url("https://akka.io/")),