Skip to content

Commit 2d178a7

Browse files
committed
Port stdlib to explicit nulls
1 parent ade36e1 commit 2d178a7

File tree

90 files changed

+1360
-1338
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+1360
-1338
lines changed

library/src/scala/Enumeration.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
107107
private val vmap: mutable.Map[Int, Value] = new mutable.HashMap
108108

109109
/** The cache listing all values of this enumeration. */
110-
@transient private var vset: ValueSet = null
110+
@transient @annotation.stableNull private var vset: ValueSet | Null = null
111111
@transient @volatile private var vsetDefined = false
112112

113113
/** The mapping from the integer used to identify values to their
@@ -121,7 +121,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
121121
vset = (ValueSet.newBuilder ++= vmap.values).result()
122122
vsetDefined = true
123123
}
124-
vset
124+
vset.nn
125125
}
126126

127127
/** The integer to use to identify the next created value. */
@@ -130,7 +130,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
130130
/** The string to use to name the next created value. */
131131
protected var nextName: Iterator[String] = _
132132

133-
private def nextNameOrNull =
133+
private def nextNameOrNull: String | Null =
134134
if (nextName != null && nextName.hasNext) nextName.next() else null
135135

136136
/** The highest integer amongst those used to identify values in this
@@ -177,7 +177,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
177177
* @param name A human-readable name for that value.
178178
* @return Fresh value called `name`.
179179
*/
180-
protected final def Value(name: String): Value = Value(nextId, name)
180+
protected final def Value(name: String | Null): Value = Value(nextId, name)
181181

182182
/** Creates a fresh value, part of this enumeration, called `name`
183183
* and identified by the integer `i`.
@@ -187,7 +187,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
187187
* @param name A human-readable name for that value.
188188
* @return Fresh value with the provided identifier `i` and name `name`.
189189
*/
190-
protected final def Value(i: Int, name: String): Value = new Val(i, name)
190+
protected final def Value(i: Int, name: String | Null): Value = new Val(i, name)
191191

