diff --git a/build.sbt b/build.sbt index 82a522f7a7..679df2d278 100644 --- a/build.sbt +++ b/build.sbt @@ -648,7 +648,10 @@ lazy val core = crossProject(JSPlatform, JVMPlatform, NativePlatform) ProblemFilters.exclude[IncompatibleResultTypeProblem]( "cats.effect.unsafe.TimerSkipList.insert"), ProblemFilters.exclude[IncompatibleResultTypeProblem]( - "cats.effect.unsafe.WorkerThread.sleep") + "cats.effect.unsafe.WorkerThread.sleep"), + // #3787, internal utility that was no longer needed + ProblemFilters.exclude[MissingClassProblem]("cats.effect.Thunk"), + ProblemFilters.exclude[MissingClassProblem]("cats.effect.Thunk$") ) ++ { if (tlIsScala3.value) { // Scala 3 specific exclusions diff --git a/core/jvm/src/main/scala/cats/effect/IOCompanionPlatform.scala b/core/jvm/src/main/scala/cats/effect/IOCompanionPlatform.scala index cf3a5303ac..ad9d75c8b7 100644 --- a/core/jvm/src/main/scala/cats/effect/IOCompanionPlatform.scala +++ b/core/jvm/src/main/scala/cats/effect/IOCompanionPlatform.scala @@ -47,14 +47,14 @@ private[effect] abstract class IOCompanionPlatform { this: IO.type => * Implements [[cats.effect.kernel.Sync.blocking]]. */ def blocking[A](thunk: => A): IO[A] = { - val fn = Thunk.asFunction0(thunk) + val fn = () => thunk Blocking(TypeBlocking, fn, Tracing.calculateTracingEvent(fn.getClass)) } // this cannot be marked private[effect] because of static forwarders in Java @deprecated("use interruptible / interruptibleMany instead", "3.3.0") def interruptible[A](many: Boolean, thunk: => A): IO[A] = { - val fn = Thunk.asFunction0(thunk) + val fn = () => thunk Blocking( if (many) TypeInterruptibleMany else TypeInterruptibleOnce, fn, @@ -80,7 +80,7 @@ private[effect] abstract class IOCompanionPlatform { this: IO.type => * Implements [[cats.effect.kernel.Sync.interruptible[A](thunk:=>A):*]] */ def interruptible[A](thunk: => A): IO[A] = { - val fn = Thunk.asFunction0(thunk) + val fn = () => thunk Blocking(TypeInterruptibleOnce, fn, Tracing.calculateTracingEvent(fn.getClass)) } @@ -104,7 +104,7 @@ private[effect] abstract class IOCompanionPlatform { this: IO.type => * Implements [[cats.effect.kernel.Sync!.interruptibleMany]] */ def interruptibleMany[A](thunk: => A): IO[A] = { - val fn = Thunk.asFunction0(thunk) + val fn = () => thunk Blocking(TypeInterruptibleMany, fn, Tracing.calculateTracingEvent(fn.getClass)) } diff --git a/core/shared/src/main/scala/cats/effect/IO.scala b/core/shared/src/main/scala/cats/effect/IO.scala index 372630f7bb..ea520aa21f 100644 --- a/core/shared/src/main/scala/cats/effect/IO.scala +++ b/core/shared/src/main/scala/cats/effect/IO.scala @@ -1130,7 +1130,7 @@ object IO extends IOCompanionPlatform with IOLowPriorityImplicits { * Any exceptions thrown by the effect will be caught and sequenced into the `IO`. */ def delay[A](thunk: => A): IO[A] = { - val fn = Thunk.asFunction0(thunk) + val fn = () => thunk Delay(fn, Tracing.calculateTracingEvent(fn)) } diff --git a/core/shared/src/main/scala/cats/effect/Thunk.scala b/core/shared/src/main/scala/cats/effect/Thunk.scala deleted file mode 100644 index d60a39a080..0000000000 --- a/core/shared/src/main/scala/cats/effect/Thunk.scala +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2020-2023 Typelevel - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package cats.effect - -/** - * A utility to convert a by-name `thunk: => A` to a `Function0[A]` (its binary representation). - * Scala 2 performs this optimization automatically but on Scala 3 the thunk is wrapped inside - * of a new `Function0`. See https://github.com/typelevel/cats-effect/pull/2226 - */ -private object Thunk { - private[this] val impl = - ((x: Any) => x).asInstanceOf[(=> Any) => Function0[Any]] - - def asFunction0[A](thunk: => A): Function0[A] = impl(thunk).asInstanceOf[Function0[A]] -} diff --git a/tests/shared/src/test/scala/cats/effect/ThunkSpec.scala b/tests/shared/src/test/scala/cats/effect/ThunkSpec.scala index 92624ab5bd..f51f7b1e98 100644 --- a/tests/shared/src/test/scala/cats/effect/ThunkSpec.scala +++ b/tests/shared/src/test/scala/cats/effect/ThunkSpec.scala @@ -18,11 +18,11 @@ package cats.effect class ThunkSpec extends BaseSpec { - "Thunk.asFunction0" should { + "IO.delay" should { "return the same function" in { var i = 0 val f = () => i += 1 - Thunk.asFunction0(f()) eq f + IO.delay(f()).asInstanceOf[IO.Delay[Unit]].thunk eq f } }