Skip to content

can't try catch using a PartialFunction[Throwable, _] #8662

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

Closed
ohze opened this issue Apr 4, 2020 · 2 comments
Closed

can't try catch using a PartialFunction[Throwable, _] #8662

ohze opened this issue Apr 4, 2020 · 2 comments
Assignees

Comments

@ohze
Copy link

ohze commented Apr 4, 2020

Minimized code

object Main {
  class EThrow extends Exception
  class ECatch extends Exception

  def ok(): Unit =
    try throw new EThrow
    catch { case _: ECatch => }

  def notOk(): Unit = {
    val pf: PartialFunction[Throwable, Unit] = {
      case e: ECatch =>
    }
    try throw new EThrow
    catch pf
  }

  def test(f: => Unit): Unit =
    try f catch {
      case _: EThrow    => println("OK")
      // case e: Throwable => println("! expect an EThrow but got " + e.getClass)
    }

  def main(args: Array[String]): Unit = {
    test { ok() }
    test { notOk() }
  }
}

Runtime output

[error] Exception in thread "main" scala.MatchError: Main$EThrow (of class Main$EThrow)
[error] 	at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:341)
[error] 	at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:339)
[error] 	at Main$$anon$1.applyOrElse(Main.scala:11)
[error] 	at Main$$anon$1.applyOrElse(Main.scala:11)
[error] 	at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:35)
[error] 	at Main$.notOk(Main.scala:14)
[error] 	at Main$.main$$anonfun$2(Main.scala:25)
[error] 	at dotty.runtime.function.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
[error] 	at Main$.test(Main.scala:18)
[error] 	at Main$.main(Main.scala:25)
[error] 	at Main.main(Main.scala)

Expectation

running successfully as in scala 2 (println "OK" 2 times)

@ohze ohze added the itype:bug label Apr 4, 2020
@ohze
Copy link
Author

ohze commented Apr 4, 2020

decompile:

  • dotty:
    public void notOk() {
        final PartialFunction pf = (PartialFunction)new Main$$anon.Main$$anon$1();
        try {
            throw new Main.EThrow();
        } catch (Throwable ex$) {
            pf.apply((Object)ex$);
        }
    }
  • scala 2.13.1:
    public void notOk() {
        final PartialFunction pf = (PartialFunction)new Main$$anonfun.Main$$anonfun$1();
        try {
            throw new Main.EThrow();
        } catch (Throwable x$1) {
            final PartialFunction catchExpr$1 = pf;
            if (catchExpr$1.isDefinedAt((Object)x$1)) {
                catchExpr$1.apply((Object)x$1);
                return;
            }
            throw x$1;
        }
    }

odersky added a commit to dotty-staging/dotty that referenced this issue Apr 4, 2020
giabao added a commit to ohze/akka that referenced this issue Apr 5, 2020
@odersky
Copy link
Contributor

odersky commented Apr 5, 2020

Before, dotty only implemented catch handlers that are plain functions. That was an oversight.

nicolasstucki added a commit that referenced this issue Apr 6, 2020
Fix #8662: Fix desugaring of try-catches that are partial functions
giabao added a commit to ohze/akka that referenced this issue Apr 6, 2020
giabao added a commit to ohze/akka that referenced this issue Apr 7, 2020
giabao added a commit to ohze/akka that referenced this issue Apr 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants