From 99fb2b420f2fbf0eca5a98d0e52f8c6b580cd18f Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Fri, 17 Sep 2010 00:16:33 +0000 Subject: Does what can probably be done about strange it... Does what can probably be done about strange iterator exhaustion behavior. Maybe we should start thinking about iteratees... Closes #3760, no review. --- src/library/scala/collection/LinearSeqLike.scala | 10 +++++++++- src/library/scala/collection/immutable/Stream.scala | 6 +++++- test/files/run/bug3760.scala | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 test/files/run/bug3760.scala diff --git a/src/library/scala/collection/LinearSeqLike.scala b/src/library/scala/collection/LinearSeqLike.scala index c2c4996f47..8bf45348db 100644 --- a/src/library/scala/collection/LinearSeqLike.scala +++ b/src/library/scala/collection/LinearSeqLike.scala @@ -56,6 +56,14 @@ trait LinearSeqLike[+A, +Repr <: LinearSeqLike[A, Repr]] extends SeqLike[A, Repr if (hasNext) { val result = these.head; these = these.tail; result } else Iterator.empty.next - override def toList: List[A] = these.toList + + /** Have to clear these so the iterator is exhausted like + * it would be without the optimization. + */ + override def toList: List[A] = { + val xs = these.toList + these = newBuilder.result + xs + } } } diff --git a/src/library/scala/collection/immutable/Stream.scala b/src/library/scala/collection/immutable/Stream.scala index 5339b29e7c..02a17427ef 100644 --- a/src/library/scala/collection/immutable/Stream.scala +++ b/src/library/scala/collection/immutable/Stream.scala @@ -262,7 +262,11 @@ self => these = new LazyCell(cur.tail) result } - override def toStream = these.v + override def toStream = { + val result = these.v + these = new LazyCell(Stream.empty) + result + } override def toList = toStream.toList } diff --git a/test/files/run/bug3760.scala b/test/files/run/bug3760.scala new file mode 100644 index 0000000000..b78406824e --- /dev/null +++ b/test/files/run/bug3760.scala @@ -0,0 +1,17 @@ +object Test { + def main(args: Array[String]): Unit = { + { + val it = Iterable(1,2).iterator + val xs = it.toList + + assert(it.isEmpty) + } + + { + val it = Iterator(1, 2) + val xs = it.toStream.toList + + assert(it.isEmpty) + } + } +} -- cgit v1.2.3