aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorNicolas Stucki <nicolas.stucki@gmail.com>2016-06-10 11:14:17 +0200
committerNicolas Stucki <nicolas.stucki@gmail.com>2016-06-28 11:08:53 +0200
commitb0ebe6ad30ce2584aa221b3ed8d10042bd9e97ac (patch)
treef7072a30b533821762dc0a18e5a5ba3e76e4ead5 /tests
parentcc87bd3b452e76ae974504c4f4e6beba996be3e8 (diff)
downloaddotty-b0ebe6ad30ce2584aa221b3ed8d10042bd9e97ac.tar.gz
dotty-b0ebe6ad30ce2584aa221b3ed8d10042bd9e97ac.tar.bz2
dotty-b0ebe6ad30ce2584aa221b3ed8d10042bd9e97ac.zip
Fix #856: Handle try/catch cases as catch cases if possible.
Previously they were all lifted into a match with the came cases. Now the first cases are handled directly by by the catch. If one of the cases can not be handled the old scheme is applied to to it and all subsequent cases.
Diffstat (limited to 'tests')
-rw-r--r--tests/neg/tryPatternMatchError.scala35
-rw-r--r--tests/run/tryPatternMatch.check20
-rw-r--r--tests/run/tryPatternMatch.scala139
3 files changed, 194 insertions, 0 deletions
diff --git a/tests/neg/tryPatternMatchError.scala b/tests/neg/tryPatternMatchError.scala
new file mode 100644
index 000000000..fe12a6232
--- /dev/null
+++ b/tests/neg/tryPatternMatchError.scala
@@ -0,0 +1,35 @@
+import java.io.IOException
+import java.lang.NullPointerException
+import java.lang.IllegalArgumentException
+
+object IAE {
+ def unapply(e: Exception): Option[String] =
+ if (e.isInstanceOf[IllegalArgumentException]) Some(e.getMessage)
+ else None
+}
+
+object EX extends Exception
+
+trait ExceptionTrait extends Exception
+
+object Test {
+ def main(args: Array[String]): Unit = {
+ var a: Int = 1
+ try {
+ throw new IllegalArgumentException()
+ } catch {
+ case e: IOException if e.getMessage == null =>
+ case e: NullPointerException =>
+ case e: IndexOutOfBoundsException =>
+ case _: NoSuchElementException =>
+ case _: ExceptionTrait =>
+ case _: NoSuchElementException if a <= 1 =>
+ case _: NullPointerException | _:IOException =>
+ case `a` => // This case should probably emmit an error
+ case e: Int => // error
+ case EX =>
+ case IAE(msg) =>
+ case e: IllegalArgumentException =>
+ }
+ }
+}
diff --git a/tests/run/tryPatternMatch.check b/tests/run/tryPatternMatch.check
new file mode 100644
index 000000000..44f7b7d5a
--- /dev/null
+++ b/tests/run/tryPatternMatch.check
@@ -0,0 +1,20 @@
+success 1
+success 2
+success 3
+success 4
+success 5
+success 6
+success 7
+success 8
+success 9.1
+success 9.2
+IllegalArgumentException: abc
+IllegalArgumentException
+NullPointerException | IOException
+NoSuchElementException
+EX
+InnerException
+NullPointerException
+ExceptionTrait
+ClassCastException
+TimeoutException escaped
diff --git a/tests/run/tryPatternMatch.scala b/tests/run/tryPatternMatch.scala
new file mode 100644
index 000000000..06b469d4d
--- /dev/null
+++ b/tests/run/tryPatternMatch.scala
@@ -0,0 +1,139 @@
+import java.io.IOException
+import java.util.concurrent.TimeoutException
+
+object IAE {
+ def unapply(e: Exception): Option[String] =
+ if (e.isInstanceOf[IllegalArgumentException] && e.getMessage != null) Some(e.getMessage)
+ else None
+}
+
+object EX extends Exception {
+ val msg = "a"
+ class InnerException extends Exception(msg)
+}
+
+trait ExceptionTrait extends Exception
+
+trait TestTrait {
+ type ExceptionType <: Exception
+
+ def traitTest(): Unit = {
+ try {
+ throw new IOException
+ } catch {
+ case _: ExceptionType => println("success 9.2")
+ case _ => println("failed 9.2")
+ }
+ }
+}
+
+object Test extends TestTrait {
+ type ExceptionType = IOException
+
+ def main(args: Array[String]): Unit = {
+ var a: Int = 1
+
+ try {
+ throw new Exception("abc")
+ } catch {
+ case _: Exception => println("success 1")
+ case _ => println("failed 1")
+ }
+
+ try {
+ throw new Exception("abc")
+ } catch {
+ case e: Exception => println("success 2")
+ case _ => println("failed 2")
+ }
+
+ try {
+ throw new Exception("abc")
+ } catch {
+ case e: Exception if e.getMessage == "abc" => println("success 3")
+ case _ => println("failed 3")
+ }
+
+ try {
+ throw new Exception("abc")
+ } catch {
+ case e: Exception if e.getMessage == "" => println("failed 4")
+ case _ => println("success 4")
+ }
+
+ try {
+ throw EX
+ } catch {
+ case EX => println("success 5")
+ case _ => println("failed 5")
+ }
+
+ try {
+ throw new EX.InnerException
+ } catch {
+ case _: EX.InnerException => println("success 6")
+ case _ => println("failed 6")
+ }
+
+ try {
+ throw new NullPointerException
+ } catch {
+ case _: NullPointerException | _:IOException => println("success 7")
+ case _ => println("failed 7")
+ }
+
+ try {
+ throw new ExceptionTrait {}
+ } catch {
+ case _: ExceptionTrait => println("success 8")
+ case _ => println("failed 8")
+ }
+
+ try {
+ throw new IOException
+ } catch {
+ case _: ExceptionType => println("success 9.1")
+ case _ => println("failed 9.1")
+ }
+
+ traitTest() // test 9.2
+
+ def testThrow(throwIt: => Unit): Unit = {
+ try {
+ throwIt
+ } catch {
+ // These cases will be compiled as catch cases
+ case e: NullPointerException => println("NullPointerException")
+ case e: IndexOutOfBoundsException => println("IndexOutOfBoundsException")
+ case _: NoSuchElementException => println("NoSuchElementException")
+ case _: EX.InnerException => println("InnerException")
+ // All the following will be compiled as a match
+ case IAE(msg) => println("IllegalArgumentException: " + msg)
+ case _: ExceptionTrait => println("ExceptionTrait")
+ case e: IOException if e.getMessage == null => println("IOException")
+ case _: NullPointerException | _:IOException => println("NullPointerException | IOException")
+ case `a` => println("`a`")
+ case EX => println("EX")
+ case e: IllegalArgumentException => println("IllegalArgumentException")
+ case _: ClassCastException => println("ClassCastException")
+ }
+ }
+
+ testThrow(throw new IllegalArgumentException("abc"))
+ testThrow(throw new IllegalArgumentException())
+ testThrow(throw new IOException("abc"))
+ testThrow(throw new NoSuchElementException())
+ testThrow(throw EX)
+ testThrow(throw new EX.InnerException)
+ testThrow(throw new NullPointerException())
+ testThrow(throw new ExceptionTrait {})
+ testThrow(throw a.asInstanceOf[Throwable])
+ try {
+ testThrow(throw new TimeoutException)
+ println("TimeoutException did not escape")
+ } catch {
+ case _: TimeoutException => println("TimeoutException escaped")
+ }
+ }
+
+}