From 24e5777dc964c005cf7ba13617caa3da26574849 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Tue, 29 Jun 2021 11:39:11 +0200 Subject: [PATCH] Refine condition for eliding return in pattern matching Reverting to the first version of #5082. Fixes #12976 --- .../tools/dotc/transform/PatternMatcher.scala | 2 +- tests/run/i12976.scala | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 tests/run/i12976.scala diff --git a/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala b/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala index 047b2587299a..ea72f476eb18 100644 --- a/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -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)) } } diff --git a/tests/run/i12976.scala b/tests/run/i12976.scala new file mode 100644 index 000000000000..a14a18885f1e --- /dev/null +++ b/tests/run/i12976.scala @@ -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 + } + +} \ No newline at end of file