diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index a158b7ced1d..34d491c15f5 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -1528,6 +1528,15 @@ public class ExpressionCodegen extends JetVisitor implem } } + private boolean hasFinallyBLocks() { + for (BlockStackElement element : blockStackElements) { + if (element instanceof FinallyBlockStackElement) { + return true; + } + } + return false; + } + private void genFinallyBlockOrGoto( @Nullable FinallyBlockStackElement finallyBlockStackElement, @Nullable Label tryCatchBlockEnd @@ -1566,7 +1575,14 @@ public class ExpressionCodegen extends JetVisitor implem JetExpression returnedExpression = expression.getReturnedExpression(); if (returnedExpression != null) { gen(returnedExpression, returnType); - doFinallyOnReturn(); + boolean hasFinallyBLocks = hasFinallyBLocks(); + if (hasFinallyBLocks) { + int returnValIndex = myFrameMap.enterTemp(returnType); + StackValue.local(returnValIndex, returnType).store(returnType, v); + doFinallyOnReturn(); + StackValue.local(returnValIndex, returnType).put(returnType, v); + myFrameMap.leaveTemp(returnType); + } v.areturn(returnType); } else { diff --git a/compiler/testData/codegen/box/finally/kt3706.kt b/compiler/testData/codegen/box/finally/kt3706.kt new file mode 100644 index 00000000000..923e3aa045a --- /dev/null +++ b/compiler/testData/codegen/box/finally/kt3706.kt @@ -0,0 +1,16 @@ +fun f(): Int { + try { + return 0 + } + finally { + try { // culprit ?? remove this try-catch and it works. + } catch (ignore: Exception) { + } + } +} + +fun box(): String { + if (f() != 0) return "fail1" + + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/box/finally/kt4134.kt b/compiler/testData/codegen/box/finally/kt4134.kt new file mode 100644 index 00000000000..24e4750d089 --- /dev/null +++ b/compiler/testData/codegen/box/finally/kt4134.kt @@ -0,0 +1,15 @@ +fun io(s: R, a: (R) -> T): T { + try { + return a(s) + } finally { + try { + s.toString() + } catch(e: Exception) { + //NOP + } + } +} + +fun box() : String { + return io(("OK"), {it}) +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java index 79c112ba1b6..6ded8f412dc 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java @@ -2544,6 +2544,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest("compiler/testData/codegen/box/finally/kt3549.kt"); } + @TestMetadata("kt3706.kt") + public void testKt3706() throws Exception { + doTest("compiler/testData/codegen/box/finally/kt3706.kt"); + } + @TestMetadata("kt3867.kt") public void testKt3867() throws Exception { doTest("compiler/testData/codegen/box/finally/kt3867.kt"); @@ -2559,6 +2564,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest("compiler/testData/codegen/box/finally/kt3894.kt"); } + @TestMetadata("kt4134.kt") + public void testKt4134() throws Exception { + doTest("compiler/testData/codegen/box/finally/kt4134.kt"); + } + @TestMetadata("loopAndFinally.kt") public void testLoopAndFinally() throws Exception { doTest("compiler/testData/codegen/box/finally/loopAndFinally.kt");