Skip to content

Commit

Permalink
Refine condition for eliding return in pattern matching
Browse files Browse the repository at this point in the history
Reverting to the first version of scala#5082.

Fixes scala#12976
  • Loading branch information
odersky authored and BarkingBad committed Jul 23, 2021
1 parent 0626f62 commit 24e5777
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ object PatternMatcher {
default
}
case ResultPlan(tree) =>
if (tree.tpe <:< defn.NothingType) tree // For example MatchError
if (tree.symbol == defn.throwMethod) tree // For example MatchError
else Return(tree, ref(resultLabel))
}
}
Expand Down
39 changes: 39 additions & 0 deletions tests/run/i12976.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

case class A(s: String)

class B {
def b1[X](str: String): String = str

def b2[X](str: String): X = null.asInstanceOf[X]
}

object Test {

def main(args: Array[String]): Unit = {
val a = A("aaa")
val b = new B

// no error
a match {
case A(s) =>
b.b1(s)
}

// no error if add explicit type param
a match {
case A(s) =>
b.b2[Boolean](s)
}

// scala.MatchError: A(aaa)
try
a match {
case A(s) =>
b.b2(s)
}
assert(false)
catch case ex: NullPointerException =>
() // OK
}

}

0 comments on commit 24e5777

Please sign in to comment.