Skip to content

Commit f34a3bb

Browse files
authored
Merge pull request #12977 from dotty-staging/fix-12976
Refine condition for eliding return in pattern matching
2 parents d96a500 + 61b82a0 commit f34a3bb

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ object PatternMatcher {
928928
default
929929
}
930930
case ResultPlan(tree) =>
931-
if (tree.tpe <:< defn.NothingType) tree // For example MatchError
931+
if (tree.symbol == defn.throwMethod) tree // For example MatchError
932932
else Return(tree, ref(resultLabel))
933933
}
934934
}

tests/run/i12976.scala

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
case class A(s: String)
3+
4+
class B {
5+
def b1[X](str: String): String = str
6+
7+
def b2[X](str: String): X = null.asInstanceOf[X]
8+
}
9+
10+
object Test {
11+
12+
def main(args: Array[String]): Unit = {
13+
val a = A("aaa")
14+
val b = new B
15+
16+
// no error
17+
a match {
18+
case A(s) =>
19+
b.b1(s)
20+
}
21+
22+
// no error if add explicit type param
23+
a match {
24+
case A(s) =>
25+
b.b2[Boolean](s)
26+
}
27+
28+
// scala.MatchError: A(aaa)
29+
try
30+
a match {
31+
case A(s) =>
32+
b.b2(s)
33+
}
34+
assert(false)
35+
catch case ex: NullPointerException =>
36+
() // OK
37+
}
38+
39+
}

0 commit comments

Comments
 (0)