diff --git a/src/library/scala/collection/generic/GenericCompanion.scala b/src/library/scala/collection/generic/GenericCompanion.scala index eb75fb6a3a53..2df924fccdb0 100644 --- a/src/library/scala/collection/generic/GenericCompanion.scala +++ b/src/library/scala/collection/generic/GenericCompanion.scala @@ -39,7 +39,10 @@ abstract class GenericCompanion[+CC[X] <: GenTraversable[X]] { /** An empty collection of type `$Coll[A]` * @tparam A the type of the ${coll}'s elements */ - def empty[A]: CC[A] = newBuilder[A].result() + def empty[A]: CC[A] = { + if ((this eq immutable.Seq) || (this eq collection.Seq)) Nil.asInstanceOf[CC[A]] + else newBuilder[A].result() + } /** Creates a $coll with the specified elements. * @tparam A the type of the ${coll}'s elements diff --git a/test/junit/scala/collection/SeqTest.scala b/test/junit/scala/collection/SeqTest.scala new file mode 100644 index 000000000000..6bd5e901f4c2 --- /dev/null +++ b/test/junit/scala/collection/SeqTest.scala @@ -0,0 +1,16 @@ +package scala.collection + +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +import scala.tools.testing.AllocationTest + +@RunWith(classOf[JUnit4]) +class SeqTest extends AllocationTest{ + + @Test def emptyNonAllocating(): Unit = { + nonAllocating(Seq.empty) + nonAllocating(Seq()) + } +} diff --git a/test/junit/scala/collection/immutable/ListTest.scala b/test/junit/scala/collection/immutable/ListTest.scala index 1006801029b0..a7ceb9a13065 100644 --- a/test/junit/scala/collection/immutable/ListTest.scala +++ b/test/junit/scala/collection/immutable/ListTest.scala @@ -5,9 +5,10 @@ import org.junit.runner.RunWith import org.junit.runners.JUnit4 import scala.ref.WeakReference +import scala.tools.testing.AllocationTest @RunWith(classOf[JUnit4]) -class ListTest { +class ListTest extends AllocationTest{ /** * Test that empty iterator does not hold reference * to complete List @@ -46,4 +47,9 @@ class ListTest { // real assertion Assert.assertTrue(emptyIterators.exists(_._2.get.isEmpty)) } + + @Test def emptyNonAllocating(): Unit = { + nonAllocating(List.empty) + nonAllocating(List()) + } } diff --git a/test/junit/scala/collection/immutable/SeqTest.scala b/test/junit/scala/collection/immutable/SeqTest.scala new file mode 100644 index 000000000000..dd5a65887267 --- /dev/null +++ b/test/junit/scala/collection/immutable/SeqTest.scala @@ -0,0 +1,16 @@ +package scala.collection.immutable + +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.junit.{Assert, Test} + +import scala.tools.testing.AllocationTest + +@RunWith(classOf[JUnit4]) +class SeqTest extends AllocationTest{ + + @Test def emptyNonAllocating(): Unit = { + nonAllocating(Seq.empty) + nonAllocating(Seq()) + } +}