Skip to content

Commit 6cc2028

Browse files
committed
add error tests
1 parent abbe971 commit 6cc2028

File tree

6 files changed

+45
-42
lines changed

6 files changed

+45
-42
lines changed

compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala

-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ object ErrorReporting {
161161
case fail: Implicits.NoMatchingImplicits => // do nothing
162162
case _ => attempts += ((failure.tree, ""))
163163
if foundWithoutNull then
164-
// !!! TODO: The need a test that tests this error message with a check file
165164
i""".
166165
|Since explicit-nulls is enabled, the selection is rejected because
167166
|${qualType.widen} could be null at runtime.

tests/explicit-nulls/neg-separate/upper-bounds/TestA_1.scala

-8
This file was deleted.

tests/explicit-nulls/neg-separate/upper-bounds/TestB_2.scala

-18
This file was deleted.

tests/explicit-nulls/neg/bounds.scala

+23-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
val x1: String = ???
2-
val x2: String | Null = ???
1+
// Test Null type in bounds
32

4-
// T has to be nullable type
5-
def f1[T >: Null <: AnyRef | Null](x: T): T = x
3+
class Test {
4+
val x1: String = ???
5+
val x2: String | Null = ???
66

7-
// Null is no longer a subtype of AnyRef, impossible to apply this method directly.
8-
// We can bypass this restriction by importing unsafeNulls.
9-
def f2[T >: Null <: AnyRef](x: T): T = x
7+
// T has to be nullable type
8+
def f1[T >: Null <: AnyRef | Null](x: T): T = x
109

11-
def nullOf[T >: Null <: AnyRef | Null]: T = null
10+
// Null is no longer a subtype of AnyRef, so it is impossible to apply this method directly.
11+
// However, defining this kind of functions is allowed.
12+
// We can bypass this restriction by importing unsafeNulls.
13+
def f2[T >: Null <: AnyRef](x: T): T = x
1214

13-
def g = {
14-
f1(x1)
15-
f1(x2)
15+
def nullOf[T >: Null <: AnyRef | Null]: T = null
1616

17-
f2(x1) // error
18-
f2(x2) // error
17+
def g = {
18+
f1(x1)
19+
f1(x2)
1920

20-
val n1: String = nullOf // error
21-
val n3: String | Null = nullOf
21+
f2(x1) // error
22+
f2(x2) // error
23+
24+
val n1: String = nullOf // error
25+
val n3: String | Null = nullOf
26+
}
27+
28+
// Bounds in class definition is strictly checked
29+
class A[T >: Null <: AnyRef] {} // error: conflicting bounds Null <: ... <: AnyRef
2230
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- [E008] Not Found Error: tests/explicit-nulls/neg/nn-basic.scala:3:21 ------------------------------------------------
2+
3 | val s2: String = s.trim // error
3+
| ^^^^^^
4+
| value trim is not a member of String | Null.
5+
| Since explicit-nulls is enabled, the selection is rejected because
6+
| String | Null could be null at runtime.
7+
| If you want to select trim without checking for a null value,
8+
| insert a .nn before .trim or import scala.language.unsafeNulls.
9+
-- [E008] Not Found Error: tests/explicit-nulls/neg/nn-basic.scala:5:17 ------------------------------------------------
10+
5 | val l: Int = s.length // error
11+
| ^^^^^^^^
12+
| value length is not a member of String | Null.
13+
| Since explicit-nulls is enabled, the selection is rejected because
14+
| String | Null could be null at runtime.
15+
| If you want to select length without checking for a null value,
16+
| insert a .nn before .length or import scala.language.unsafeNulls.
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Test {
2+
val s: String | Null = ???
3+
val s2: String = s.trim // error
4+
val s3: String | Null = s.nn.trim
5+
val l: Int = s.length // error
6+
}

0 commit comments

Comments
 (0)