Skip to content

Commit 831f9eb

Browse files
committed
Do not generate Return nodes for throw expressions
Namely throw MatchError
1 parent e02c785 commit 831f9eb

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,8 @@ object PatternMatcher {
839839
default
840840
}
841841
case ResultPlan(tree) =>
842-
Return(tree, ref(resultLabel))
842+
if (tree.symbol eq defn.throwMethod) tree // Namely MatchError
843+
else Return(tree, ref(resultLabel))
843844
}
844845
}
845846

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,49 @@ class TestBCode extends DottyBytecodeTest {
401401
}
402402
}
403403

404+
@Test def aaaa = {
405+
val source =
406+
"""class Test {
407+
| def test(a: Any): Int = {
408+
| a match {
409+
| case _: Test => 1
410+
| }
411+
| }
412+
|}
413+
""".stripMargin
414+
415+
checkBCode(source) { dir =>
416+
val moduleIn = dir.lookupName("Test.class", directory = false)
417+
val moduleNode = loadClassNode(moduleIn.input)
418+
val method = getMethod(moduleNode, "test")
419+
420+
val instructions = instructionsFromMethod(method)
421+
val expected = List(
422+
VarOp(Opcodes.ALOAD, 1),
423+
VarOp(Opcodes.ASTORE, 2),
424+
VarOp(Opcodes.ALOAD, 2),
425+
TypeOp(Opcodes.INSTANCEOF, "Test"),
426+
Jump(Opcodes.IFEQ, Label(10)),
427+
VarOp(Opcodes.ALOAD, 2),
428+
TypeOp(Opcodes.CHECKCAST, "Test"),
429+
VarOp(Opcodes.ASTORE, 3),
430+
Op(Opcodes.ICONST_1),
431+
Jump(Opcodes.GOTO, Label(17)),
432+
Label(10),
433+
FrameEntry(1, List("java/lang/Object"), List()),
434+
TypeOp(Opcodes.NEW, "scala/MatchError"),
435+
Op(Opcodes.DUP),
436+
VarOp(Opcodes.ALOAD, 2),
437+
Invoke(Opcodes.INVOKESPECIAL, "scala/MatchError", "<init>", "(Ljava/lang/Object;)V", false),
438+
Op(Opcodes.ATHROW),
439+
Label(17),
440+
FrameEntry(0, List("Test", "java/lang/Object", "java/lang/Object", "Test"), List(1)),
441+
Op(Opcodes.IRETURN)
442+
)
443+
assert(instructions == expected,
444+
"`test` was not properly generated\n" + diffInstructions(instructions, expected))
445+
446+
}
447+
}
448+
404449
}

0 commit comments

Comments
 (0)