Skip to content

Commit 547a6f1

Browse files
authored
Merge pull request scala#10810 from som-snytt/issue/13019-treeseqmap-empty
Empty iterator must not mutate
2 parents 9a602e1 + 8e35f82 commit 547a6f1

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/library/scala/collection/immutable/TreeSeqMap.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,11 @@ object TreeSeqMap extends MapFactory[TreeSeqMap] {
392392

393393
if (it != Zero) push(it)
394394

395-
def hasNext = index != 0
395+
def hasNext = index > 0
396396
@tailrec
397397
def next(): V =
398-
pop match {
398+
if (!hasNext) scala.collection.Iterator.empty.next()
399+
else pop match {
399400
case Bin(_,_, Tip(_, v), right) =>
400401
push(right)
401402
v

test/junit/scala/collection/immutable/TreeSeqMapTest.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ class TreeSeqMapTest {
174174
assertEquals(s"modification empty from instance keeps modification order", List(1 -> 3, 3 -> 4), e3.toList)
175175
}
176176
}
177+
@Test
178+
def `t13019 empty.iterator is idempotent`: Unit = {
179+
val m = util.Try(TreeSeqMap.empty.iterator.next())
180+
assertTrue(m.isFailure)
181+
assertFalse("empty iterator does not have next", TreeSeqMap.empty.iterator.hasNext)
182+
}
177183
}
178184
object TreeSeqMapTest extends App {
179185
import TreeSeqMap.Ordering._

0 commit comments

Comments
 (0)