Skip to content

Commit

Permalink
Fix Scala 3 compile
Browse files Browse the repository at this point in the history
  • Loading branch information
armanbilge committed Aug 3, 2023
1 parent b40fcae commit 3ccef16
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 35 deletions.
14 changes: 7 additions & 7 deletions core/js/src/main/scala/cats/effect/IOPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package cats.effect

import scala.concurrent.Future
import scala.scalajs.js.{|, Function1, JavaScriptException, Promise, Thenable}
import scala.scalajs.js

abstract private[effect] class IOPlatform[+A] { self: IO[A] =>

Expand All @@ -31,10 +31,10 @@ abstract private[effect] class IOPlatform[+A] { self: IO[A] =>
* @see
* [[IO.fromPromise]]
*/
def unsafeToPromise()(implicit runtime: unsafe.IORuntime): Promise[A] =
new Promise[A]((resolve: Function1[A | Thenable[A], _], reject: Function1[Any, _]) =>
def unsafeToPromise()(implicit runtime: unsafe.IORuntime): js.Promise[A] =
new js.Promise[A]((resolve, reject) =>
self.unsafeRunAsync {
case Left(JavaScriptException(e)) =>
case Left(js.JavaScriptException(e)) =>
reject(e)
()

Expand Down Expand Up @@ -76,10 +76,10 @@ abstract private[effect] class IOPlatform[+A] { self: IO[A] =>
* @see
* [[IO.syncStep(limit:Int)*]]
*/
def unsafeRunSyncToPromise()(implicit runtime: unsafe.IORuntime): Promise[A] =
def unsafeRunSyncToPromise()(implicit runtime: unsafe.IORuntime): js.Promise[A] =
self.syncStep(runtime.config.autoYieldThreshold).attempt.unsafeRunSync() match {
case Left(t) => Promise.reject(t)
case Left(t) => js.Promise.reject(t)
case Right(Left(ioa)) => ioa.unsafeToPromise()
case Right(Right(a)) => Promise.resolve[A](a)
case Right(Right(a)) => js.Promise.resolve[A](a)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private[unsafe] abstract class IORuntimeCompanionPlatform { this: IORuntime.type
() => resetGlobal(),
IORuntimeConfig())
}
()
}

_global
Expand Down
4 changes: 3 additions & 1 deletion core/shared/src/main/scala/cats/effect/IODeferred.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ private final class IODeferred[A] extends Deferred[IO, A] {
def clear(): Unit = {
stack.clearCurrent(handle)
val clearCount = clearCounter.incrementAndGet()
if ((clearCount & (clearCount - 1)) == 0) // power of 2
if ((clearCount & (clearCount - 1)) == 0) { // power of 2
clearCounter.addAndGet(-callbacks.pack(clearCount))
()
}
()
}

Expand Down
24 changes: 12 additions & 12 deletions kernel/js/src/main/scala/cats/effect/kernel/AsyncPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,42 @@

package cats.effect.kernel

import scala.scalajs.js.{|, defined, Function1, JavaScriptException, Promise, Thenable}
import scala.scalajs.js

private[kernel] trait AsyncPlatform[F[_]] { this: Async[F] =>

def fromPromise[A](iop: F[Promise[A]]): F[A] = fromThenable(widen(iop))
def fromPromise[A](iop: F[js.Promise[A]]): F[A] = fromThenable(widen(iop))

def fromPromiseCancelable[A](iop: F[(Promise[A], F[Unit])]): F[A] =
def fromPromiseCancelable[A](iop: F[(js.Promise[A], F[Unit])]): F[A] =
fromThenableCancelable(widen(iop))

def fromThenable[A](iot: F[Thenable[A]]): F[A] =
def fromThenable[A](iot: F[js.Thenable[A]]): F[A] =
flatMap(iot) { t =>
async_[A] { cb =>
t.`then`[Unit](mkOnFulfilled(cb), defined(mkOnRejected(cb)))
t.`then`[Unit](mkOnFulfilled(cb), js.defined(mkOnRejected(cb)))
()
}
}

def fromThenableCancelable[A](iot: F[(Thenable[A], F[Unit])]): F[A] =
def fromThenableCancelable[A](iot: F[(js.Thenable[A], F[Unit])]): F[A] =
flatMap(iot) {
case (t, fin) =>
async[A] { cb =>
as(delay(t.`then`[Unit](mkOnFulfilled(cb), defined(mkOnRejected(cb)))), Some(fin))
as(delay(t.`then`[Unit](mkOnFulfilled(cb), js.defined(mkOnRejected(cb)))), Some(fin))
}
}

@inline private[this] def mkOnFulfilled[A](
cb: Either[Throwable, A] => Unit): Function1[A, Unit | Thenable[Unit]] =
(v: A) => cb(Right(v)): Unit | Thenable[Unit]
cb: Either[Throwable, A] => Unit): js.Function1[A, js.UndefOr[js.Thenable[Unit]]] =
(v: A) => cb(Right(v)): js.UndefOr[js.Thenable[Unit]]

@inline private[this] def mkOnRejected[A](
cb: Either[Throwable, A] => Unit): Function1[Any, Unit | Thenable[Unit]] = { (a: Any) =>
cb: Either[Throwable, A] => Unit): js.Function1[Any, js.UndefOr[js.Thenable[Unit]]] = { (a: Any) =>
val e = a match {
case th: Throwable => th
case _ => JavaScriptException(a)
case _ => js.JavaScriptException(a)
}

cb(Left(e)): Unit | Thenable[Unit]
cb(Left(e)): js.UndefOr[js.Thenable[Unit]]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import cats.effect.kernel.testkit.{pure, OutcomeGenerators, PureConcGenerators,
import cats.effect.kernel.testkit.TimeT._
import cats.effect.kernel.testkit.pure._
import cats.laws.discipline.arbitrary._
import cats.syntax.all._

import org.scalacheck.Prop
import org.specs2.mutable._
Expand Down Expand Up @@ -55,7 +54,7 @@ class OptionTPureConcSpec extends Specification with Discipline with BaseSpec {
case Outcome.Succeeded(fa) =>
observed = true

OptionT(fa.value.map(_ must beNone).as(None))
OptionT(fa.value.map(_ must beNone).map(_ => None))

case _ => Applicative[OptionT[PureConc[Int, *], *]].unit
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@

package cats.effect.std

import scala.scalajs.js.{|, Function1, JavaScriptException, Promise, Thenable}
import scala.scalajs.js

private[std] trait DispatcherPlatform[F[_]] { this: Dispatcher[F] =>

/**
* Submits an effect to be executed, returning a `Promise` that holds the result of its
* evaluation.
*/
def unsafeToPromise[A](fa: F[A]): Promise[A] =
new Promise[A]((resolve: Function1[A | Thenable[A], _], reject: Function1[Any, _]) =>
def unsafeToPromise[A](fa: F[A]): js.Promise[A] =
new js.Promise[A]((resolve, reject) =>
unsafeRunAsync(fa) {
case Left(JavaScriptException(e)) =>
case Left(js.JavaScriptException(e)) =>
reject(e)
()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ private[std] abstract class ConsoleCompanionCrossPlatform {
if (len > 0) {
if (builder.charAt(len - 1) == '\r') {
builder.deleteCharAt(len - 1)
()
}
}
builder.toString()
Expand Down
1 change: 0 additions & 1 deletion std/shared/src/main/scala/cats/effect/std/Semaphore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cats
package effect
package std

import cats.Applicative
import cats.effect.kernel._
import cats.effect.kernel.syntax.all._
import cats.syntax.all._
Expand Down
16 changes: 16 additions & 0 deletions tests/shared/src/test/scala/cats/effect/MemoizeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class MemoizeSpec extends BaseSpec with Discipline {

"Concurrent.memoize does not evaluate the effect if the inner `F[A]` isn't bound" in ticked {
implicit ticker =>
import cats.syntax.all._

val op = for {
ref <- Ref.of[F, Int](0)
action = ref.update(_ + 1)
Expand All @@ -57,6 +59,8 @@ class MemoizeSpec extends BaseSpec with Discipline {

"Concurrent.memoize evaluates effect once if inner `F[A]` is bound twice" in ticked {
implicit ticker =>
import cats.syntax.all._

val op = for {
ref <- Ref.of[F, Int](0)
action = ref.modify { s =>
Expand All @@ -77,6 +81,8 @@ class MemoizeSpec extends BaseSpec with Discipline {

"Concurrent.memoize effect evaluates effect once if the inner `F[A]` is bound twice (race)" in ticked {
implicit ticker =>
import cats.syntax.all._

val op = for {
ref <- Ref.of[F, Int](0)
action = ref.modify { s =>
Expand Down Expand Up @@ -108,6 +114,8 @@ class MemoizeSpec extends BaseSpec with Discipline {

"Memoized effects can be canceled when there are no other active subscribers (1)" in ticked {
implicit ticker =>
import cats.syntax.all._

val op = for {
completed <- Ref[F].of(false)
action = liftK(IO.sleep(200.millis)) >> completed.set(true)
Expand All @@ -127,6 +135,8 @@ class MemoizeSpec extends BaseSpec with Discipline {

"Memoized effects can be canceled when there are no other active subscribers (2)" in ticked {
implicit ticker =>
import cats.syntax.all._

val op = for {
completed <- Ref[F].of(false)
action = liftK(IO.sleep(300.millis)) >> completed.set(true)
Expand All @@ -149,6 +159,8 @@ class MemoizeSpec extends BaseSpec with Discipline {

"Memoized effects can be canceled when there are no other active subscribers (3)" in ticked {
implicit ticker =>
import cats.syntax.all._

val op = for {
completed <- Ref[F].of(false)
action = liftK(IO.sleep(300.millis)) >> completed.set(true)
Expand All @@ -171,6 +183,8 @@ class MemoizeSpec extends BaseSpec with Discipline {

"Running a memoized effect after it was previously canceled reruns it" in ticked {
implicit ticker =>
import cats.syntax.all._

val op = for {
started <- Ref[F].of(0)
completed <- Ref[F].of(0)
Expand All @@ -195,6 +209,8 @@ class MemoizeSpec extends BaseSpec with Discipline {

"Attempting to cancel a memoized effect with active subscribers is a no-op" in ticked {
implicit ticker =>
import cats.syntax.all._

val op = for {
startCounter <- Ref[F].of(0)
condition <- Deferred[F, Unit]
Expand Down
6 changes: 3 additions & 3 deletions tests/shared/src/test/scala/cats/effect/ResourceSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ class ResourceSpec extends BaseSpec with ScalaCheck with Discipline {
val outerInit = Resource.make(IO.unit)(_ => IO { outerClosed = true })

val async = Async[Resource[IO, *]].async[Unit] { cb =>
(inner *> Resource.eval(IO(cb(Right(()))))).as(None)
(inner *> Resource.eval(IO(cb(Right(()))))).map(_ => None)
}

(outerInit *> async *> waitR).use_.unsafeToFuture()
Expand Down Expand Up @@ -933,11 +933,11 @@ class ResourceSpec extends BaseSpec with ScalaCheck with Discipline {
val winner = Resource
.make(IO.unit)(_ => IO { winnerClosed = true })
.evalMap(_ => IO.sleep(100.millis))
.as("winner")
.map(_ => "winner")
val loser = Resource
.make(IO.unit)(_ => IO { loserClosed = true })
.evalMap(_ => IO.sleep(200.millis))
.as("loser")
.map(_ => "loser")

val target =
winner.race(loser).evalMap(e => IO { results = e }) *> waitR *> Resource.eval(IO {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package cats
package effect
package kernel

import cats.{Eq, Show}
import cats.Eq
import cats.data.State

import scala.concurrent.duration._
Expand Down Expand Up @@ -148,6 +148,7 @@ class LensRefSpec extends BaseSpec with DetectPlatform { outer =>

op must completeAs((false, Foo(5, -1)))
}
else ()

"tryModify - successfully modifies underlying Ref" in ticked { implicit ticker =>
val op = for {
Expand Down Expand Up @@ -180,6 +181,7 @@ class LensRefSpec extends BaseSpec with DetectPlatform { outer =>

op must completeAs((None, Foo(5, -1)))
}
else ()

"tryModifyState - successfully modifies underlying Ref" in ticked { implicit ticker =>
val op = for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package cats
package effect
package kernel

import cats.syntax.all._

import scala.concurrent.duration._

class MiniSemaphoreSpec extends BaseSpec { outer =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class RefSpec extends BaseSpec with DetectPlatform { outer =>

op must completeAs(false)
}
else ()

"tryModifyState - modification occurs successfully" in ticked { implicit ticker =>
val op = for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package cats
package effect
package std

import cats.syntax.all._

import org.specs2.specification.core.Fragments

import scala.concurrent.duration._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ class DispatcherSpec extends BaseSpec with DetectPlatform {
_ <- IO(if (rogueResult == false) {
// if the rogue task is not completed then we must have failed to submit it
rogueSubmitResult must beLeft
()
})
} yield ok
}
Expand Down

0 comments on commit 3ccef16

Please sign in to comment.