Skip to content

Commit eff3022

Browse files
authored
Merge pull request scala#10416 from lrytz/t12796
Fix ListBuffer.insertAfter, used in patchInPlace
2 parents 30ac2f0 + 53bc01b commit eff3022

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/library/scala/collection/mutable/ListBuffer.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ListBuffer[A]
4646
@transient private[this] var mutationCount: Int = 0
4747

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

@@ -244,6 +244,7 @@ class ListBuffer[A]
244244
val follow = getNext(prev)
245245
if (prev eq null) first = fresh.first else prev.next = fresh.first
246246
fresh.last0.next = follow
247+
if (follow.isEmpty) last0 = fresh.last0
247248
len += fresh.length
248249
}
249250
}

test/junit/scala/collection/mutable/ListBufferTest.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,19 @@ class ListBufferTest {
323323
test(ArrayBuffer[String]())
324324
test(ListBuffer[String]())
325325
}
326+
327+
@Test
328+
def t12796(): Unit = {
329+
val b = ListBuffer(1).patchInPlace(1, List(2), 1)
330+
assertEquals(2, b.last)
331+
assertEquals(List(1, 2), b.toList)
332+
}
333+
334+
@Test
335+
def t12796b(): Unit = {
336+
val b = ListBuffer(1, 2)
337+
b.insertAll(2, List(3))
338+
assertEquals(3, b.last)
339+
assertEquals(List(1, 2, 3), b.toList)
340+
}
326341
}

0 commit comments

Comments
 (0)