Skip to content

Fix #746: Generate efficient try cases for parameterized exceptions #3929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class TryCatchPatterns extends MiniPhase {
(pre == NoPrefix || pre.widen.typeSymbol.isStatic) && // Does not require outer class check
!tp.symbol.is(Flags.Trait) && // Traits not supported by JVM
tp.derivesFrom(defn.ThrowableClass)
case tp: AppliedType =>
isSimpleThrowable(tp.tycon)
case _ =>
false
}
Expand Down
28 changes: 28 additions & 0 deletions compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dotty.tools.backend.jvm
import org.junit.Assert._
import org.junit.Test

import scala.tools.asm.Opcodes

class TestBCode extends DottyBytecodeTest {
import ASMConverters._
@Test def nullChecks = {
Expand Down Expand Up @@ -213,4 +215,30 @@ class TestBCode extends DottyBytecodeTest {
assert(!arrayWrapped, "Arrays should not be wrapped when passed to a Java varargs method\n")
}
}

@Test def efficientTryCases = {
val source =
"""
|class Test {
| def test =
| try print("foo")
| catch {
| case _: scala.runtime.NonLocalReturnControl[_] => ()
| }
|}
""".stripMargin

checkBCode(source) { dir =>
val moduleIn = dir.lookupName("Test.class", directory = false)
val moduleNode = loadClassNode(moduleIn.input)
val method = getMethod(moduleNode, "test")

val hasInstanceof = instructionsFromMethod(method).exists {
case TypeOp(Opcodes.INSTANCEOF, _) => true
case _ => false
}

assert(!hasInstanceof, "Try case should not issue INSTANCEOF opcode\n")
}
}
}