Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix null-pointer regression in Vector#prependedAll and Vector#appendedAll #9983

Merged
merged 1 commit into from Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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