192192
private def populateNameMap(): Unit = {
193193
@tailrec def getFields(clazz: Class[_], acc: Array[JField]): Array[JField] = {
@@ -248,7 +248,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
248248
* identification behaviour.
249249
*/
250250
@SerialVersionUID(0 - 3501153230598116017L)
251-
protected class Val(i: Int, name: String) extends Value with Serializable {
251+
protected class Val(i: Int, name: String | Null) extends Value with Serializable {
252252
def this(i: Int) = this(i, nextNameOrNull)
253253
def this(name: String) = this(nextId, name)
254254
def this() = this(nextId)

library/src/scala/Predef.scala

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ object Predef extends LowPriorityImplicits {
122122
* @return The runtime [[Class]] representation of type `T`.
123123
* @group utilities
124124
*/
125-
def classOf[T]: Class[T] = null // This is a stub method. The actual implementation is filled in by the compiler.
125+
def classOf[T]: Class[T] = null.asInstanceOf[Class[T]] // This is a stub method. The actual implementation is filled in by the compiler.
126126

127127
/**
128128
* Retrieve the single value of a type with a unique inhabitant.
@@ -286,12 +286,12 @@ object Predef extends LowPriorityImplicits {
286286
to be available at runtime.
287287
To achieve this, we keep the Scala 3 signature publicly available.
288288
We rely on the fact that it is `inline` and will not be visible in the bytecode.
289-
To add the required Scala 2 ones, we define the `scala2Assert`, we use:
290-
- `@targetName` to swap the name in the generated code to `assert`
289+
To add the required Scala 2 ones, we define the `scala2Assert`, we use:
290+
- `@targetName` to swap the name in the generated code to `assert`
291291
- `@publicInBinary` to make it available during runtime.
292292
As such, we would successfully hijack the definitions of `assert` such as:
293293
- At compile time, we would have the definitions of `assert`
294-
- At runtime, the definitions of `scala2Assert` as `assert`
294+
- At runtime, the definitions of `scala2Assert` as `assert`
295295
NOTE: Tasty-Reader in Scala 2 will have to learn about this swapping if we are to
296296
allow loading the full Scala 3 library by it.
297297
*/
@@ -426,7 +426,7 @@ object Predef extends LowPriorityImplicits {
426426
@inline def formatted(fmtstr: String): String = fmtstr format self
427427
}
428428

429-
/** Injects String concatenation operator `+` to any classes.
429+
/** Injects String concatenation operator `+` to any classes.
430430
* @group implicit-classes-any
431431
*/
432432
@(deprecated @companionMethod)("Implicit injection of + is deprecated. Convert to String to call +", "2.13.0")
@@ -658,46 +658,46 @@ private[scala] abstract class LowPriorityImplicits extends LowPriorityImplicits2
658658
@inline implicit def booleanWrapper(x: Boolean): runtime.RichBoolean = new runtime.RichBoolean(x)
659659

660660
/** @group conversions-array-to-wrapped-array */
661-
implicit def genericWrapArray[T](xs: Array[T]): ArraySeq[T] =
661+
implicit def genericWrapArray[T](xs: Array[T] | Null): ArraySeq[T] | Null =
662662
if (xs eq null) null
663663
else ArraySeq.make(xs)
664664

665665
// Since the JVM thinks arrays are covariant, one 0-length Array[AnyRef]
666666
// is as good as another for all T <: AnyRef. Instead of creating 100,000,000
667667
// unique ones by way of this implicit, let's share one.
668668
/** @group conversions-array-to-wrapped-array */
669-
implicit def wrapRefArray[T <: AnyRef](xs: Array[T]): ArraySeq.ofRef[T] = {
669+
implicit def wrapRefArray[T <: AnyRef](xs: Array[T] | Null): ArraySeq.ofRef[T] | Null = {
670670
if (xs eq null) null
671671
else if (xs.length == 0) ArraySeq.empty[AnyRef].asInstanceOf[ArraySeq.ofRef[T]]
672672
else new ArraySeq.ofRef[T](xs)
673673
}
674674

675675
/** @group conversions-array-to-wrapped-array */
676-
implicit def wrapIntArray(xs: Array[Int]): ArraySeq.ofInt = if (xs ne null) new ArraySeq.ofInt(xs) else null
676+
implicit def wrapIntArray(xs: Array[Int] | Null): ArraySeq.ofInt | Null = if (xs ne null) new ArraySeq.ofInt(xs) else null
677677
/** @group conversions-array-to-wrapped-array */
678-
implicit def wrapDoubleArray(xs: Array[Double]): ArraySeq.ofDouble = if (xs ne null) new ArraySeq.ofDouble(xs) else null
678+
implicit def wrapDoubleArray(xs: Array[Double] | Null): ArraySeq.ofDouble | Null = if (xs ne null) new ArraySeq.ofDouble(xs) else null
679679
/** @group conversions-array-to-wrapped-array */
680-
implicit def wrapLongArray(xs: Array[Long]): ArraySeq.ofLong = if (xs ne null) new ArraySeq.ofLong(xs) else null
680+
implicit def wrapLongArray(xs: Array[Long] | Null): ArraySeq.ofLong | Null = if (xs ne null) new ArraySeq.ofLong(xs) else null
681681
/** @group conversions-array-to-wrapped-array */
682-
implicit def wrapFloatArray(xs: Array[Float]): ArraySeq.ofFloat = if (xs ne null) new ArraySeq.ofFloat(xs) else null
682+
implicit def wrapFloatArray(xs: Array[Float] | Null): ArraySeq.ofFloat | Null = if (xs ne null) new ArraySeq.ofFloat(xs) else null
683683
/** @group conversions-array-to-wrapped-array */
684-
implicit def wrapCharArray(xs: Array[Char]): ArraySeq.ofChar = if (xs ne null) new ArraySeq.ofChar(xs) else null
684+
implicit def wrapCharArray(xs: Array[Char] | Null): ArraySeq.ofChar | Null = if (xs ne null) new ArraySeq.ofChar(xs) else null
685685
/** @group conversions-array-to-wrapped-array */
686-
implicit def wrapByteArray(xs: Array[Byte]): ArraySeq.ofByte = if (xs ne null) new ArraySeq.ofByte(xs) else null
686+
implicit def wrapByteArray(xs: Array[Byte] | Null): ArraySeq.ofByte | Null = if (xs ne null) new ArraySeq.ofByte(xs) else null
687687
/** @group conversions-array-to-wrapped-array */
688-
implicit def wrapShortArray(xs: Array[Short]): ArraySeq.ofShort = if (xs ne null) new ArraySeq.ofShort(xs) else null
688+
implicit def wrapShortArray(xs: Array[Short] | Null): ArraySeq.ofShort | Null = if (xs ne null) new ArraySeq.ofShort(xs) else null
689689
/** @group conversions-array-to-wrapped-array */
690-
implicit def wrapBooleanArray(xs: Array[Boolean]): ArraySeq.ofBoolean = if (xs ne null) new ArraySeq.ofBoolean(xs) else null
690+
implicit def wrapBooleanArray(xs: Array[Boolean] | Null): ArraySeq.ofBoolean | Null = if (xs ne null) new ArraySeq.ofBoolean(xs) else null
691691
/** @group conversions-array-to-wrapped-array */
692-
implicit def wrapUnitArray(xs: Array[Unit]): ArraySeq.ofUnit = if (xs ne null) new ArraySeq.ofUnit(xs) else null
692+
implicit def wrapUnitArray(xs: Array[Unit] | Null): ArraySeq.ofUnit | Null = if (xs ne null) new ArraySeq.ofUnit(xs) else null
693693

694694
/** @group conversions-string */
695-
implicit def wrapString(s: String): WrappedString = if (s ne null) new WrappedString(s) else null
695+
implicit def wrapString(s: String | Null): WrappedString | Null = if (s ne null) new WrappedString(s) else null
696696
}
697697

698698
private[scala] abstract class LowPriorityImplicits2 {
699699
@deprecated("implicit conversions from Array to immutable.IndexedSeq are implemented by copying; use `toIndexedSeq` explicitly if you want to copy, or use the more efficient non-copying ArraySeq.unsafeWrapArray", since="2.13.0")
700-
implicit def copyArrayToImmutableIndexedSeq[T](xs: Array[T]): IndexedSeq[T] =
700+
implicit def copyArrayToImmutableIndexedSeq[T](xs: Array[T] | Null): IndexedSeq[T] | Null =
701701
if (xs eq null) null
702702
else new ArrayOps(xs).toIndexedSeq
703703
}

library/src/scala/Specializable.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ object Specializable {
2424
trait SpecializedGroup
2525

2626
// Smuggle a list of types by way of a tuple upon which Group is parameterized.
27-
class Group[T >: Null](value: T) extends SpecializedGroup
27+
class Group[T](value: T) extends SpecializedGroup
2828

29-
final val Primitives: Group[(Byte, Short, Int, Long, Char, Float, Double, Boolean, Unit)] = null
30-
final val Everything: Group[(Byte, Short, Int, Long, Char, Float, Double, Boolean, Unit, AnyRef)] = null
31-
final val Bits32AndUp: Group[(Int, Long, Float, Double)] = null
32-
final val Integral: Group[(Byte, Short, Int, Long, Char)] = null
33-
final val AllNumeric: Group[(Byte, Short, Int, Long, Char, Float, Double)] = null
34-
final val BestOfBreed: Group[(Int, Double, Boolean, Unit, AnyRef)] = null
35-
final val Unit: Group[Tuple1[Unit]] = null
29+
final val Primitives: Group[(Byte, Short, Int, Long, Char, Float, Double, Boolean, Unit)] = null.asInstanceOf
30+
final val Everything: Group[(Byte, Short, Int, Long, Char, Float, Double, Boolean, Unit, AnyRef)] = null.asInstanceOf
31+
final val Bits32AndUp: Group[(Int, Long, Float, Double)] = null.asInstanceOf
32+
final val Integral: Group[(Byte, Short, Int, Long, Char)] = null.asInstanceOf
33+
final val AllNumeric: Group[(Byte, Short, Int, Long, Char, Float, Double)] = null.asInstanceOf
34+
final val BestOfBreed: Group[(Int, Double, Boolean, Unit, AnyRef)] = null.asInstanceOf
35+
final val Unit: Group[Tuple1[Unit]] = null.asInstanceOf
3636

37-
final val Arg: Group[(Int, Long, Float, Double)] = null
38-
final val Args: Group[(Int, Long, Double)] = null
39-
final val Return: Group[(Int, Long, Float, Double, Boolean, Unit)] = null
37+
final val Arg: Group[(Int, Long, Float, Double)] = null.asInstanceOf
38+
final val Args: Group[(Int, Long, Double)] = null.asInstanceOf
39+
final val Return: Group[(Int, Long, Float, Double, Boolean, Unit)] = null.asInstanceOf
4040
}

0 commit comments

Comments
 (0)