-
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
Conversation
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.
Comments while reading out of curiosity
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 comment
The reason will be displayed to describe this comment to others. Learn more.
.nn.get(null)
reads strange to me - it looks redundant contradictory?
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.
Here classOf[sun.misc.Unsafe].getDeclaredFields.nn
is an Array[Field | Null]
and then after calling find
is an Option[Field | Null]
, so calling map would require asserting the inner value is also not null
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.
.map
is being called on Option[Field | Null]
.
The type of _
is thus Field | Null
.
The type of _.nn
is thus java.lang.reflect.Field
. This type has a get(Object)
method to read the value of the field. The API of this method says that for a static field, the parameter of type Object
is ignored and should be null.
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.
OK, thanks for the explanation!
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") |
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.
What is the benefit of this change?
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.
A workaround for #7882
@@ -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 def mapNull[A, B](a: A, 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 comment
The reason will be displayed to describe this comment to others. Learn more.
We can't use eq
for null
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.
Co-authored-by: Nicolas Stucki <[email protected]>
ceddf52
to
4a1c9f2
Compare
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.
LGTM
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if A = X | Null
(for some type X
)?
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.
That shouldn't be a problem: an expression of type X | Null | Null
is still allowed to be compared with null
.
- This was caused by changes in issue scala#13729
- This was caused by changes in issue scala#13729
- This was caused by changes in issue scala#13729
- This was caused by changes in issue scala#13729
- This was caused by changes in issue scala#13729
- This was caused by changes in issue scala#13729
- This was caused by changes in issue scala#13729
No description provided.