summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVinicius Miana <vinicius@miana.com.br>2013-01-29 14:14:02 -0200
committerVinicius Miana <vinicius@miana.com.br>2013-02-08 14:43:37 -0200
commit7b425bf7f02c15d6c3577f21318c6bdfc92d6b35 (patch)
tree929d9a8929bd7b1472dbf3e424fb35d7e312fb74
parent1a63cf8b9b48c98fa754a1eb6dcfe35220016c74 (diff)
downloadscala-7b425bf7f02c15d6c3577f21318c6bdfc92d6b35.tar.gz
scala-7b425bf7f02c15d6c3577f21318c6bdfc92d6b35.tar.bz2
scala-7b425bf7f02c15d6c3577f21318c6bdfc92d6b35.zip
SI-6370 changed ListMap apply0 method to produce correct error message when a key is not found
Current implementation of apply0 relies on tail method to iterate over all keys. When the list gets to its end, tail produces an 'empty map' message in its exception, which is thrown by ListMap. This change checks if the collection is empty before calling tail and provides a more appropriate key not found message. Signed-off-by: Vinicius Miana <vinicius@miana.com.br>
-rw-r--r--src/library/scala/collection/immutable/ListMap.scala8
-rw-r--r--test/files/run/t6370.scala12
2 files changed, 18 insertions, 2 deletions
diff --git a/src/library/scala/collection/immutable/ListMap.scala b/src/library/scala/collection/immutable/ListMap.scala
index 670540187e..75817350e5 100644
--- a/src/library/scala/collection/immutable/ListMap.scala
+++ b/src/library/scala/collection/immutable/ListMap.scala
@@ -156,8 +156,12 @@ extends AbstractMap[A, B]
* @return the value associated with the given key.
*/
override def apply(k: A): B1 = apply0(this, k)
-
- @tailrec private def apply0(cur: ListMap[A, B1], k: A): B1 = if (k == cur.key) cur.value else apply0(cur.tail, k)
+
+
+ @tailrec private def apply0(cur: ListMap[A, B1], k: A): B1 =
+ if (cur.isEmpty) throw new NoSuchElementException("key not found: "+k)
+ else if (k == cur.key) cur.value
+ else apply0(cur.tail, k)
/** Checks if this map maps `key` to a value and return the
* value if it exists.
diff --git a/test/files/run/t6370.scala b/test/files/run/t6370.scala
new file mode 100644
index 0000000000..c86b87dc8a
--- /dev/null
+++ b/test/files/run/t6370.scala
@@ -0,0 +1,12 @@
+object Test {
+
+ def main(args: Array[String]): Unit = {
+ val m = collection.immutable.ListMap( "x" -> 1 )
+ try {
+ m("y")
+ } catch {
+ case e : NoSuchElementException => assert(e.getMessage() == "key not found: y")
+ }
+
+ }
+}