Skip to content

Commit 5c7c312

Browse files
committed
modify eq tests
1 parent 56cdadc commit 5c7c312

File tree

6 files changed

+42
-81
lines changed

6 files changed

+42
-81
lines changed

library/src/dotty/DottyPredef.scala

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,4 @@ object DottyPredef {
5151
def[T] (x: T|Null) nn: x.type & T =
5252
if (x == null) throw new NullPointerException("tried to cast away nullability, but value is null")
5353
else x.asInstanceOf[x.type & T]
54-
55-
/** Reference equality where the receiver is a nullable union.
56-
* Note that if the receiver `r` is a reference type (e.g. `String`), then `r.eq` will invoke the
57-
* `eq` method in `AnyRef`.
58-
*/
59-
def (x: AnyRef|Null) eq(y: AnyRef|Null): Boolean =
60-
x.asInstanceOf[AnyRef] eq y.asInstanceOf[AnyRef]
61-
62-
/** Reference disequality where the receiver is a nullable union.
63-
* Note that if the receiver `r` is a reference type (e.g. `String`), then `r.ne` will invoke the
64-
* `ne` method in `AnyRef`.
65-
*/
66-
def (x: AnyRef|Null) ne(y: AnyRef|Null): Boolean =
67-
x.asInstanceOf[AnyRef] ne y.asInstanceOf[AnyRef]
6854
}

tests/explicit-nulls/neg/eq.scala

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,31 @@ class Foo {
33
// Null itself
44
val x0: Null = null
55
x0 == null
6+
x0 != null
67
null == x0
78
null == null
89

910
// Nullable types: OK
1011
val x1: String|Null = null
1112
x1 == null
1213
null == x1
14+
x1 == x0
15+
x1 != x0
16+
x0 == x1
1317

1418
// Reference types, even non-nullable ones: OK.
1519
// Allowed as an escape hatch.
1620
val x2: String = "hello"
17-
x2 != null
21+
x2 != null
1822
x2 == null
1923
null == x2
2024

2125
// Value types: not allowed.
22-
1 == null // error
23-
null == 1 // error
24-
true == null // error
25-
null == true // error
26+
1 == null // error
27+
null != 0 // error
28+
null == 0 // error
29+
true == null // error
30+
null == false // error
31+
'a' == null // error
32+
null == 'b' // error
2633
}

tests/explicit-nulls/neg/eq2.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
21
// Test that we can't compare for equality `null` and
32
// classes that derive from AnyVal.
43
class Foo(x: Int) extends AnyVal
54

65
class Bar {
76
val foo: Foo = new Foo(15)
8-
if (foo == null) {} // error
7+
if (foo == null) {} // error: Values of types Null and Foo cannot be compared
98
if (null == foo) {} // error
9+
if (foo != null) {} // error
10+
if (null != foo) {} // error
1011

1112
// To test against null, make the type nullable.
1213
val foo2: Foo|Null = foo
13-
if (foo2 == null) {}
14+
if (foo2 == null) {}
1415
if (null == foo2) {}
16+
if (foo2 != null) {}
17+
if (null != foo2) {}
1518
}

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

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

tests/explicit-nulls/run/eq.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
object Test {
3+
4+
def main(args: Array[String]): Unit = {
5+
val x: String|Null = ""
6+
val y: String|Null = null
7+
val z: String = ""
8+
9+
assert(x == x)
10+
assert(x == "")
11+
assert("" == x)
12+
assert(x == z)
13+
assert(z == x)
14+
assert(x != "xx")
15+
assert(x != y)
16+
assert(y == y)
17+
18+
assert(x != null)
19+
assert(null != x)
20+
assert(y == null)
21+
assert(null == y)
22+
assert(null == null)
23+
}
24+
}

tests/explicit-nulls/run/subtype-any.scala

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

0 commit comments

Comments
 (0)