Skip to content

Commit d62ceb8

Browse files
committed
Merge pull request scala#3530 from Ichoran/issue/6632
SI-6632 ListBuffer's updated accepts negative positions
2 parents d227a89 + d3a302b commit d62ceb8

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

src/library/scala/collection/SeqLike.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,11 +509,14 @@ trait SeqLike[+A, +Repr] extends Any with IterableLike[A, Repr] with GenSeqLike[
509509
}
510510

511511
def updated[B >: A, That](index: Int, elem: B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
512+
if (index < 0) throw new IndexOutOfBoundsException(index.toString)
512513
val b = bf(repr)
513514
val (prefix, rest) = this.splitAt(index)
515+
val restColl = toCollection(rest)
516+
if (restColl.isEmpty) throw new IndexOutOfBoundsException(index.toString)
514517
b ++= toCollection(prefix)
515518
b += elem
516-
b ++= toCollection(rest).view.tail
519+
b ++= restColl.view.tail
517520
b.result()
518521
}
519522

test/files/run/t6632.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
java.lang.IndexOutOfBoundsException: -1
22
java.lang.IndexOutOfBoundsException: -2
33
java.lang.IndexOutOfBoundsException: -3
4+
java.lang.IndexOutOfBoundsException: -1
5+
java.lang.IndexOutOfBoundsException: 5

test/files/run/t6632.scala

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,20 @@ object Test extends App {
33

44
def newLB = ListBuffer('a, 'b, 'c, 'd, 'e)
55

6-
val lb0 = newLB
6+
def iiobe[A](f: => A) =
7+
try { f }
8+
catch { case ex: IndexOutOfBoundsException => println(ex) }
79

8-
try {
9-
lb0.insert(-1, 'x)
10-
} catch {
11-
case ex: IndexOutOfBoundsException => println(ex)
12-
}
10+
val lb0 = newLB
11+
iiobe( lb0.insert(-1, 'x) )
1312

1413
val lb1 = newLB
15-
16-
try {
17-
lb1.insertAll(-2, Array('x, 'y, 'z))
18-
} catch {
19-
case ex: IndexOutOfBoundsException => println(ex)
20-
}
14+
iiobe( lb1.insertAll(-2, Array('x, 'y, 'z)) )
2115

2216
val lb2 = newLB
17+
iiobe( lb2.update(-3, 'u) )
2318

24-
try {
25-
lb2.update(-3, 'u)
26-
} catch {
27-
case ex: IndexOutOfBoundsException => println(ex)
28-
}
29-
}
19+
val lb3 = newLB
20+
iiobe( lb3.updated(-1, 'u) )
21+
iiobe( lb3.updated(5, 'u) )
22+
}

0 commit comments

Comments
 (0)