Skip to content

Commit

Permalink
Merge pull request #9983 from retronym/ticket/t12564-vector-npe
Browse files Browse the repository at this point in the history
Fix NPE regression in Vector.prependedAll
  • Loading branch information
lrytz committed Apr 4, 2022
2 parents 0c58491 + a0d3051 commit 6471120
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/library/scala/collection/immutable/Vector.scala
Expand Up @@ -193,13 +193,15 @@ sealed abstract class Vector[+A] private[immutable] (private[immutable] final va
override def prepended[B >: A](elem: B): Vector[B] = super.prepended(elem)
override def prependedAll[B >: A](prefix: collection.IterableOnce[B]): Vector[B] = {
val k = prefix.knownSize
if(k == 0) this
if (k == 0) this
else if (k < 0) super.prependedAll(prefix)
else prependedAll0(prefix, k)
}

override final def appendedAll[B >: A](suffix: collection.IterableOnce[B]): Vector[B] = {
val k = suffix.knownSize
if(k == 0) this
if (k == 0) this
else if (k < 0) super.appendedAll(suffix)
else appendedAll0(suffix, k)
}

Expand Down
13 changes: 13 additions & 0 deletions test/junit/scala/collection/immutable/VectorTest.scala
Expand Up @@ -13,6 +13,19 @@ import scala.reflect.{ClassTag, classTag}
class VectorTest {
import VectorUtils.validateDebug


@Test
def t12564(): Unit = {
val vector1 = Vector.fill(1000)(0)
// Some prepending, manifesting the bug seems to require 2 separate prepends
val vector2 = List.fill(25)(1) ++: 2 +: vector1
// Now append for fun and profit
val vector3 = vector2 ++ List.fill(40)(3)
// Any iteration will do although might hit NullPointerException in different place
val vector4 = vector3.collect { case n => n }
assert(vector3 == vector4)
}

@Test
def hasCorrectDropAndTakeMethods(): Unit = {
val v = Vector(0) ++ Vector(1 to 64: _*)
Expand Down

0 comments on commit 6471120

Please sign in to comment.