Skip to content

Commit cb22645

Browse files
authored
Merge pull request #14632 from dotty-staging/add-eq-ne
add eq/ne extension methods for AnyRef|Null to Predef
2 parents 96ac286 + 7c6b559 commit cb22645

File tree

5 files changed

+65
-43
lines changed

5 files changed

+65
-43
lines changed

library/src/scala/runtime/stdLibPatches/Predef.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package scala.runtime.stdLibPatches
22

3+
import scala.annotation.experimental
4+
35
object Predef:
46
import compiletime.summonFrom
57

@@ -47,4 +49,19 @@ object Predef:
4749
*/
4850
extension [T](x: T | Null) inline def nn: x.type & T =
4951
scala.runtime.Scala3RunTime.nn(x)
52+
53+
extension (inline x: AnyRef | Null)
54+
/** Enables an expression of type `T|Null`, where `T` is a subtype of `AnyRef`, to be checked for `null`
55+
* using `eq` rather than only `==`. This is needed because `Null` no longer has
56+
* `eq` or `ne` methods, only `==` and `!=` inherited from `Any`. */
57+
@experimental
58+
inline def eq(inline y: AnyRef | Null): Boolean =
59+
x.asInstanceOf[AnyRef] eq y.asInstanceOf[AnyRef]
60+
/** Enables an expression of type `T|Null`, where `T` is a subtype of `AnyRef`, to be checked for `null`
61+
* using `ne` rather than only `!=`. This is needed because `Null` no longer has
62+
* `eq` or `ne` methods, only `==` and `!=` inherited from `Any`. */
63+
@experimental
64+
inline def ne(inline y: AnyRef | Null): Boolean =
65+
!(x eq y)
66+
5067
end Predef

tests/coverage/pos/Inlined.scoverage.check

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ Inlined$package$
4242
Object
4343
covtest.Inlined$package$
4444
testInlined
45-
340
46-
367
47-
9
45+
378
46+
405
47+
11
4848
Scala3RunTime
4949
Select
5050
false
@@ -59,9 +59,9 @@ Inlined$package$
5959
Object
6060
covtest.Inlined$package$
6161
testInlined
62-
340
63-
382
64-
9
62+
378
63+
420
64+
11
6565
assertFailed
6666
Apply
6767
false
@@ -76,9 +76,9 @@ Inlined$package$
7676
Object
7777
covtest.Inlined$package$
7878
testInlined
79-
340
80-
382
81-
9
79+
378
80+
420
81+
11
8282
<none>
8383
Block
8484
true
@@ -127,9 +127,9 @@ Inlined$package$
127127
Object
128128
covtest.Inlined$package$
129129
testInlined
130-
340
131-
367
132-
9
130+
378
131+
405
132+
11
133133
Scala3RunTime
134134
Select
135135
false
@@ -144,9 +144,9 @@ Inlined$package$
144144
Object
145145
covtest.Inlined$package$
146146
testInlined
147-
340
148-
382
149-
9
147+
378
148+
420
149+
11
150150
assertFailed
151151
Apply
152152
false
@@ -161,9 +161,9 @@ Inlined$package$
161161
Object
162162
covtest.Inlined$package$
163163
testInlined
164-
340
165-
382
166-
9
164+
378
165+
420
166+
11
167167
<none>
168168
Block
169169
true
@@ -212,9 +212,9 @@ Inlined$package$
212212
Object
213213
covtest.Inlined$package$
214214
testInlined
215-
340
216-
367
217-
9
215+
378
216+
405
217+
11
218218
Scala3RunTime
219219
Select
220220
false
@@ -229,9 +229,9 @@ Inlined$package$
229229
Object
230230
covtest.Inlined$package$
231231
testInlined
232-
340
233-
382
234-
9
232+
378
233+
420
234+
11
235235
assertFailed
236236
Apply
237237
false
@@ -246,9 +246,9 @@ Inlined$package$
246246
Object
247247
covtest.Inlined$package$
248248
testInlined
249-
340
250-
382
251-
9
249+
378
250+
420
251+
11
252252
<none>
253253
Block
254254
true

tests/explicit-nulls/pos/eq-ne.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
val s1: String = ???
2+
val s2: String | Null = ???
3+
4+
def f = {
5+
s1 eq s2
6+
s2 eq s1
7+
8+
s1 ne s2
9+
s2 ne s1
10+
11+
s1 eq null
12+
s2 eq null
13+
14+
null eq s1
15+
null eq s2
16+
}

tests/explicit-nulls/unsafe-common/unsafe-eq.scala

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/run-custom-args/tasty-inspector/stdlibExperimentalDefinitions.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ val experimentalDefinitionInLibrary = Set(
7676
// Need experimental annotation macros to check that design works.
7777
"scala.quoted.Quotes.reflectModule.ClassDefModule.apply",
7878
"scala.quoted.Quotes.reflectModule.SymbolModule.newClass",
79+
80+
//// New extension methods: Explicit Nulls
81+
// Should be stabilized in 3.2.0.
82+
"scala.Predef$.eq",
83+
"scala.Predef$.ne",
7984
)
8085

8186

0 commit comments

Comments
 (0)