Skip to content

Commit a958794

Browse files
committed
Fix NPEs revealed by tests
1 parent 747eb65 commit a958794

File tree

6 files changed

+32
-34
lines changed

6 files changed

+32
-34
lines changed

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ class CompilationTests {
205205
).checkCompile()
206206

207207
// Explicit nulls tests
208-
@Ignore
209208
@Test def explicitNullsNeg: Unit = {
210209
implicit val testGroup: TestGroup = TestGroup("explicitNullsNeg")
211210
aggregateTests(
@@ -215,7 +214,6 @@ class CompilationTests {
215214
)
216215
}.checkExpectedErrors()
217216

218-
@Ignore
219217
@Test def explicitNullsPos: Unit = {
220218
implicit val testGroup: TestGroup = TestGroup("explicitNullsPos")
221219
aggregateTests(
@@ -235,13 +233,11 @@ class CompilationTests {
235233
}
236234
}
237235

238-
@Ignore
239236
@Test def explicitNullsWarn: Unit = {
240237
implicit val testGroup: TestGroup = TestGroup("explicitNullsWarn")
241238
compileFilesInDir("tests/explicit-nulls/warn", explicitNullsOptions)
242239
}.checkWarnings()
243240

244-
@Ignore
245241
@Test def explicitNullsRun: Unit = {
246242
implicit val testGroup: TestGroup = TestGroup("explicitNullsRun")
247243
compileFilesInDir("tests/explicit-nulls/run", explicitNullsOptions)

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ private[collection] object RedBlackTree {
311311
if (tree.right ne null) _foreachEntry(tree.right.nn, f)
312312
}
313313

314-
def iterator[A: Ordering, B](tree: Tree[A, B], start: Option[A] = None): Iterator[(A, B)] = new EntriesIterator(tree, start)
315-
def keysIterator[A: Ordering](tree: Tree[A, _], start: Option[A] = None): Iterator[A] = new KeysIterator(tree, start)
316-
def valuesIterator[A: Ordering, B](tree: Tree[A, B], start: Option[A] = None): Iterator[B] = new ValuesIterator(tree, start)
314+
def iterator[A: Ordering, B](tree: Tree[A, B] | Null, start: Option[A] = None): Iterator[(A, B)] = new EntriesIterator(tree, start)
315+
def keysIterator[A: Ordering](tree: Tree[A, _] | Null, start: Option[A] = None): Iterator[A] = new KeysIterator(tree, start)
316+
def valuesIterator[A: Ordering, B](tree: Tree[A, B] | Null, start: Option[A] = None): Iterator[B] = new ValuesIterator(tree, start)
317317

318318
@tailrec
319319
def nth[A, B](tree: Tree[A, B], n: Int): Tree[A, B] = {
@@ -927,16 +927,16 @@ private[collection] object RedBlackTree {
927927
equal && (this.lookahead eq null) && (that.lookahead eq null)
928928
}
929929
}
930-
private[this] class EntriesIterator[A: Ordering, B](tree: Tree[A, B], focus: Option[A]) extends TreeIterator[A, B, (A, B)](tree, focus) {
931-
override def nextResult(tree: Tree[A, B]) = (tree.key, tree.value)
930+
private[this] class EntriesIterator[A: Ordering, B](tree: Tree[A, B] | Null, focus: Option[A]) extends TreeIterator[A, B, (A, B)](tree, focus) {
931+
override def nextResult(tree: Tree[A, B]) = (tree.nn.key, tree.nn.value)
932932
}
933933

934-
private[this] class KeysIterator[A: Ordering, B](tree: Tree[A, B], focus: Option[A]) extends TreeIterator[A, B, A](tree, focus) {
935-
override def nextResult(tree: Tree[A, B]) = tree.key
934+
private[this] class KeysIterator[A: Ordering, B](tree: Tree[A, B] | Null, focus: Option[A]) extends TreeIterator[A, B, A](tree, focus) {
935+
override def nextResult(tree: Tree[A, B]) = tree.nn.key
936936
}
937937

938-
private[this] class ValuesIterator[A: Ordering, B](tree: Tree[A, B], focus: Option[A]) extends TreeIterator[A, B, B](tree, focus) {
939-
override def nextResult(tree: Tree[A, B]) = tree.value
938+
private[this] class ValuesIterator[A: Ordering, B](tree: Tree[A, B] | Null, focus: Option[A]) extends TreeIterator[A, B, B](tree, focus) {
939+
override def nextResult(tree: Tree[A, B]) = tree.nn.value
940940
}
941941

942942
/** Build a Tree suitable for a TreeSet from an ordered sequence of keys */
@@ -1139,33 +1139,35 @@ private[collection] object RedBlackTree {
11391139
else 2*bh-1
11401140
}
11411141

1142-
private[this] def joinRight[A, B](tl: Tree[A, B], k: A, v: B, tr: Tree[A, B], bhtl: Int, rtr: Int): Tree[A, B] = {
1142+
private[this] def joinRight[A, B](tl: Tree[A, B] | Null, k: A, v: B, tr: Tree[A, B] | Null, bhtl: Int, rtr: Int): Tree[A, B] = {
11431143
val rtl = rank(tl, bhtl)
11441144
if(rtl == (rtr/2)*2) RedTree(k, v, tl, tr)
11451145
else {
1146+
val tlnn = tl.nn
11461147
val tlBlack = isBlackTree(tl)
11471148
val bhtlr = if(tlBlack) bhtl-1 else bhtl
1148-
val ttr = joinRight(tl.right.nn, k, v, tr, bhtlr, rtr)
1149+
val ttr = joinRight(tlnn.right, k, v, tr, bhtlr, rtr)
11491150
if(tlBlack && isRedTree(ttr) && isRedTree(ttr.right))
11501151
RedTree(ttr.key, ttr.value,
1151-
BlackTree(tl.key, tl.value, tl.left, ttr.left),
1152+
BlackTree(tlnn.key, tlnn.value, tlnn.left, ttr.left),
11521153
ttr.right.nn.black)
1153-
else mkTree(tlBlack, tl.key, tl.value, tl.left, ttr)
1154+
else mkTree(tlBlack, tlnn.key, tlnn.value, tlnn.left, ttr)
11541155
}
11551156
}
11561157

1157-
private[this] def joinLeft[A, B](tl: Tree[A, B], k: A, v: B, tr: Tree[A, B], rtl: Int, bhtr: Int): Tree[A, B] = {
1158+
private[this] def joinLeft[A, B](tl: Tree[A, B] | Null, k: A, v: B, tr: Tree[A, B] | Null, rtl: Int, bhtr: Int): Tree[A, B] = {
11581159
val rtr = rank(tr, bhtr)
11591160
if(rtr == (rtl/2)*2) RedTree(k, v, tl, tr)
11601161
else {
1162+
val trnn = tr.nn
11611163
val trBlack = isBlackTree(tr)
11621164
val bhtrl = if(trBlack) bhtr-1 else bhtr
1163-
val ttl = joinLeft(tl, k, v, tr.left.nn, rtl, bhtrl)
1165+
val ttl = joinLeft(tl, k, v, trnn.left, rtl, bhtrl)
11641166
if(trBlack && isRedTree(ttl) && isRedTree(ttl.left))
11651167
RedTree(ttl.key, ttl.value,
11661168
ttl.left.nn.black,
1167-
BlackTree(tr.key, tr.value, ttl.right, tr.right))
1168-
else mkTree(trBlack, tr.key, tr.value, ttl, tr.right)
1169+
BlackTree(trnn.key, trnn.value, ttl.right, trnn.right))
1170+
else mkTree(trBlack, trnn.key, trnn.value, ttl, trnn.right)
11691171
}
11701172
}
11711173

@@ -1175,7 +1177,7 @@ private[collection] object RedBlackTree {
11751177
val bhtl = h(tl, 0)
11761178
val bhtr = h(tr, 0)
11771179
if(bhtl > bhtr) {
1178-
val tt = joinRight(tl.nn, k, v, tr.nn, bhtl, rank(tr, bhtr))
1180+
val tt = joinRight(tl.nn, k, v, tr.nn, bhtl, rank(tr, bhtr)) // todo: nullable
11791181
if(isRedTree(tt) && isRedTree(tt.right)) tt.black
11801182
else tt
11811183
} else if(bhtr > bhtl) {

library/src/scala/collection/immutable/TreeMap.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ final class TreeMap[K, +V] private (private val tree: RB.Tree[K, V] | Null)(impl
8787

8888
override def sortedMapFactory: SortedMapFactory[TreeMap] = TreeMap
8989

90-
def iterator: Iterator[(K, V)] = RB.iterator(tree.nn)
90+
def iterator: Iterator[(K, V)] = RB.iterator(tree)
9191

92-
def keysIteratorFrom(start: K): Iterator[K] = RB.keysIterator(tree.nn, Some(start))
92+
def keysIteratorFrom(start: K): Iterator[K] = RB.keysIterator(tree, Some(start))
9393

9494
override def keySet: TreeSet[K] = new TreeSet(tree)(ordering)
9595

96-
def iteratorFrom(start: K): Iterator[(K, V)] = RB.iterator(tree.nn, Some(start))
96+
def iteratorFrom(start: K): Iterator[(K, V)] = RB.iterator(tree, Some(start))
9797

98-
override def valuesIteratorFrom(start: K): Iterator[V] = RB.valuesIterator(tree.nn, Some(start))
98+
override def valuesIteratorFrom(start: K): Iterator[V] = RB.valuesIterator(tree, Some(start))
9999

100100
override def stepper[S <: Stepper[_]](implicit shape: StepperShape[(K, V), S]): S with EfficientSplit =
101101
shape.parUnbox(

library/src/scala/collection/immutable/TreeSet.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ final class TreeSet[A] private[immutable] (private[immutable] val tree: RB.Tree[
130130
if (v eq null) Option.empty else Some(v.key)
131131
}
132132

133-
def iterator: Iterator[A] = RB.keysIterator(tree.nn)
133+
def iterator: Iterator[A] = RB.keysIterator(tree)
134134

135-
def iteratorFrom(start: A): Iterator[A] = RB.keysIterator(tree.nn, Some(start))
135+
def iteratorFrom(start: A): Iterator[A] = RB.keysIterator(tree, Some(start))
136136

137137
override def stepper[S <: Stepper[_]](implicit shape: StepperShape[A, S]): S with EfficientSplit = {
138138
import scala.collection.convert.impl._

library/src/scala/collection/mutable/UnrolledBuffer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ object UnrolledBuffer extends StrictOptimizedClassTagSeqFactory[UnrolledBuffer]
264264

265265
/** Unrolled buffer node.
266266
*/
267-
class Unrolled[T: ClassTag] private[collection] (var size: Int, var array: Array[T], var next: Unrolled[T] | Null, val buff: UnrolledBuffer[T] | Null) {
267+
class Unrolled[T: ClassTag] private[collection] (var size: Int, var array: Array[T], var next: Unrolled[T] | Null, val buff: UnrolledBuffer[T] | Null = null) {
268268
this: Unrolled[T]^{} =>
269269
private[collection] def this() = this(0, new Array[T](unrolledlength), null, null)
270270
private[collection] def this(b: UnrolledBuffer[T] | Null) = this(0, new Array[T](unrolledlength), null, b)

library/src/scala/util/matching/Regex.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class Regex private[matching](val pattern: Pattern, groupNames: String*) extends
284284
* @param s The string to match
285285
* @return The matches
286286
*/
287-
def unapplySeq(s: CharSequence): Option[List[String]] = {
287+
def unapplySeq(s: CharSequence): Option[List[String | Null]] = {
288288
val m = pattern.matcher(s)
289289
if (runMatcher(m)) Some(List.tabulate(m.groupCount) { i => m.group(i + 1) })
290290
else None
@@ -338,7 +338,7 @@ class Regex private[matching](val pattern: Pattern, groupNames: String*) extends
338338
* Otherwise, this Regex is applied to the previously matched input,
339339
* and the result of that match is used.
340340
*/
341-
def unapplySeq(m: Match): Option[List[String]] =
341+
def unapplySeq(m: Match): Option[List[String | Null]] =
342342
if (m.matched == null) None
343343
else if (m.matcher.pattern == this.pattern) Regex.extractGroupsFromMatch(m)
344344
else unapplySeq(m.matched.nn)
@@ -783,13 +783,13 @@ object Regex {
783783
* }}}
784784
*/
785785
object Groups {
786-
def unapplySeq(m: Match): Option[Seq[String]] = {
786+
def unapplySeq(m: Match): Option[Seq[String | Null]] = {
787787
if (m.groupCount > 0) extractGroupsFromMatch(m) else None
788788
}
789789
}
790790

791-
@inline private def extractGroupsFromMatch(m: Match): Option[List[String]] =
792-
Some(List.tabulate(m.groupCount) { i => m.group(i + 1).nn })
791+
@inline private def extractGroupsFromMatch(m: Match): Option[List[String | Null]] =
792+
Some(List.tabulate(m.groupCount) { i => m.group(i + 1) })
793793

794794
/** A class to step through a sequence of regex matches.
795795
*

0 commit comments

Comments
 (0)