Fixed codegen for single-branch if

#KT-2597 Fixed
 #KT-2598 Fixed
This commit is contained in:
Alexander Udalov
2012-08-10 14:38:21 +04:00
parent 4bc1b2636a
commit 9350369fab
5 changed files with 49 additions and 4 deletions
@@ -687,14 +687,26 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
}
private StackValue generateSingleBranchIf(StackValue condition, JetExpression expression, boolean inverse) {
Type expressionType = expressionType(expression);
Type targetType = expressionType;
if (!expressionType.equals(TUPLE0_TYPE)) {
targetType = TYPE_OBJECT;
}
Label elseLabel = new Label();
condition.condJump(elseLabel, inverse, v);
gen(expression, expressionType);
StackValue.coerce(expressionType, targetType, v);
Label end = new Label();
v.goTo(end);
condition.condJump(end, inverse, v);
gen(expression, Type.VOID_TYPE);
v.mark(elseLabel);
StackValue.putTuple0Instance(v);
v.mark(end);
return StackValue.none();
return StackValue.onStack(targetType);
}
@Override
@@ -242,6 +242,10 @@ public abstract class StackValue {
else
v.iconst(0);
}
else if (toType.equals(JetTypeMapper.TUPLE0_TYPE) && !fromType.equals(JetTypeMapper.TUPLE0_TYPE)) {
pop(fromType, v);
putTuple0Instance(v);
}
else if (toType.getSort() == Type.OBJECT && fromType.equals(JetTypeMapper.TYPE_OBJECT) || toType.getSort() == Type.ARRAY) {
v.checkcast(toType);
}
@@ -0,0 +1,10 @@
fun box(): String {
var i = 0
{
if (1 == 1) {
i++
} else {
}
}()
return "OK"
}
@@ -0,0 +1,9 @@
fun foo(condition: Boolean): String {
val u = if (condition) {
"OK"
} else {
}
return u.toString()
}
fun box() = foo(true)
@@ -391,4 +391,14 @@ public class ControlStructuresTest extends CodegenTestCase {
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
blackBoxFile("controlStructures/tryCatchFinallyChain.kt");
}
public void testKt2597() {
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
blackBoxFile("regressions/kt2597.kt");
}
public void testKt2598() {
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
blackBoxFile("regressions/kt2598.kt");
}
}