Skip to content

Commit da9ae84

Browse files
committed
Fix spacing, i21577.check, and warn comment changes
1 parent 442146a commit da9ae84

File tree

9 files changed

+102
-59
lines changed

9 files changed

+102
-59
lines changed

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -969,12 +969,5 @@ object SpaceEngine {
969969

970970
def checkMatch(m: Match)(using Context): Unit =
971971
if exhaustivityCheckable(m.selector) then checkExhaustivity(m)
972-
973-
if reachabilityCheckable(m.selector) then
974-
// With explicit nulls, even if the selector type is non-nullable,
975-
// we still need to consider the possibility of null value,
976-
// so we use the after-erasure nullability for space operations
977-
// to achieve consistent runtime behavior.
978-
// For example, `val x: String = ???; x match { case null => }` should not be unreachable.
979-
withoutMode(Mode.SafeNulls)(checkReachability(m))
972+
if reachabilityCheckable(m.selector) then checkReachability(m)
980973
}

tests/explicit-nulls/neg/flow-match.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
object MatchTest {
44
def f6(s: String | Null): String = s match {
55
case s2 => s2 // error
6-
case null => "other"
7-
case s3 => s3
6+
case s3 => s3 // OK since not null
87
}
98

109
def f7(s: String | Null): String = s match {
1110
case null => "other"
12-
case null => "other"
13-
case s3 => s3
11+
case s3 => s3 // OK since not null
1412
}
15-
}
13+
}

tests/explicit-nulls/run/pattern-matching.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ object Test:
3434
val r6 = s2 match
3535
case null => 200
3636
case s2: String => 100
37-
assert(r6 == 200)
37+
assert(r6 == 200)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- [E030] Match case Unreachable Warning: tests/explicit-nulls/warn/flow-match.scala:6:9 -------------------------------
2+
6 | case null => "other" // warn
3+
| ^^^^
4+
| Unreachable case
5+
-- [E030] Match case Unreachable Warning: tests/explicit-nulls/warn/flow-match.scala:7:9 -------------------------------
6+
7 | case s3 => s3 // warn
7+
| ^^
8+
| Unreachable case
9+
-- [E030] Match case Unreachable Warning: tests/explicit-nulls/warn/flow-match.scala:12:9 ------------------------------
10+
12 | case null => "other" // warn
11+
| ^^^^
12+
| Unreachable case
13+
-- [E030] Match case Unreachable Warning: tests/explicit-nulls/warn/flow-match.scala:14:9 ------------------------------
14+
14 | case s4 => s4.nn // warn
15+
| ^^
16+
| Unreachable case
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Test unreachable matches in presence of nulls
2+
3+
object MatchTest2 {
4+
def f6(s: String | Null): String = s match {
5+
case s2 => s2.nn
6+
case null => "other" // warn
7+
case s3 => s3 // warn
8+
}
9+
10+
def f7(s: String | Null): String = s match {
11+
case null => "other"
12+
case null => "other" // warn
13+
case s3: String => s3
14+
case s4 => s4.nn // warn
15+
}
16+
}
Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
-- [E121] Pattern Match Warning: tests\explicit-nulls\warn\i21577.scala:5:9 --------------------------------------------
2-
5 | case _ => // warn
3-
| ^
4-
| Unreachable case except for null (if this is intentional, consider writing case null => instead).
5-
-- [E121] Pattern Match Warning: tests\explicit-nulls\warn\i21577.scala:12:9 -------------------------------------------
6-
12 | case _ => // warn
7-
| ^
8-
| Unreachable case except for null (if this is intentional, consider writing case null => instead).
9-
-- [E121] Pattern Match Warning: tests\explicit-nulls\warn\i21577.scala:16:7 -------------------------------------------
10-
16 | case _ => // warn
11-
| ^
12-
| Unreachable case except for null (if this is intentional, consider writing case null => instead).
13-
-- [E121] Pattern Match Warning: tests\explicit-nulls\warn\i21577.scala:20:7 -------------------------------------------
14-
20 | case _ => // warn
15-
| ^
16-
| Unreachable case except for null (if this is intentional, consider writing case null => instead).
17-
-- [E029] Pattern Match Exhaustivity Warning: tests\explicit-nulls\warn\i21577.scala:29:27 -----------------------------
18-
29 |def f7(s: String | Null) = s match // warn
19-
| ^
20-
| match may not be exhaustive.
21-
|
22-
| It would fail on pattern case: _: Null
23-
|
24-
| longer explanation available when compiling with `-explain`
25-
-- [E029] Pattern Match Exhaustivity Warning: tests\explicit-nulls\warn\i21577.scala:36:33 -----------------------------
26-
36 |def f9(s: String | Int | Null) = s match // warn
27-
| ^
28-
| match may not be exhaustive.
29-
|
30-
| It would fail on pattern case: _: Int
31-
|
32-
| longer explanation available when compiling with `-explain`
1+
-- [E121] Pattern Match Warning: tests/explicit-nulls/warn/i21577.scala:5:9 --------------------------------------------
2+
5 | case _ => // warn: null only
3+
| ^
4+
| Unreachable case except for null (if this is intentional, consider writing case null => instead).
5+
-- [E121] Pattern Match Warning: tests/explicit-nulls/warn/i21577.scala:12:9 -------------------------------------------
6+
12 | case _ => // warn: null only
7+
| ^
8+
| Unreachable case except for null (if this is intentional, consider writing case null => instead).
9+
-- [E121] Pattern Match Warning: tests/explicit-nulls/warn/i21577.scala:16:7 -------------------------------------------
10+
16 | case _ => // warn: null only
11+
| ^
12+
| Unreachable case except for null (if this is intentional, consider writing case null => instead).
13+
-- [E030] Match case Unreachable Warning: tests/explicit-nulls/warn/i21577.scala:20:7 ----------------------------------
14+
20 | case _ => // warn: unreachable
15+
| ^
16+
| Unreachable case
17+
-- [E029] Pattern Match Exhaustivity Warning: tests/explicit-nulls/warn/i21577.scala:29:27 -----------------------------
18+
29 |def f7(s: String | Null) = s match // warn: not exhaustive
19+
| ^
20+
| match may not be exhaustive.
21+
|
22+
| It would fail on pattern case: _: Null
23+
|
24+
| longer explanation available when compiling with `-explain`
25+
-- [E029] Pattern Match Exhaustivity Warning: tests/explicit-nulls/warn/i21577.scala:36:33 -----------------------------
26+
36 |def f9(s: String | Int | Null) = s match // warn: not exhaustive
27+
| ^
28+
| match may not be exhaustive.
29+
|
30+
| It would fail on pattern case: _: Int
31+
|
32+
| longer explanation available when compiling with `-explain`

