return value of try/catch/finally changed according to frontend changes

#KT-910 Fixed
This commit is contained in:
Dmitry Jemerov
2012-06-08 18:36:14 +02:00
parent 8baae95531
commit f945ec6a27
4 changed files with 34 additions and 10 deletions
@@ -2672,8 +2672,6 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
/*
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
@@ -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"
@@ -0,0 +1,24 @@
import java.util.Set
import java.util.HashSet
fun foo() : Int =
try {
2
}
finally {
"s"
}
fun bar(set : Set<Int>) : Set<Int> =
try {
set
}
finally {
set.add(42)
}
fun box() : String {
if (foo() != 2) return "fail 1"
val s = bar(HashSet<Int>())
return if (s.contains(42)) "OK" else "fail 2"
}
@@ -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");
}
}