-
Notifications
You must be signed in to change notification settings - Fork 1.1k
enable -Yexplicit-nulls for scala3-library #13729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -328,54 +328,59 @@ object IArray: | |
extension [T, U >: T: ClassTag](x: T) | ||
def +:(arr: IArray[U]): IArray[U] = genericArrayOps(arr).prepended(x) | ||
|
||
// For backwards compatibility with code compiled without -Yexplicit-nulls | ||
private inline def mapNull[A, B](a: A, inline f: B): B = | ||
if((a: A|Null) == null) null.asInstanceOf[B] else f | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That shouldn't be a problem: an expression of type |
||
|
||
/** Conversion from IArray to immutable.ArraySeq */ | ||
implicit def genericWrapArray[T](arr: IArray[T]): ArraySeq[T] = | ||
if arr eq null then null else ArraySeq.unsafeWrapArray(arr) | ||
mapNull(arr, ArraySeq.unsafeWrapArray(arr)) | ||
|
||
/** Conversion from IArray to immutable.ArraySeq */ | ||
implicit def wrapRefArray[T <: AnyRef](arr: IArray[T]): ArraySeq.ofRef[T] = | ||
// Since the JVM thinks arrays are covariant, one 0-length Array[AnyRef] | ||
// is as good as another for all T <: AnyRef. Instead of creating 100,000,000 | ||
// unique ones by way of this implicit, let's share one. | ||
if (arr eq null) null | ||
else if (arr.length == 0) ArraySeq.empty[AnyRef].asInstanceOf[ArraySeq.ofRef[T]] | ||
else ArraySeq.ofRef(arr.asInstanceOf[Array[T]]) | ||
mapNull(arr, | ||
if (arr.length == 0) ArraySeq.empty[AnyRef].asInstanceOf[ArraySeq.ofRef[T]] | ||
else ArraySeq.ofRef(arr.asInstanceOf[Array[T]]) | ||
) | ||
|
||
/** Conversion from IArray to immutable.ArraySeq */ | ||
implicit def wrapIntArray(arr: IArray[Int]): ArraySeq.ofInt = | ||
if (arr ne null) new ArraySeq.ofInt(arr.asInstanceOf[Array[Int]]) else null | ||
mapNull(arr, new ArraySeq.ofInt(arr.asInstanceOf[Array[Int]])) | ||
|
||
/** Conversion from IArray to immutable.ArraySeq */ | ||
implicit def wrapDoubleIArray(arr: IArray[Double]): ArraySeq.ofDouble = | ||
if (arr ne null) new ArraySeq.ofDouble(arr.asInstanceOf[Array[Double]]) else null | ||
mapNull(arr, new ArraySeq.ofDouble(arr.asInstanceOf[Array[Double]])) | ||
|
||
/** Conversion from IArray to immutable.ArraySeq */ | ||
implicit def wrapLongIArray(arr: IArray[Long]): ArraySeq.ofLong = | ||
if (arr ne null) new ArraySeq.ofLong(arr.asInstanceOf[Array[Long]]) else null | ||
mapNull(arr, new ArraySeq.ofLong(arr.asInstanceOf[Array[Long]])) | ||
|
||
/** Conversion from IArray to immutable.ArraySeq */ | ||
implicit def wrapFloatIArray(arr: IArray[Float]): ArraySeq.ofFloat = | ||
if (arr ne null) new ArraySeq.ofFloat(arr.asInstanceOf[Array[Float]]) else null | ||
mapNull(arr, new ArraySeq.ofFloat(arr.asInstanceOf[Array[Float]])) | ||
|
||
/** Conversion from IArray to immutable.ArraySeq */ | ||
implicit def wrapCharIArray(arr: IArray[Char]): ArraySeq.ofChar = | ||
if (arr ne null) new ArraySeq.ofChar(arr.asInstanceOf[Array[Char]]) else null | ||
mapNull(arr, new ArraySeq.ofChar(arr.asInstanceOf[Array[Char]])) | ||
|
||
/** Conversion from IArray to immutable.ArraySeq */ | ||
implicit def wrapByteIArray(arr: IArray[Byte]): ArraySeq.ofByte = | ||
if (arr ne null) new ArraySeq.ofByte(arr.asInstanceOf[Array[Byte]]) else null | ||
mapNull(arr, new ArraySeq.ofByte(arr.asInstanceOf[Array[Byte]])) | ||
|
||
/** Conversion from IArray to immutable.ArraySeq */ | ||
implicit def wrapShortIArray(arr: IArray[Short]): ArraySeq.ofShort = | ||
if (arr ne null) new ArraySeq.ofShort(arr.asInstanceOf[Array[Short]]) else null | ||
mapNull(arr, new ArraySeq.ofShort(arr.asInstanceOf[Array[Short]])) | ||
|
||
/** Conversion from IArray to immutable.ArraySeq */ | ||
implicit def wrapBooleanIArray(arr: IArray[Boolean]): ArraySeq.ofBoolean = | ||
if (arr ne null) new ArraySeq.ofBoolean(arr.asInstanceOf[Array[Boolean]]) else null | ||
mapNull(arr, new ArraySeq.ofBoolean(arr.asInstanceOf[Array[Boolean]])) | ||
|
||
/** Conversion from IArray to immutable.ArraySeq */ | ||
implicit def wrapUnitIArray(arr: IArray[Unit]): ArraySeq.ofUnit = | ||
if (arr ne null) new ArraySeq.ofUnit(arr.asInstanceOf[Array[Unit]]) else null | ||
mapNull(arr, new ArraySeq.ofUnit(arr.asInstanceOf[Array[Unit]])) | ||
|
||
/** Convert an array into an immutable array without copying, the original array | ||
* must _not_ be mutated after this or the guaranteed immutablity of IArray will | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,21 +5,21 @@ package scala.runtime | |
*/ | ||
object LazyVals { | ||
private[this] val unsafe: sun.misc.Unsafe = | ||
classOf[sun.misc.Unsafe].getDeclaredFields.find { field => | ||
field.getType == classOf[sun.misc.Unsafe] && { | ||
field.setAccessible(true) | ||
classOf[sun.misc.Unsafe].getDeclaredFields.nn.find { field => | ||
field.nn.getType == classOf[sun.misc.Unsafe] && { | ||
field.nn.setAccessible(true) | ||
true | ||
} | ||
} | ||
.map(_.get(null).asInstanceOf[sun.misc.Unsafe]) | ||
.map(_.nn.get(null).asInstanceOf[sun.misc.Unsafe]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The type of The type of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, thanks for the explanation! |
||
.getOrElse { | ||
throw new ExceptionInInitializerError { | ||
new IllegalStateException("Can't find instance of sun.misc.Unsafe") | ||
} | ||
} | ||
|
||
private[this] val base: Int = { | ||
val processors = java.lang.Runtime.getRuntime.availableProcessors() | ||
val processors = java.lang.Runtime.getRuntime.nn.availableProcessors() | ||
8 * processors * processors | ||
} | ||
private[this] val monitors: Array[Object] = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,8 @@ object Scala3RunTime: | |
* Extracted to minimize the bytecode size at call site. | ||
*/ | ||
def nn[T](x: T | Null): x.type & T = | ||
if (x == null) throw new NullPointerException("tried to cast away nullability, but value is null") | ||
val isNull = x == null | ||
if (isNull) throw new NullPointerException("tried to cast away nullability, but value is null") | ||
Comment on lines
-18
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the benefit of this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A workaround for #7882 |
||
else x.asInstanceOf[x.type & T] | ||
|
||
end Scala3RunTime |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't use
eq
fornull
s anymore?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently not. The motivation comes from:
https://contributors.scala-lang.org/t/wip-scala-with-explicit-nulls/2761/4
I don't think this was discussed widely so if people feel strongly about it, we can discuss it.