From 3adaa37cd2fc40deeb58ce9da9015f329d9c254e Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Sat, 28 May 2011 23:28:11 +0000 Subject: Yikes, prependToList was violating List immutab... Yikes, prependToList was violating List immutability. Closes #4656, no review. --- src/library/scala/collection/mutable/ListBuffer.scala | 9 +++++++-- test/files/run/bug4656.check | 1 + test/files/run/bug4656.scala | 13 +++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 test/files/run/bug4656.check create mode 100644 test/files/run/bug4656.scala diff --git a/src/library/scala/collection/mutable/ListBuffer.scala b/src/library/scala/collection/mutable/ListBuffer.scala index 6a75108dfb..f1271627c1 100644 --- a/src/library/scala/collection/mutable/ListBuffer.scala +++ b/src/library/scala/collection/mutable/ListBuffer.scala @@ -245,9 +245,14 @@ final class ListBuffer[A] * * @param xs the list to which elements are prepended */ - def prependToList(xs: List[A]): List[A] = + def prependToList(xs: List[A]): List[A] = { if (start.isEmpty) xs - else { last0.tl = xs; toList } + else { + if (exported) copy() + last0.tl = xs + toList + } + } // Overrides of methods in Buffer diff --git a/test/files/run/bug4656.check b/test/files/run/bug4656.check new file mode 100644 index 0000000000..15a62794a9 --- /dev/null +++ b/test/files/run/bug4656.check @@ -0,0 +1 @@ +List(1, 2, 3) diff --git a/test/files/run/bug4656.scala b/test/files/run/bug4656.scala new file mode 100644 index 0000000000..4f3d189c8f --- /dev/null +++ b/test/files/run/bug4656.scala @@ -0,0 +1,13 @@ +object Test { + def f = { + val buf = new collection.mutable.ListBuffer[Int] + buf ++= List(1, 2, 3) + val l = buf.toList + buf prependToList List(4, 5, 6) + l + } + + def main(args: Array[String]): Unit = { + println(f) + } +} -- cgit v1.2.3