diff --git a/compat/src/main/scala-2.11_2.12/scala/collection/compat/immutable/LazyList.scala b/compat/src/main/scala-2.11_2.12/scala/collection/compat/immutable/LazyList.scala index 1c82f4c3..65248c2f 100644 --- a/compat/src/main/scala-2.11_2.12/scala/collection/compat/immutable/LazyList.scala +++ b/compat/src/main/scala-2.11_2.12/scala/collection/compat/immutable/LazyList.scala @@ -1277,7 +1277,7 @@ object LazyList extends SeqFactory[LazyList] { /** Construct a LazyList consisting of a given first element followed by elements * from another LazyList. */ - def #::[B >: A](elem: => B): LazyList[B] = newLL(sCons(elem, l())) + def #::[B >: A](elem: => B): LazyList[B] = newLL(sCons(elem, newLL(l().state))) /** Construct a LazyList consisting of the concatenation of the given LazyList and * another LazyList. diff --git a/compat/src/test/scala/test/scala/collection/LazyListLazinessTest.scala b/compat/src/test/scala/test/scala/collection/LazyListLazinessTest.scala index cf9ddc67..c5aec778 100644 --- a/compat/src/test/scala/test/scala/collection/LazyListLazinessTest.scala +++ b/compat/src/test/scala/test/scala/collection/LazyListLazinessTest.scala @@ -217,7 +217,7 @@ class LazyListLazinessTest { } @Test - def lazyAppendedAll_appendedAll_properlyLazy(): Unit = { + def lazyAppendedAll_properlyLazy(): Unit = { genericAppendedColl_properlyLazy(_ lazyAppendedAll _) } @@ -778,7 +778,7 @@ class LazyListLazinessTest { @Test def `#:: properlyLazy`(): Unit = { - val factory = lazyListFactory { init => + val headInitFactory = lazyListFactory { init => def gen(index: Int): LazyList[Int] = { def elem(): Int = { init.evaluate(index); index } if (index >= LazinessChecker.count) LazyList.empty @@ -788,12 +788,25 @@ class LazyListLazinessTest { gen(0) } - assertRepeatedlyLazy(factory) + assertRepeatedlyLazy(headInitFactory) + + val tailInitFactory = lazyListFactory { init => + def gen(index: Int): LazyList[Int] = { + if (index >= LazinessChecker.count) LazyList.empty + else { + init.evaluate(index) + index #:: gen(index + 1) + } + } + + LazyList.empty lazyAppendedAll gen(0) // prevent initial state evaluation + } + assertRepeatedlyLazy(tailInitFactory) } @Test def `#::: properlyLazy`(): Unit = { - val factory = lazyListFactory { init => + val headInitFactory = lazyListFactory { init => def gen(index: Int): LazyList[Int] = { def elem(): LazyList[Int] = LazyList.fill(1) { init.evaluate(index); index } if (index >= LazinessChecker.count) LazyList.empty @@ -802,7 +815,20 @@ class LazyListLazinessTest { gen(0) } - assertRepeatedlyLazy(factory) + assertRepeatedlyLazy(headInitFactory) + + val tailInitFactory = lazyListFactory { init => + def gen(index: Int): LazyList[Int] = { + if (index >= LazinessChecker.count) LazyList.empty + else { + init.evaluate(index) + LazyList.fill(1)(index) #::: gen(index + 1) + } + } + + LazyList.empty lazyAppendedAll gen(0) // prevent initial state evaluation + } + assertRepeatedlyLazy(tailInitFactory) } @Test diff --git a/compat/src/test/scala/test/scala/collection/LazyListTest.scala b/compat/src/test/scala/test/scala/collection/LazyListTest.scala index b2d8fa4e..655a9f22 100644 --- a/compat/src/test/scala/test/scala/collection/LazyListTest.scala +++ b/compat/src/test/scala/test/scala/collection/LazyListTest.scala @@ -161,7 +161,7 @@ class LazyListTest { cyc.tail.tail.head assertEquals("LazyList(1, 2, 3, )", cyc.toString) cyc.tail.tail.tail.head - assertEquals("LazyList(1, 2, 3, 4, )", cyc.toString) + assertEquals("LazyList(1, 2, 3, 4, )", cyc.toString) cyc.tail.tail.tail.tail.head assertEquals("LazyList(1, 2, 3, 4, )", cyc.toString) }