From 8564c20baa88b3941e957bf1c805ed158de24480 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 22 Nov 2012 15:37:39 +0400 Subject: [PATCH] Fix condition generation of empty if --- .../jetbrains/jet/codegen/ExpressionCodegen.java | 6 ++---- .../codegen/controlStructures/conditionOfEmptyIf.kt | 13 +++++++++++++ .../jet/codegen/ControlStructuresTest.java | 6 +++++- 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 compiler/testData/codegen/controlStructures/conditionOfEmptyIf.kt diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index 79bf6bbbe03..19eda466eb5 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -363,6 +363,7 @@ public class ExpressionCodegen extends JetVisitor implem /* package */ StackValue generateIfExpression(JetIfExpression expression, boolean isStatement) { Type asmType = expressionType(expression); + StackValue condition = gen(expression.getCondition()); JetExpression thenExpression = expression.getThen(); JetExpression elseExpression = expression.getElse(); @@ -376,22 +377,19 @@ public class ExpressionCodegen extends JetVisitor implem if (!asmType.equals(JET_TUPLE0_TYPE)) { throw new CompilationException("Completely empty 'if' is expected to have Unit type", null, expression); } - StackValue.putTuple0Instance(v); + condition.put(asmType, v); return StackValue.onStack(asmType); } - StackValue condition = gen(expression.getCondition()); return generateSingleBranchIf(condition, expression, elseExpression, false, isStatement); } else { if (isEmptyExpression(elseExpression)) { - StackValue condition = gen(expression.getCondition()); return generateSingleBranchIf(condition, expression, thenExpression, true, isStatement); } } Label elseLabel = new Label(); - StackValue condition = gen(expression.getCondition()); condition.condJump(elseLabel, true, v); // == 0, i.e. false Label end = new Label(); diff --git a/compiler/testData/codegen/controlStructures/conditionOfEmptyIf.kt b/compiler/testData/codegen/controlStructures/conditionOfEmptyIf.kt new file mode 100644 index 00000000000..153b73525c7 --- /dev/null +++ b/compiler/testData/codegen/controlStructures/conditionOfEmptyIf.kt @@ -0,0 +1,13 @@ +var result = "Fail" + +fun setOK(): Boolean { + result = "OK" + return true +} + +fun box(): String { + if (setOK()) { + } else { + } + return result +} diff --git a/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java b/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java index c8e0221c453..e7c69161308 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java @@ -463,6 +463,10 @@ public class ControlStructuresTest extends CodegenTestCase { public void testForInSmartCastedToArray() { createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY); blackBoxFile("controlStructures/forInSmartCastedToArray.kt"); - //System.out.println(generateToText()); + } + + public void testConditionOfEmptyIf() { + createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY); + blackBoxFile("controlStructures/conditionOfEmptyIf.kt"); } }