Skip to content

Commit

Permalink
Fix NPE regression in Vector.prependedAll
Browse files Browse the repository at this point in the history
Also make a corresponding fix to appenededAll. This isn't exercised
by the test case though.
  • Loading branch information
retronym committed Mar 30, 2022
1 parent c8ee991 commit 67fd672
Show file tree
Hide file tree
Showing 2 changed files with 18 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
14 changes: 14 additions & 0 deletions test/files/run/t12564.scala
@@ -0,0 +1,14 @@
object Test extends App {

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)
}

0 comments on commit 67fd672

Please sign in to comment.