Skip to content
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

add eq/ne extension for AnyRef|Null to Scala3RunTime #14632

Merged
merged 12 commits into from
May 10, 2022
14 changes: 14 additions & 0 deletions library/src/scala/runtime/stdLibPatches/Predef.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package scala.runtime.stdLibPatches

import scala.annotation.experimental

object Predef:
import compiletime.summonFrom

Expand Down Expand Up @@ -47,4 +49,16 @@ object Predef:
*/
extension [T](x: T | Null) inline def nn: x.type & T =
scala.runtime.Scala3RunTime.nn(x)

/** Enables an expression of type `T|Null`, where `T` is a subtype of `AnyRef`, to be checked for `null`
* using `eq` and `ne`, rather than only `==` and `!=`. This is needed because `Null` no longer has
* `eq` or `ne` methods, only `==` and `!=` inherited from `Any`. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think comments need to be on individual methods, not sure if scaladoc/IDEs pick up anything from the extension block.

extension (inline x: AnyRef | Null)
@experimental
inline def eq(inline y: AnyRef | Null): Boolean =
x.asInstanceOf[AnyRef] eq y.asInstanceOf[AnyRef]
@experimental
inline def ne(inline y: AnyRef | Null): Boolean =
!(x eq y)

end Predef
54 changes: 27 additions & 27 deletions tests/coverage/pos/Inlined.scoverage.check
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ Inlined$package$
Object
covtest.Inlined$package$
testInlined
340
367
9
378
405
11
Scala3RunTime
Select
false
Expand All @@ -59,9 +59,9 @@ Inlined$package$
Object
covtest.Inlined$package$
testInlined
340
382
9
378
420
11
assertFailed
Apply
false
Expand All @@ -76,9 +76,9 @@ Inlined$package$
Object
covtest.Inlined$package$
testInlined
340
382
9
378
420
11
<none>
Block
true
Expand Down Expand Up @@ -127,9 +127,9 @@ Inlined$package$
Object
covtest.Inlined$package$
testInlined
340
367
9
378
405
11
Scala3RunTime
Select
false
Expand All @@ -144,9 +144,9 @@ Inlined$package$
Object
covtest.Inlined$package$
testInlined
340
382
9
378
420
11
assertFailed
Apply
false
Expand All @@ -161,9 +161,9 @@ Inlined$package$
Object
covtest.Inlined$package$
testInlined
340
382
9
378
420
11
<none>
Block
true
Expand Down Expand Up @@ -212,9 +212,9 @@ Inlined$package$
Object
covtest.Inlined$package$
testInlined
340
367
9
378
405
11
Scala3RunTime
Select
false
Expand All @@ -229,9 +229,9 @@ Inlined$package$
Object
covtest.Inlined$package$
testInlined
340
382
9
378
420
11
assertFailed
Apply
false
Expand All @@ -246,9 +246,9 @@ Inlined$package$
Object
covtest.Inlined$package$
testInlined
340
382
9
378
420
11
<none>
Block
true
Expand Down
16 changes: 16 additions & 0 deletions tests/explicit-nulls/pos/eq-ne.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
val s1: String = ???
val s2: String | Null = ???

def f = {
s1 eq s2
s2 eq s1

s1 ne s2
s2 ne s1

s1 eq null
s2 eq null

null eq s1
null eq s2
}
16 changes: 0 additions & 16 deletions tests/explicit-nulls/unsafe-common/unsafe-eq.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ val experimentalDefinitionInLibrary = Set(
// Need experimental annotation macros to check that design works.
"scala.quoted.Quotes.reflectModule.ClassDefModule.apply",
"scala.quoted.Quotes.reflectModule.SymbolModule.newClass",

//// New extension methods: Explicit Nulls
// Should be stabilized in 3.2.0.
"scala.Predef$.eq",
"scala.Predef$.ne",
)


Expand Down