Skip to content

Commit 3e2dd6e

Browse files
committed
add tests
1 parent 6b6fd58 commit 3e2dd6e

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

tests/explicit-nulls/neg/basic.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Test that reference types are no longer nullable.
2+
3+
class Foo {
4+
val s: String = null // error
5+
val s1: String | Null = null
6+
val s2: String | Null = ""
7+
8+
val b: Boolean = null // error
9+
val c: Int | Null = null
10+
11+
val ar1: AnyRef = null // error
12+
val ar2: AnyRef | Null = null
13+
val ob: Object = null // error
14+
15+
val av: AnyVal = null // error
16+
val a: Any = null
17+
val n: Null = null
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Foo {
2+
val x: String = null // error: String is non-nullable
3+
4+
def foo(x: String): String = "x"
5+
6+
val y = foo(null) // error: String argument is non-nullable
7+
8+
val z: String = foo("hello")
9+
10+
class Bar
11+
val b: Bar = null // error: user-created classes are also non-nullable
12+
}

tests/explicit-nulls/run/unsafe-nulls.scala

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22
// Enabling unsafeNulls allows this kind of unsafe operations,
33
// but could cause exception during runtime.
44

5-
import scala.language.unsafeNulls
5+
object F {
6+
def apply(x: String): String = x
7+
}
8+
9+
object G {
10+
def h(f: String | Null => String, x: String | Null): String | Null =
11+
f(x)
12+
}
613

714
object Test {
15+
import scala.language.unsafeNulls
16+
817
def main(args: Array[String]): Unit = {
9-
val s: String | Null = "hello"
10-
assert(s.length == 5)
18+
val s1: String | Null = "hello"
19+
assert(s1.length == 5)
1120

1221
val s2: String | Null = null
1322
try {
@@ -17,5 +26,11 @@ object Test {
1726
case e: NullPointerException =>
1827
// ok: Selecting on a null value would throw NullPointerException.
1928
}
29+
30+
val s3: String = F(s1)
31+
assert(s3.length == 5)
32+
33+
val s4: String = G.h(F.apply, s1)
34+
assert(s4.length == 5)
2035
}
2136
}

0 commit comments

Comments
 (0)