Skip to content

Commit

Permalink
Merge pull request #10416 from lrytz/t12796
Browse files Browse the repository at this point in the history
Fix ListBuffer.insertAfter, used in patchInPlace
  • Loading branch information
som-snytt committed Jun 23, 2023
2 parents 30ac2f0 + 53bc01b commit eff3022
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/library/scala/collection/mutable/ListBuffer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ListBuffer[A]
@transient private[this] var mutationCount: Int = 0

private var first: List[A] = Nil
private var last0: ::[A] = null
private var last0: ::[A] = null // last element (`last0` just because the name `last` is already taken)
private[this] var aliased = false
private[this] var len = 0

Expand Down Expand Up @@ -244,6 +244,7 @@ class ListBuffer[A]
val follow = getNext(prev)
if (prev eq null) first = fresh.first else prev.next = fresh.first
fresh.last0.next = follow
if (follow.isEmpty) last0 = fresh.last0
len += fresh.length
}
}
Expand Down
15 changes: 15 additions & 0 deletions test/junit/scala/collection/mutable/ListBufferTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,19 @@ class ListBufferTest {
test(ArrayBuffer[String]())
test(ListBuffer[String]())
}

@Test
def t12796(): Unit = {
val b = ListBuffer(1).patchInPlace(1, List(2), 1)
assertEquals(2, b.last)
assertEquals(List(1, 2), b.toList)
}

@Test
def t12796b(): Unit = {
val b = ListBuffer(1, 2)
b.insertAll(2, List(3))
assertEquals(3, b.last)
assertEquals(List(1, 2, 3), b.toList)
}
}

0 comments on commit eff3022

Please sign in to comment.