tests/explicit-nulls/warn/i21577.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ def f(s: String) =
22
val s2 = s.trim()
33
s2 match
44
case s3: String =>
5-
case _ => // warn
5+
case _ => // warn: null only
66

77

88
def f2(s: String | Null) =
99
val s2 = s.nn.trim()
1010
s2 match
1111
case s3: String =>
12-
case _ => // warn
12+
case _ => // warn: null only
1313

1414
def f3(s: String | Null) = s match
1515
case s2: String =>
16-
case _ => // warn
16+
case _ => // warn: null only
1717

1818
def f5(s: String) = s match
1919
case _: String =>
20-
case _ => // warn
20+
case _ => // warn: unreachable
2121

2222
def f6(s: String) = s.trim() match
2323
case _: String =>
@@ -26,13 +26,13 @@ def f6(s: String) = s.trim() match
2626
def f61(s: String) = s.trim() match
2727
case _: String =>
2828

29-
def f7(s: String | Null) = s match // warn
29+
def f7(s: String | Null) = s match // warn: not exhaustive
3030
case _: String =>
3131

3232
def f8(s: String | Null) = s match
3333
case _: String =>
3434
case null =>
3535

36-
def f9(s: String | Int | Null) = s match // warn
36+
def f9(s: String | Int | Null) = s match // warn: not exhaustive
3737
case _: String =>
38-
case null =>
38+
case null =>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- [E030] Match case Unreachable Warning: tests/explicit-nulls/warn/pattern-matching.scala:7:9 -------------------------
2+
7 | case _ => 200 // warn: unreachable case except for null
3+
| ^
4+
| Unreachable case
5+
-- [E030] Match case Unreachable Warning: tests/explicit-nulls/warn/pattern-matching.scala:11:9 ------------------------
6+
11 | case null => 200 // warn: unreachable
7+
| ^^^^
8+
| Unreachable case
9+
-- [E030] Match case Unreachable Warning: tests/explicit-nulls/warn/pattern-matching.scala:14:9 ------------------------
10+
14 | case null => 100 // warn: unreachable
11+
| ^^^^
12+
| Unreachable case
13+
-- [E121] Pattern Match Warning: tests/explicit-nulls/warn/pattern-matching.scala:21:9 ---------------------------------
14+
21 | case _ => 200 // warn: unreachable case except for null
15+
| ^
16+
| Unreachable case except for null (if this is intentional, consider writing case null => instead).
17+
-- [E030] Match case Unreachable Warning: tests/explicit-nulls/warn/pattern-matching.scala:39:9 ------------------------
18+
39 | case _ => 300 // warn: unreachable case except for null
19+
| ^
20+
| Unreachable case
21+
-- [E121] Pattern Match Warning: tests/explicit-nulls/warn/pattern-matching.scala:45:9 ---------------------------------
22+
45 | case _ => 300 // warn: unreachable case except for null
23+
| ^
24+
| Unreachable case except for null (if this is intentional, consider writing case null => instead).

tests/explicit-nulls/neg/pattern-matching.scala renamed to tests/explicit-nulls/warn/pattern-matching.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//> using options -Xfatal-warnings
2-
31
class Foo:
42

53
val s: String = ???
@@ -10,10 +8,10 @@ class Foo:
108

119
s match
1210
case s: String => 100
13-
case null => 200
11+
case null => 200 // warn: unreachable
1412

1513
s match
16-
case null => 100
14+
case null => 100 // warn: unreachable
1715
case _ => 200
1816

1917
val s2: String | Null = ???
@@ -50,5 +48,3 @@ class Foo:
5048
case Dog(_) => 100
5149
case Cat => 200
5250
case null => 300 // ok
53-
54-
// nopos-error: No warnings can be incurred under -Werror (or -Xfatal-warnings)

0 commit comments

Comments
 (0)