summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Kossakowski <grzegorz.kossakowski@gmail.com>2014-11-26 23:07:17 +0100
committerGrzegorz Kossakowski <grzegorz.kossakowski@gmail.com>2014-11-26 23:07:17 +0100
commit0020ac24cc48f82638184d6ce0e639d082d722a4 (patch)
tree5e353d0c352e3d65954a541bf53ec9ed6002e455
parent6f5b853dda0b00c65776b05abde23ceb03d9621d (diff)
parent1f8e81d8993aed7f5b1635384a0a48e265d0dcd8 (diff)
downloadscala-0020ac24cc48f82638184d6ce0e639d082d722a4.tar.gz
scala-0020ac24cc48f82638184d6ce0e639d082d722a4.tar.bz2
scala-0020ac24cc48f82638184d6ce0e639d082d722a4.zip
Merge pull request #4159 from Ichoran/issue/8727
SI-8727 Map.filterKeys result's contains and get are inconsistent
-rw-r--r--src/library/scala/collection/MapLike.scala6
-rw-r--r--test/junit/scala/collection/SetMapConsistencyTest.scala11
2 files changed, 16 insertions, 1 deletions
diff --git a/src/library/scala/collection/MapLike.scala b/src/library/scala/collection/MapLike.scala
index 5ec7d5c615..3de30c2c8b 100644
--- a/src/library/scala/collection/MapLike.scala
+++ b/src/library/scala/collection/MapLike.scala
@@ -230,11 +230,15 @@ self =>
protected class FilteredKeys(p: A => Boolean) extends AbstractMap[A, B] with DefaultMap[A, B] {
override def foreach[C](f: ((A, B)) => C): Unit = for (kv <- self) if (p(kv._1)) f(kv)
def iterator = self.iterator.filter(kv => p(kv._1))
- override def contains(key: A) = self.contains(key) && p(key)
+ override def contains(key: A) = p(key) && self.contains(key)
def get(key: A) = if (!p(key)) None else self.get(key)
}
/** Filters this map by retaining only keys satisfying a predicate.
+ *
+ * '''Note''': the predicate must accept any key of type `A`, not just those already
+ * present in the map, as the predicate is tested before the underlying map is queried.
+ *
* @param p the predicate used to test keys
* @return an immutable map consisting only of those key value pairs of this map where the key satisfies
* the predicate `p`. The resulting map wraps the original map without copying any elements.
diff --git a/test/junit/scala/collection/SetMapConsistencyTest.scala b/test/junit/scala/collection/SetMapConsistencyTest.scala
index 261c11a98b..0749e61c09 100644
--- a/test/junit/scala/collection/SetMapConsistencyTest.scala
+++ b/test/junit/scala/collection/SetMapConsistencyTest.scala
@@ -529,4 +529,15 @@ class SetMapConsistencyTest {
assert(nit == 4)
assert(nfe == 4)
}
+
+ @Test
+ def test_SI8727() {
+ import scala.tools.testing.AssertUtil._
+ type NSEE = NoSuchElementException
+ val map = Map(0 -> "zero", 1 -> "one")
+ val m = map.filterKeys(i => if (map contains i) true else throw new NSEE)
+ assert{ (m contains 0) && (m get 0).nonEmpty }
+ assertThrows[NSEE]{ m contains 2 }
+ assertThrows[NSEE]{ m get 2 }
+ }
}