Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented submarine error propagation for Handle #619

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -50,3 +50,9 @@ rewriteTokens {
"→": "->"
"←": "<-"
}

fileOverride {
"glob:**/scala-3/cats/mtl/**" {
runner.dialect = scala3
}
}
20 changes: 20 additions & 0 deletions core/src/main/scala-2/cats/mtl/HandleVariant.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2021 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
package mtl

trait HandleVariant
47 changes: 47 additions & 0 deletions core/src/main/scala-3/cats/mtl/HandleVariant.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2021 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
package mtl

trait HandleVariant { this: Handle.type =>
import Handle.Submarine

def allow[E]: AdHocSyntaxWired[E] =
new AdHocSyntaxWired[E]

final class AdHocSyntaxWired[E]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is Scala 3, I think with some clever use of inline we could make all the allocations / boilerplate disappear from the generated code.

Copy link
Member Author

@lenguyenthanh lenguyenthanh Mar 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried inline in 85e418b.

Combine with moving Inner class out of AdhocSyntaxWired, we completely
remove AdhocSyntaxWired footprint from generated bytecode (user's site)

before:

public final class mtl$minussubmarine$package$
implements Serializable {
    public static final mtl$minussubmarine$package$ MODULE$ = new mtl$minussubmarine$package$();

    private mtl$minussubmarine$package$() {
    }

    private Object writeReplace() {
        return new ModuleSerializationProxy(mtl$minussubmarine$package$.class);
    }

    public EitherT<Eval, Throwable, String> test() {
        return (EitherT)Handle$.MODULE$.allow() // `allow` return new AdHocSyntaxWired

    .apply((Function1 & Serializable)contextual$1 -> {
            Error$ error$ = (Error$)all$.MODULE$.toRaiseOps((Object)Error$.MODULE$);
            return (EitherT)package.all$.MODULE$.toFunctorOps(RaiseOps$.MODULE$.raise$extension((Object)error$, (Raise)contextual$1), (Functor)EitherT$.MODULE$.catsDataMonadErrorForEitherT((Monad)Eval$.MODULE$.catsBimonadForEval())).as((Object)"nope");
        }, (ApplicativeError)EitherT$.MODULE$.catsDataMonadErrorForEitherT((Monad)Eval$.MODULE$.catsBimonadForEval()))

    .rescue((Function1 & Serializable)x$1 -> {
            Error$ error$ = x$1;
            if (Error$.MODULE$.equals(error$)) {
                String string = (String)package.all$.MODULE$.catsSyntaxApplicativeId((Object)"error");
                return (EitherT)ApplicativeIdOps$.MODULE$.pure$extension((Object)string, (Applicative)EitherT$.MODULE$.catsDataMonadErrorForEitherT((Monad)Eval$.MODULE$.catsBimonadForEval()));
            }
            throw new MatchError((Object)error$);
        });
    }
}

after:

public final class mtl$minussubmarine$package$
implements Serializable {
    public static final mtl$minussubmarine$package$ MODULE$ = new mtl$minussubmarine$package$();

    private mtl$minussubmarine$package$() {
    }

    private Object writeReplace() {
        return new ModuleSerializationProxy(mtl$minussubmarine$package$.class);
    }

    public EitherT<Eval, Throwable, String> test() {
        Handle$ HandleCrossCompat_this = Handle$.MODULE$;
        Handle$ HandleCrossCompat_this2 = Handle$.MODULE$;
        MonadError x$2$proxy1 = EitherT$.MODULE$.catsDataMonadErrorForEitherT((Monad)Eval$.MODULE$.catsBimonadForEval());

        return (EitherT)new HandleCrossCompat.InnerWired((HandleCrossCompat)HandleCrossCompat_this2, (Function1 & Serializable)evidence$1 -> {

            Error$ error$ = (Error$)all$.MODULE$.toRaiseOps((Object)Error$.MODULE$);
            return (EitherT)package.all$.MODULE$.toFunctorOps(RaiseOps$.MODULE$.raise$extension((Object)error$, (Raise)evidence$1), (Functor)EitherT$.MODULE$.catsDataMonadErrorForEitherT((Monad)Eval$.MODULE$.catsBimonadForEval())).as((Object)"nope");
        }, (ApplicativeError)x$2$proxy1).rescue((Function1 & Serializable)x$1 -> {
            Error$ error$ = x$1;
            if (Error$.MODULE$.equals(error$)) {
                String string = (String)package.all$.MODULE$.catsSyntaxApplicativeId((Object)"error");
                return (EitherT)ApplicativeIdOps$.MODULE$.pure$extension((Object)string, (Applicative)EitherT$.MODULE$.catsDataMonadErrorForEitherT((Monad)Eval$.MODULE$.catsBimonadForEval()));
            }
            throw new MatchError((Object)error$);
        });
    }
}

scala code for both:

//> using scala 3.nightly
//> using dep org.typelevel::cats-mtl:1.5.0-89-85e418b-20250308T182332Z-SNAPSHOT
//> using dep org.typelevel::cats-core:2.13.0
//> using options -explain

import cats.Eval
import cats.data.EitherT
import cats.mtl.Handle.*
import cats.mtl.syntax.all.*
import cats.syntax.all.*

object Error

type F[A] = EitherT[Eval, Throwable, A]

def test1 =
  allow[Error.type]:
    Error.raise[F, String].as("nope")
  .rescue:
    case Error => "error".pure[F]

Copy link
Member Author

@lenguyenthanh lenguyenthanh Mar 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another fancy way to do it is using polymorphic function 40a7772:

-  inline def allow[E]: AdHocSyntaxWired[E] =
-    new AdHocSyntaxWired[E]()
-
-  private[mtl] final class AdHocSyntaxWired[E]:
-    inline def apply[F[_], A](inline body: Handle[F, E] ?=> F[A]): InnerWired[F, E, A] =
-      new InnerWired(body)
+  def allow[E]: [F[_], A] => (Handle[F, E] ?=> F[A]) => InnerWired[F, E, A] =
+    [F[_], A] => (body: Handle[F, E] ?=> F[A]) => InnerWired(body)

Generated code is a bit nicer compare to inline solution:

public final class mtl$minussubmarine$package$
implements Serializable {
    public static final mtl$minussubmarine$package$ MODULE$ = new mtl$minussubmarine$package$();

    private mtl$minussubmarine$package$() {
    }

    private Object writeReplace() {
        return new ModuleSerializationProxy(mtl$minussubmarine$package$.class);
    }

    public EitherT<Eval, Throwable, String> test1() {

        return (EitherT)((HandleCrossCompat.InnerWired)Handle$.MODULE$.allow().apply((Function1 & Serializable)contextual$1 -> {
            Error$ error$ = (Error$)all$.MODULE$.toRaiseOps((Object)Error$.MODULE$);
            return (EitherT)package.all$.MODULE$.toFunctorOps(RaiseOps$.MODULE$.raise$extension((Object)error$, (Raise)contextual$1), (Functor)EitherT$.MODULE$.catsDataMonadErrorForEitherT((Monad)Eval$.MODULE$.catsBimonadForEval())).as((Object)"nope");
        })).rescue((Function1 & Serializable)x$1 -> {
            Error$ error$ = x$1;
            if (Error$.MODULE$.equals(error$)) {
                String string = (String)package.all$.MODULE$.catsSyntaxApplicativeId((Object)"error");
                return (EitherT)ApplicativeIdOps$.MODULE$.pure$extension((Object)string, (Applicative)EitherT$.MODULE$.catsDataMonadErrorForEitherT((Monad)Eval$.MODULE$.catsBimonadForEval()));
            }
            throw new MatchError((Object)error$);
        }, (ApplicativeError)EitherT$.MODULE$.catsDataMonadErrorForEitherT((Monad)Eval$.MODULE$.catsBimonadForEval()));
    }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! It looks like we are still allocating InnerWired, do you think we can make that one go away too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! It looks like we are still allocating InnerWired, do you think we can make that one go away too?

I tried but couldn't find a way. opaque doesn't work because opaque doesn't work with context function.

I tried with value class by extending AnyVal because of Value classes may not be a member of another class error.

Maybe there is another way? Let me try some more.

btw, which approach do you prefer polymorphic function or inlining?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right, let me investigate.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hit a compiler crash while trying to make InnerWired extends AnyVal: scala/scala3#22752

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, which approach do you prefer polymorphic function or inlining?

I think the polymorphic function is allocating something? to call the apply on?

Handle$.MODULE$.allow().apply

yes it is:

    default public <E> Function1 allow() {
        return new Function1(this){
            private final /* synthetic */ HandleCrossCompat $outer;
            {
                if ($outer == null) {
                    throw new NullPointerException();
                }
                this.$outer = $outer;
            }

            public Object apply(Object body) {
                return new InnerWired<F, E, A>(this.$outer, arg_0 -> HandleCrossCompat.cats$mtl$HandleCrossCompat$$anon$1$$_$apply$$anonfun$1(body, arg_0));
            }

            private static /* synthetic */ Object $deserializeLambda$(SerializedLambda serializedLambda) {
                return LambdaDeserialize.bootstrap("lambdaDeserialize", new MethodHandle[]{cats$mtl$HandleCrossCompat$$anon$1$$_$apply$$anonfun$1(java.lang.Object cats.mtl.Handle )}, serializedLambda);
            }
        };
    }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried extension function to replace InnerWired like this:

private[mtl] trait HandleCrossCompat { this: Handle.type =>

  inline def allow[E]: AdHocSyntaxWired[E] =
    new AdHocSyntaxWired[E]()

  private[mtl] final class AdHocSyntaxWired[E]:

    inline def apply[F[_], A](inline body: Handle[F, E] ?=> F[A]): HF[F, E, A] =
      body
}

type HF[F[_], E, A] = Handle[F, E] ?=> F[A]
object HF:
  import Handle.Submarine
  extension [F[_], E, A](body: HF[F, E, A])
    def rescue(h: E => F[A]): ApplicativeThrow[F] ?=> F[A] =
      val Marker = new AnyRef

      def inner[B](fb: F[B])(f: E => F[B]): F[B] =
        ApplicativeThrow[F].handleErrorWith(fb):
          case Submarine(e, Marker) => f(e.asInstanceOf[E])
          case t => ApplicativeThrow[F].raiseError(t)

      given Handle[F, E] with
        def applicative = Applicative[F]
        def raise[E2 <: E, B](e: E2): F[B] =
          ApplicativeThrow[F].raiseError(Submarine(e, Marker))
        def handleWith[B](fb: F[B])(f: E => F[B]): F[B] = inner(fb)(f)

      inner(body)(h)

but this also failed miserably, because allow will eagerly try to consume Handle[F,E], so we can't never reach rescue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@armanbilge do you have any idea to avoid InnerWired, I'm out of idea 😓


def apply[F[_], A](body: Handle[F, E] ?=> F[A])(using ApplicativeThrow[F]): Inner[F, A] =
new Inner(body)

final class Inner[F[_], A](body: Handle[F, E] ?=> F[A])(using ApplicativeThrow[F]):
def rescue(h: E => F[A]): F[A] =
val Marker = new AnyRef

def inner[B](fb: F[B])(f: E => F[B]): F[B] =
ApplicativeThrow[F].handleErrorWith(fb):
case Submarine(e, Marker) => f(e.asInstanceOf[E])
case t => ApplicativeThrow[F].raiseError(t)

given Handle[F, E] with
def applicative = Applicative[F]
def raise[E2 <: E, B](e: E2): F[B] =
ApplicativeThrow[F].raiseError(Submarine(e, Marker))
def handleWith[B](fb: F[B])(f: E => F[B]): F[B] = inner(fb)(f)

inner(body)(h)
}
38 changes: 37 additions & 1 deletion core/src/main/scala/cats/mtl/Handle.scala
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ package mtl
import cats.data._

import scala.annotation.implicitNotFound
import scala.util.control.NoStackTrace

@implicitNotFound(
"Could not find an implicit instance of Handle[${F}, ${E}]. If you\nhave a good way of handling errors of type ${E} at this location, you may want\nto construct a value of type EitherT for this call-site, rather than ${F}.\nAn example type:\n\n EitherT[${F}, ${E}, *]\n\nThis is analogous to writing try/catch around this call. The EitherT will\n\"catch\" the errors of type ${E}.\n\nIf you do not wish to handle errors of type ${E} at this location, you should\nadd an implicit parameter of this type to your function. For example:\n\n (implicit fhandle: Handle[${F}, ${E}}])\n")
@@ -217,6 +218,41 @@ private[mtl] trait HandleInstances extends HandleLowPriorityInstances {
}
}

object Handle extends HandleInstances {
object Handle extends HandleInstances with HandleVariant {

def apply[F[_], E](implicit ev: Handle[F, E]): Handle[F, E] = ev

def allowF[F[_], E]: AdHocSyntaxTired[F, E] =
new AdHocSyntaxTired[F, E]

final class AdHocSyntaxTired[F[_], E] {

def apply[A](body: Handle[F, E] => F[A])(implicit F: ApplicativeThrow[F]): Inner[A] =
new Inner(body)

final class Inner[A](body: Handle[F, E] => F[A])(implicit F: ApplicativeThrow[F]) {
def rescue(h: E => F[A]): F[A] = {
val Marker = new AnyRef

def inner[B](fb: F[B])(f: E => F[B]): F[B] =
ApplicativeThrow[F].handleErrorWith(fb) {
case Submarine(e, Marker) => f(e.asInstanceOf[E])
case t => ApplicativeThrow[F].raiseError(t)
}

val fa = body(new Handle[F, E] {
def applicative = Applicative[F]
def raise[E2 <: E, B](e: E2): F[B] =
ApplicativeThrow[F].raiseError(Submarine(e, Marker))
def handleWith[B](fb: F[B])(f: E => F[B]): F[B] = inner(fb)(f)
})

inner(fa)(h)
}
}
}

private[mtl] final case class Submarine[E](e: E, marker: AnyRef)
extends RuntimeException
with NoStackTrace
}
59 changes: 59 additions & 0 deletions tests/shared/src/test/scala-3/cats/mtl/tests/Handle3Tests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2021 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
package mtl
package tests

import cats.data.EitherT
import cats.mtl.syntax.all.*
import cats.syntax.all.*
import cats.mtl.Handle.*

class Handle3Tests extends BaseSuite:

type F[A] = EitherT[Eval, Throwable, A]

test("submerge custom errors (scala 3)"):
enum Error:
case First, Second, Third

val test =
allow[Error]:
Error.Second.raise[F, String].as("nope")
.rescue:
case Error.First => "0".pure[F]
case Error.Second => "1".pure[F]
case Error.Third => "2".pure[F]

assert(test.value.value.toOption == Some("1"))

test("submerge two independent errors (scala 3)"):
enum Error1:
case First, Second, Third
enum Error2:
case Fourth
val test =
allow[Error1]:
allow[Error2]:
Error1.Third.raise[F, String].as("nope")
.rescue:
case e => e.toString.pure[F]
.rescue:
case Error1.First => "first1".pure[F]
case Error1.Second => "second1".pure[F]
case Error1.Third => "third1".pure[F]
assert(test.value.value.toOption == Some("third1"))
85 changes: 84 additions & 1 deletion tests/shared/src/test/scala/cats/mtl/tests/HandleTests.scala
Original file line number Diff line number Diff line change
@@ -18,9 +18,16 @@ package cats
package mtl
package tests

import cats.data.{Kleisli, WriterT}
import cats.data.{EitherT, Kleisli, WriterT}
import cats.laws.discipline.arbitrary._
import cats.mtl.syntax.all._
import cats.syntax.all._

import org.scalacheck.{Arbitrary, Cogen}, Arbitrary.arbitrary

class HandleTests extends BaseSuite {
type F[A] = EitherT[Eval, Throwable, A]

test("handleForApplicativeError") {
case class Foo[A](bar: A)

@@ -39,4 +46,80 @@ class HandleTests extends BaseSuite {
Handle[Kleisli[Foo, Unit, *], String]
Handle[WriterT[Foo, String, *], String]
}

test("submerge custom errors") {
sealed trait Error extends Product with Serializable

object Error {
case object First extends Error
case object Second extends Error
case object Third extends Error
}

val test =
Handle.allowF[F, Error](implicit h => Error.Second.raise.as("nope")) rescue {
case Error.First => "0".pure[F]
case Error.Second => "1".pure[F]
case Error.Third => "2".pure[F]
}

assert(test.value.value.toOption == Some("1"))
}

test("submerge two independent errors") {
sealed trait Error1 extends Product with Serializable

object Error1 {
case object First extends Error1
case object Second extends Error1
case object Third extends Error1
}

sealed trait Error2 extends Product with Serializable

val test = Handle.allowF[F, Error1] { implicit h1 =>
Handle.allowF[F, Error2] { implicit h2 =>
val _ =
h2 // it's helpful to test the raise syntax infers even when multiple handles are present
Error1.Third.raise.as("nope")
} rescue { e => e.toString.pure[F] }
} rescue {
case Error1.First => "first1".pure[F]
case Error1.Second => "second1".pure[F]
case Error1.Third => "third1".pure[F]
}

assert(test.value.value.toOption == Some("third1"))
}

{
final case class Error(value: Int)

object Error {
implicit val arbError: Arbitrary[Error] =
Arbitrary(arbitrary[Int].flatMap(Error(_)))

implicit val cogenError: Cogen[Error] =
Cogen((_: Error).value.toLong)

implicit val eqError: Eq[Error] =
Eq.by((_: Error).value)
}

implicit val eqThrowable: Eq[Throwable] =
Eq.fromUniversalEquals[Throwable]

val test = Handle.allowF[F, Error] { implicit h =>
EitherT liftF {
Eval later {
checkAll(
"Handle.allowF[F, Error]",
cats.mtl.laws.discipline.HandleTests[F, Error].handle[Int])
}
}
} rescue { case Error(_) => ().pure[F] }

test.value.value
()
}
}