KT-769, KT-773 wrong compilation of when
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
namespace demo2
|
||||
|
||||
fun print(o : Any?) {}
|
||||
|
||||
fun test(i : Int) {
|
||||
var monthString : String? = "<empty>"
|
||||
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"
|
||||
}
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user