summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2013-12-10 00:45:48 +0100
committerJason Zaugg <jzaugg@gmail.com>2013-12-10 00:56:26 +0100
commit006e2f2aadf5d15ff1b9b32f1b7e96960b778933 (patch)
tree9b15e2049fb10410aae1ec8606257e716fd4828d
parent0c927046dc5df974e6c39187107cf3548825282b (diff)
downloadscala-006e2f2aadf5d15ff1b9b32f1b7e96960b778933.tar.gz
scala-006e2f2aadf5d15ff1b9b32f1b7e96960b778933.tar.bz2
scala-006e2f2aadf5d15ff1b9b32f1b7e96960b778933.zip
SI-7912 Be defensive calling `toString` in `MatchError#getMessage`
Otherwise, objects with exception-throwing `toString` lead to a cascading error far removed from the originally failed match.
-rw-r--r--src/library/scala/MatchError.scala10
-rw-r--r--test/files/run/t7912.scala16
2 files changed, 24 insertions, 2 deletions
diff --git a/src/library/scala/MatchError.scala b/src/library/scala/MatchError.scala
index 6ba7e833d3..9965bb19b5 100644
--- a/src/library/scala/MatchError.scala
+++ b/src/library/scala/MatchError.scala
@@ -23,9 +23,15 @@ final class MatchError(obj: Any) extends RuntimeException {
/** There's no reason we need to call toString eagerly,
* so defer it until getMessage is called.
*/
- private lazy val objString =
+ private lazy val objString = {
+ def ofClass = "of class " + obj.getClass.getName
if (obj == null) "null"
- else obj.toString() + " (of class " + obj.getClass.getName + ")"
+ else try {
+ obj.toString() + " (" + ofClass + ")"
+ } catch {
+ case _: Throwable => "an instance " + ofClass
+ }
+ }
override def getMessage() = objString
}
diff --git a/test/files/run/t7912.scala b/test/files/run/t7912.scala
new file mode 100644
index 0000000000..3d603e0e97
--- /dev/null
+++ b/test/files/run/t7912.scala
@@ -0,0 +1,16 @@
+case object A { override def toString = ??? }
+
+object Test {
+ def foo: Int = (A: Any) match {
+ case 0 => 0
+ }
+ def main(args: Array[String]): Unit = {
+ try {
+ foo
+ sys.error("no exception")
+ } catch {
+ case me: MatchError => assert(me.getMessage == "an instance of class A$", me.getMessage)
+ case ex: Throwable => sys.error("not a match error: " + ex.getClass)
+ }
+ }
+}