diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InternalFinallyBlockInliner.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InternalFinallyBlockInliner.java index 75e48d3b981..f57d10f40e9 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InternalFinallyBlockInliner.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InternalFinallyBlockInliner.java @@ -143,7 +143,7 @@ public class InternalFinallyBlockInliner extends CoveringTryCatchNodeProcessor { AbstractInsnNode markedReturn = curIns; AbstractInsnNode instrInsertFinallyBefore = markedReturn.getPrevious(); - AbstractInsnNode nextPrev = instrInsertFinallyBefore.getPrevious().getPrevious(); + AbstractInsnNode nextPrev = instrInsertFinallyBefore.getPrevious(); assert markedReturn.getNext() instanceof LabelNode : "Label should be occurred after non-local return"; LabelNode newFinallyEnd = (LabelNode) markedReturn.getNext(); Type nonLocalReturnType = InlineCodegenUtil.getReturnType(markedReturn.getOpcode()); @@ -166,6 +166,8 @@ public class InternalFinallyBlockInliner extends CoveringTryCatchNodeProcessor { List clusterBlocks = clusterToFindFinally.getBlocks(); TryCatchBlockNodeInfo nodeWithDefaultHandlerIfExists = clusterBlocks.get(clusterBlocks.size() - 1); + FinallyBlockInfo finallyInfo = findFinallyBlockBody(nodeWithDefaultHandlerIfExists, getTryBlocksMetaInfo().getAllIntervals()); + if (finallyInfo == null) continue; if (nodeWithDefaultHandlerIfExists.getOnlyCopyNotProcess()) { //lambdas finally generated before non-local return instruction, @@ -173,9 +175,6 @@ public class InternalFinallyBlockInliner extends CoveringTryCatchNodeProcessor { throw new RuntimeException("Lambda try blocks should be skipped"); } - FinallyBlockInfo finallyInfo = findFinallyBlockBody(nodeWithDefaultHandlerIfExists, getTryBlocksMetaInfo().getAllIntervals()); - if (finallyInfo == null) continue; - originalDepthIndex++; instructions.resetLabels(); diff --git a/compiler/testData/codegen/boxInline/nonLocalReturns/kt8948.1.kt b/compiler/testData/codegen/boxInline/nonLocalReturns/kt8948.1.kt new file mode 100644 index 00000000000..23474b1dd63 --- /dev/null +++ b/compiler/testData/codegen/boxInline/nonLocalReturns/kt8948.1.kt @@ -0,0 +1,13 @@ +import test.* + +fun box(): String { + foo { + try { + return "OK" + } catch(e: Exception) { + return "fail 1" + } + } + + return "fail 2" +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxInline/nonLocalReturns/kt8948.2.kt b/compiler/testData/codegen/boxInline/nonLocalReturns/kt8948.2.kt new file mode 100644 index 00000000000..9c48b772152 --- /dev/null +++ b/compiler/testData/codegen/boxInline/nonLocalReturns/kt8948.2.kt @@ -0,0 +1,9 @@ +package test + +inline fun foo(f: () -> Unit) { + try { + f() + } + finally { + } +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxInline/nonLocalReturns/kt8948v2.1.kt b/compiler/testData/codegen/boxInline/nonLocalReturns/kt8948v2.1.kt new file mode 100644 index 00000000000..fe525e1341d --- /dev/null +++ b/compiler/testData/codegen/boxInline/nonLocalReturns/kt8948v2.1.kt @@ -0,0 +1,21 @@ +import test.* + +var p = "fail" + +fun test() { + foo { + try { + p = "O" + return + } catch(e: Exception) { + return + } finally { + p += "K" + } + } +} + +fun box(): String { + test() + return p +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxInline/nonLocalReturns/kt8948v2.2.kt b/compiler/testData/codegen/boxInline/nonLocalReturns/kt8948v2.2.kt new file mode 100644 index 00000000000..c776f4ee115 --- /dev/null +++ b/compiler/testData/codegen/boxInline/nonLocalReturns/kt8948v2.2.kt @@ -0,0 +1,10 @@ +package test + +inline fun foo(f: () -> Unit) { + try { + f() + } + finally { + 1 + } +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java index c9b132e29d0..ff9992cc3aa 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxInlineCodegenTestGenerated.java @@ -481,6 +481,18 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo doTestMultiFileWithInlineCheck(fileName); } + @TestMetadata("kt8948.1.kt") + public void testKt8948() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/nonLocalReturns/kt8948.1.kt"); + doTestMultiFileWithInlineCheck(fileName); + } + + @TestMetadata("kt8948v2.1.kt") + public void testKt8948v2() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/nonLocalReturns/kt8948v2.1.kt"); + doTestMultiFileWithInlineCheck(fileName); + } + @TestMetadata("nestedNonLocals.1.kt") public void testNestedNonLocals() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/nonLocalReturns/nestedNonLocals.1.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java index 9b55099e8f8..ca11728bd63 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -481,6 +481,18 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi doBoxTestWithInlineCheck(fileName); } + @TestMetadata("kt8948.1.kt") + public void testKt8948() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/nonLocalReturns/kt8948.1.kt"); + doBoxTestWithInlineCheck(fileName); + } + + @TestMetadata("kt8948v2.1.kt") + public void testKt8948v2() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/nonLocalReturns/kt8948v2.1.kt"); + doBoxTestWithInlineCheck(fileName); + } + @TestMetadata("nestedNonLocals.1.kt") public void testNestedNonLocals() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxInline/nonLocalReturns/nestedNonLocals.1.kt");