From 28a716190c5faf549ed302a1c19d9611c32d2010 Mon Sep 17 00:00:00 2001 From: James Iry Date: Mon, 25 Feb 2013 16:25:22 -0800 Subject: SI-7181 Prepare to remove duplicated finally blocks As a first step towards fixing 7181, this commit improves the comments and variable names around generating try/catch/finally blocks in GenICode and adds a test verifying the current functionality of try/catch/finally blocks --- test/files/run/t7181.check | 23 ++++++++++++++ test/files/run/t7181.scala | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 test/files/run/t7181.check create mode 100644 test/files/run/t7181.scala (limited to 'test/files') diff --git a/test/files/run/t7181.check b/test/files/run/t7181.check new file mode 100644 index 0000000000..e4b8e30dfe --- /dev/null +++ b/test/files/run/t7181.check @@ -0,0 +1,23 @@ +normal exit MainNormalExit +finally MainNormalExit +normal flow MainNormalExit + +return MainReturn +finally MainReturn + +uncaught exception MainUncaughtException +finally MainUncaughtException + +caught exception ExceptionNormalExit +normal exit ExceptionNormalExit +finally ExceptionNormalExit +normal flow ExceptionNormalExit + +caught exception ExceptionReturn +return ExceptionReturn +finally ExceptionReturn + +caught exception ExceptionUncaughtException +uncaught exception ExceptionUncaughtException +finally ExceptionUncaughtException + diff --git a/test/files/run/t7181.scala b/test/files/run/t7181.scala new file mode 100644 index 0000000000..a055e43481 --- /dev/null +++ b/test/files/run/t7181.scala @@ -0,0 +1,78 @@ +sealed abstract class Action +// exit the try body normally +case object MainNormalExit extends Action +// exit the try body with a 'return' +case object MainReturn extends Action +// exit the try body with an uncaught exception +case object MainUncaughtException extends Action +// exit the try body with a caught exception and exit the exception handler normally +case object ExceptionNormalExit extends Action +// exit the try body with a caught exception and exit the exception handler with a 'return' +case object ExceptionReturn extends Action +// exit the try body with a caught exception and exit the exception handler with an uncaught exception +case object ExceptionUncaughtException extends Action + +case class UncaughtException(action: Action) extends RuntimeException +case class CaughtException(action: Action) extends RuntimeException + +object Test extends App { + def test(action: Action, expectException: Boolean = false) { + var gotException = false + val result = try + driver(action) + catch { + case UncaughtException(a) => + gotException = true + a + } + if (gotException) assert(expectException, "Got unexpected exception") + else assert(!expectException, "Did not get expected exception") + + assert(result == action, s"Expected $action but got $result") + println() + } + + def driver(action: Action): Action = { + val result = try { + action match { + case MainNormalExit => + println(s"normal exit $action") + action + case MainReturn => + println(s"return $action") + return action + case MainUncaughtException => + println(s"uncaught exception $action") + throw UncaughtException(action) + case _ => + println(s"caught exception $action") + throw CaughtException(action) + } + } catch { + case CaughtException(action) => action match { + case ExceptionNormalExit => + println(s"normal exit $action") + action + case ExceptionReturn => + println(s"return $action") + return action + case ExceptionUncaughtException => + println(s"uncaught exception $action") + throw UncaughtException(action) + case _ => + sys.error(s"unexpected $action in exception handler") + } + } finally { + println(s"finally $action") + } + println(s"normal flow $action") + result + } + + test(MainNormalExit) + test(MainReturn) + test(MainUncaughtException, true) + test(ExceptionNormalExit) + test(ExceptionReturn) + test(ExceptionUncaughtException, true) +} -- cgit v1.2.3