Skip to content

Commit d54e2b7

Browse files
committed
[bug#12121] Add test for inserting an ArrayBuffer into itself
1 parent 8cc0e17 commit d54e2b7

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

library/src/scala/collection/mutable/ArrayBuffer.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ class ArrayBuffer[A] private (initialElements: Array[AnyRef], initialSize: Int)
167167
size0 = size0 + elemsLength
168168
elems match {
169169
case elems: ArrayBuffer[_] =>
170+
// if `elems eq this`, this works because `elems.array eq this.array`,
171+
// we didn't overwrite the values being inserted after moving them in
172+
// the previous copy a few lines up, and `System.arraycopy` will
173+
// effectively "read" all the values before overwriting any of them.
170174
Array.copy(elems.array, 0, array, index, elemsLength)
171175
case _ =>
172176
var i = 0

0 commit comments

Comments
 (0)