Skip to content

Commit b83b3d9

Browse files
authored
Do not assume left is non-null if another branch is null in splitLast (#24325)
Fix NPE in #24177
2 parents eca6849 + 82c0948 commit b83b3d9

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

library/src/scala/collection/immutable/RedBlackTree.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,8 +1201,8 @@ private[collection] object RedBlackTree {
12011201
}
12021202
}
12031203

1204-
private def splitLast[A, B](t: Tree[A, B]): (Tree[A, B], A, B) =
1205-
if(t.right eq null) (t.left.nn, t.key, t.value)
1204+
private def splitLast[A, B](t: Tree[A, B]): (Tree[A, B] | Null, A, B) =
1205+
if (t.right eq null) (t.left, t.key, t.value)
12061206
else {
12071207
val (tt, kk, vv) = splitLast(t.right.nn)
12081208
(join(t.left, t.key, t.value, tt), kk, vv)

tests/run/sorted-sets-ok.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import scala.collection.SortedSet
2+
import scala.collection.immutable.{SortedSet => ISortedSet}
3+
import scala.collection.mutable.{SortedSet => MSortedSet}
4+
5+
@main def Test(): Unit =
6+
val s1: SortedSet[Int] = SortedSet(1, 2, 3)
7+
println(s1)
8+
val s2 = s1.filter(_ % 2 == 1)
9+
println(s2)
10+
11+
val s3: ISortedSet[Int] = ISortedSet(1, 2, 3)
12+
println(s3)
13+
val s4 = s3.filter(_ % 2 == 1)
14+
println(s4)
15+
16+
val s5: MSortedSet[Int] = MSortedSet(4, 5, 6)
17+
println(s5)
18+
val s6 = s5.filter(_ % 2 == 0)
19+
println(s6)

0 commit comments

Comments
 (0)