Skip to content

Commit 607e130

Browse files
committed
Use MatchCaseUnreachable
1 parent 6e12d35 commit 607e130

File tree

8 files changed

+45
-20
lines changed

8 files changed

+45
-20
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -948,11 +948,11 @@ class UncheckedTypePattern(argType: Type, whyNot: String)(using Context)
948948
|"""
949949
}
950950

951-
class MatchCaseUnreachable()(using Context)
951+
class MatchCaseUnreachable(why: String = "")(using Context)
952952
extends Message(MatchCaseUnreachableID) {
953953
def kind = MessageKind.MatchCaseUnreachable
954-
def msg(using Context) = "Unreachable case"
955-
def explain(using Context) = ""
954+
override protected def msg(using Context) = "Unreachable case"
955+
override protected def explain(using Context) = why
956956
}
957957

958958
class MatchCaseOnlyNullWarning()(using Context)

compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,11 @@ object TypeTestsCasts {
205205
def testCls = effectiveClass(testType.widen)
206206
def unboxedTestCls = effectiveClass(unboxedTestType.widen)
207207

208-
def unreachable(why: => String)(using Context): Boolean = {
209-
if (flagUnrelated)
210-
if (inMatch) report.error(em"this case is unreachable since $why", expr.srcPos)
208+
def unreachable(why: => String)(using Context): false =
209+
if flagUnrelated then
210+
if inMatch then report.error(MatchCaseUnreachable(why), expr.srcPos)
211211
else report.warning(em"this will always yield false since $why", expr.srcPos)
212212
false
213-
}
214213

215214
/** Are `foundCls` and `testCls` classes that allow checks
216215
* whether a test would be always false?

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,8 +949,8 @@ object SpaceEngine {
949949
if prev == Empty && covered == Empty then // defer until a case is reachable
950950
recur(rest, prevs, pat :: deferred)
951951
else
952-
for pat <- deferred.reverseIterator
953-
do report.warning(MatchCaseUnreachable(), pat.srcPos)
952+
for deferral <- deferred.reverseIterator
953+
do report.warning(MatchCaseUnreachable(), deferral.srcPos)
954954

955955
if pat != EmptyTree // rethrow case of catch uses EmptyTree
956956
&& !pat.symbol.isAllOf(SyntheticCase, butNot=Method) // ExpandSAMs default cases use SyntheticCase

tests/neg/i11118.check

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
| If the narrowing is intentional, this can be communicated by adding `.runtimeChecked` after the expression,
77
| which may result in a MatchError at runtime.
88
| This patch can be rewritten automatically under -rewrite -source 3.8-migration.
9-
-- Error: tests/neg/i11118.scala:2:4 -----------------------------------------------------------------------------------
9+
-- [E030] Match case Unreachable Error: tests/neg/i11118.scala:2:4 -----------------------------------------------------
1010
2 |val (a,b) = (1,2,3) // error // warning
1111
| ^
12-
| this case is unreachable since type (Int, Int, Int) is not a subclass of class Tuple2
12+
| Unreachable case
13+
|
14+
| longer explanation available when compiling with `-explain`

tests/neg/i24789.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//> using options -Werror -explain
2+
3+
case class A(a: Int)
4+
case class B(b: Int)
5+
case class C(c: Int)
6+
7+
val a = (A(1): A | B) match
8+
case A(_) => "OK"
9+
case B(_) => "OK"
10+
case C(_) => "Not OK" // error

tests/neg/i8711.check

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,22 @@
1010
19 | case x: (B | C) => x // warn
1111
| ^^^^^^^^^^
1212
| Unreachable case
13-
-- Error: tests/neg/i8711.scala:9:9 ------------------------------------------------------------------------------------
13+
-- [E030] Match case Unreachable Error: tests/neg/i8711.scala:9:9 ------------------------------------------------------
1414
9 | case x: B => x // error: this case is unreachable since class A is not a subclass of class B
1515
| ^
16-
| this case is unreachable since type A and class B are unrelated
17-
-- Error: tests/neg/i8711.scala:14:9 -----------------------------------------------------------------------------------
16+
| Unreachable case
17+
|---------------------------------------------------------------------------------------------------------------------
18+
| Explanation (enabled by `-explain`)
19+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20+
| type A and class B are unrelated
21+
---------------------------------------------------------------------------------------------------------------------
22+
-- [E030] Match case Unreachable Error: tests/neg/i8711.scala:14:9 -----------------------------------------------------
1823
14 | case x: C => x // error
1924
| ^
20-
| this case is unreachable since type A | B and class C are unrelated
25+
| Unreachable case
26+
|--------------------------------------------------------------------------------------------------------------------
27+
| Explanation (enabled by `-explain`)
28+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
29+
| type A | B and class C are unrelated
30+
--------------------------------------------------------------------------------------------------------------------
2131
No warnings can be incurred under -Werror

tests/neg/i8711.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//> using options -Werror
1+
//> using options -Werror -explain
22

33
class A
44
class B

tests/neg/named-tuples-2.check

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
-- Error: tests/neg/named-tuples-2.scala:4:9 ---------------------------------------------------------------------------
1+
-- [E030] Match case Unreachable Error: tests/neg/named-tuples-2.scala:4:9 ---------------------------------------------
22
4 | case (name, age) => () // error
33
| ^
4-
| this case is unreachable since type (String, Int, Boolean) is not a subclass of class Tuple2
5-
-- Error: tests/neg/named-tuples-2.scala:5:9 ---------------------------------------------------------------------------
4+
| Unreachable case
5+
|
6+
| longer explanation available when compiling with `-explain`
7+
-- [E030] Match case Unreachable Error: tests/neg/named-tuples-2.scala:5:9 ---------------------------------------------
68
5 | case (n, a, m, x) => () // error
79
| ^
8-
| this case is unreachable since type (String, Int, Boolean) is not a subclass of class Tuple4
10+
| Unreachable case
11+
|
12+
| longer explanation available when compiling with `-explain`

0 commit comments

Comments
 (0)