From f945ec6a27f9ad1d553793acb177a56349c1cfbe Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Fri, 8 Jun 2012 18:36:14 +0200 Subject: [PATCH] return value of try/catch/finally changed according to frontend changes #KT-910 Fixed --- .../jet/codegen/ExpressionCodegen.java | 13 ++++------ .../testData/codegen/regressions/kt496.jet | 2 +- .../testData/codegen/regressions/kt910.kt | 24 +++++++++++++++++++ .../jet/codegen/ControlStructuresTest.java | 5 ++++ 4 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 compiler/testData/codegen/regressions/kt910.kt diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index 5218d25a1fa..e136eca297e 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -2672,8 +2672,6 @@ public class ExpressionCodegen extends JetVisitor { /* The "returned" value of try expression with no finally is either the last expression in the try block or the last expression in the catch block (or blocks). - -If finally block is present, its last expression is the value of try expression. */ Label savedContinueLabel = continueLabel; continueLabel = null; @@ -2689,14 +2687,11 @@ If finally block is present, its last expression is the value of try expression. Label tryStart = new Label(); v.mark(tryStart); v.nop(); // prevent verify error on empty try - if(finallyBlock == null) - gen(expression.getTryBlock(), expectedAsmType); - else - gen(expression.getTryBlock(), Type.VOID_TYPE); + gen(expression.getTryBlock(), expectedAsmType); Label tryEnd = new Label(); v.mark(tryEnd); if (finallyBlock != null) { - gen(finallyBlock.getFinalExpression(), expectedAsmType); + gen(finallyBlock.getFinalExpression(), Type.VOID_TYPE); } Label end = new Label(); v.goTo(end); // TODO don't generate goto if there's no code following try/catch @@ -2711,12 +2706,12 @@ If finally block is present, its last expression is the value of try expression. int index = lookupLocal(descriptor); v.store(index, descriptorType); - gen(clause.getCatchBody(), finallyBlock != null ? Type.VOID_TYPE : expectedAsmType); + gen(clause.getCatchBody(), expectedAsmType); myFrameMap.leave(descriptor); if (finallyBlock != null) { - gen(finallyBlock.getFinalExpression(), expectedAsmType); + gen(finallyBlock.getFinalExpression(), Type.VOID_TYPE); } v.goTo(end); // TODO don't generate goto if there's no code following try/catch diff --git a/compiler/testData/codegen/regressions/kt496.jet b/compiler/testData/codegen/regressions/kt496.jet index 79754d51129..b481604def0 100644 --- a/compiler/testData/codegen/regressions/kt496.jet +++ b/compiler/testData/codegen/regressions/kt496.jet @@ -80,7 +80,7 @@ fun box() : String { if(test2()) return "test2 failed" if(test3() != 2) return "test3 failed" System.out?.println(test4()) - if(test4() != 3) return "test4 failed" + if(test4() != 0) return "test4 failed" if(test5() != 11) return "test5 failed" if(test6() != 10) return "test6 failed" diff --git a/compiler/testData/codegen/regressions/kt910.kt b/compiler/testData/codegen/regressions/kt910.kt new file mode 100644 index 00000000000..c9b8f23d511 --- /dev/null +++ b/compiler/testData/codegen/regressions/kt910.kt @@ -0,0 +1,24 @@ +import java.util.Set +import java.util.HashSet + +fun foo() : Int = + try { + 2 + } + finally { + "s" + } + +fun bar(set : Set) : Set = + try { + set + } + finally { + set.add(42) + } + +fun box() : String { + if (foo() != 2) return "fail 1" + val s = bar(HashSet()) + return if (s.contains(42)) "OK" else "fail 2" +} diff --git a/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java b/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java index 6a44c189f96..f00acbd182f 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java @@ -351,4 +351,9 @@ public class ControlStructuresTest extends CodegenTestCase { createEnvironmentWithMockJdkAndIdeaAnnotations(CompilerSpecialMode.JDK_HEADERS); blackBoxFile("regressions/kt2062.kt"); } + + public void testKt910() { + createEnvironmentWithMockJdkAndIdeaAnnotations(CompilerSpecialMode.JDK_HEADERS); + blackBoxFile("regressions/kt910.kt"); + } }