From 43885f0955c5e0cc0079d6125f25da7b76b3b029 Mon Sep 17 00:00:00 2001 From: Alex Tkachman Date: Wed, 7 Dec 2011 20:34:48 +0200 Subject: [PATCH] KT-769, KT-773 wrong compilation of when --- .../jet/codegen/ExpressionCodegen.java | 22 ++++++++++------ .../testData/codegen/regressions/kt769.jet | 11 ++++++++ .../testData/codegen/regressions/kt773.jet | 25 +++++++++++++++++++ .../jet/codegen/ControlStructuresTest.java | 10 ++++++++ 4 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 compiler/testData/codegen/regressions/kt769.jet create mode 100644 compiler/testData/codegen/regressions/kt773.jet diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index 9f29c28ce15..28e82ad993a 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -2562,13 +2562,21 @@ If finally block is present, its last expression is the value of try expression. public StackValue visitWhenExpression(JetWhenExpression expression, StackValue receiver) { JetExpression expr = expression.getSubjectExpression(); final Type subjectType = expressionType(expr); + final Type resultType = expressionType(expression); final int subjectLocal = myFrameMap.enterTemp(subjectType.getSize()); gen(expr, subjectType); v.store(subjectLocal, subjectType); Label end = new Label(); - Label nextCondition = null; boolean hasElse = false; + for (JetWhenEntry whenEntry : expression.getEntries()) { + if(whenEntry.isElse()) { + hasElse = true; + break; + } + } + + Label nextCondition = null; for (JetWhenEntry whenEntry : expression.getEntries()) { if (nextCondition != null) { v.mark(nextCondition); @@ -2588,13 +2596,13 @@ If finally block is present, its last expression is the value of try expression. } } } - else { - hasElse = true; - } + v.visitLabel(thisEntry); - genToJVMStack(whenEntry.getExpression()); + gen(whenEntry.getExpression(), resultType); mark.dropTo(); - v.goTo(end); + if (!whenEntry.isElse()) { + v.goTo(end); + } } if (!hasElse && nextCondition != null) { v.mark(nextCondition); @@ -2603,7 +2611,7 @@ If finally block is present, its last expression is the value of try expression. v.mark(end); myFrameMap.leaveTemp(subjectType.getSize()); - return StackValue.onStack(expressionType(expression)); + return StackValue.onStack(resultType); } private StackValue generateWhenCondition(Type subjectType, int subjectLocal, JetWhenCondition condition, @Nullable Label nextEntry) { diff --git a/compiler/testData/codegen/regressions/kt769.jet b/compiler/testData/codegen/regressions/kt769.jet new file mode 100644 index 00000000000..266af79d867 --- /dev/null +++ b/compiler/testData/codegen/regressions/kt769.jet @@ -0,0 +1,11 @@ +namespace w_range + +fun box() : String { + var i = 0 + when (i) { + 1 => i-- + else => { i = 2 } + } + System.out?.println(i) + return "OK" +} \ No newline at end of file diff --git a/compiler/testData/codegen/regressions/kt773.jet b/compiler/testData/codegen/regressions/kt773.jet new file mode 100644 index 00000000000..6cad8d6ae03 --- /dev/null +++ b/compiler/testData/codegen/regressions/kt773.jet @@ -0,0 +1,25 @@ +namespace demo2 + +fun print(o : Any?) {} + +fun test(i : Int) { + var monthString : String? = "" + when (i) { + 1 => { + print(1) + print(2) + print(3) + print(4) + print(5) + } + else => { + monthString = "Invalid month" + } + } + print(monthString) +} + +fun box() : String { + for (i in 1..12) test(i) + return "OK" +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java b/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java index a1bf84af9d6..b6f0f1b3ad5 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java @@ -221,6 +221,16 @@ public class ControlStructuresTest extends CodegenTestCase { blackBoxFile("regressions/kt434.jet"); } + public void testKt769() throws Exception { + blackBoxFile("regressions/kt769.jet"); +// System.out.println(generateToText()); + } + + public void testKt773() throws Exception { + blackBoxFile("regressions/kt773.jet"); +// System.out.println(generateToText()); + } + public void testQuicksort() throws Exception { blackBoxFile("controlStructures/quicksort.jet"); // System.out.println(generateToText());