Skip to content

Commit cdc25fd

Browse files
authored
Merge pull request #3929 from dotty-staging/fix-#746
Fix #746: Generate efficient try cases for parameterized exceptions
2 parents 1793e86 + 578b334 commit cdc25fd

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class TryCatchPatterns extends MiniPhase {
7676
(pre == NoPrefix || pre.widen.typeSymbol.isStatic) && // Does not require outer class check
7777
!tp.symbol.is(Flags.Trait) && // Traits not supported by JVM
7878
tp.derivesFrom(defn.ThrowableClass)
79+
case tp: AppliedType =>
80+
isSimpleThrowable(tp.tycon)
7981
case _ =>
8082
false
8183
}

compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala

+28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package dotty.tools.backend.jvm
33
import org.junit.Assert._
44
import org.junit.Test
55

6+
import scala.tools.asm.Opcodes
7+
68
class TestBCode extends DottyBytecodeTest {
79
import ASMConverters._
810
@Test def nullChecks = {
@@ -213,4 +215,30 @@ class TestBCode extends DottyBytecodeTest {
213215
assert(!arrayWrapped, "Arrays should not be wrapped when passed to a Java varargs method\n")
214216
}
215217
}
218+
219+
@Test def efficientTryCases = {
220+
val source =
221+
"""
222+
|class Test {
223+
| def test =
224+
| try print("foo")
225+
| catch {
226+
| case _: scala.runtime.NonLocalReturnControl[_] => ()
227+
| }
228+
|}
229+
""".stripMargin
230+
231+
checkBCode(source) { dir =>
232+
val moduleIn = dir.lookupName("Test.class", directory = false)
233+
val moduleNode = loadClassNode(moduleIn.input)
234+
val method = getMethod(moduleNode, "test")
235+
236+
val hasInstanceof = instructionsFromMethod(method).exists {
237+
case TypeOp(Opcodes.INSTANCEOF, _) => true
238+
case _ => false
239+
}
240+
241+
assert(!hasInstanceof, "Try case should not issue INSTANCEOF opcode\n")
242+
}
243+
}
216244
}

0 commit comments

Comments
 (0)