Return Unit.VALUE if Unit or Any is expected

StackValue.coerce will do its job in casting void to Unit correctly

 #KT-4212 Fixed
This commit is contained in:
Alexander Udalov
2013-12-04 19:41:02 +04:00
parent f19ede0637
commit 6276dd1483
3 changed files with 26 additions and 4 deletions
@@ -1607,13 +1607,10 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
public void returnExpression(JetExpression expr) {
StackValue lastValue = gen(expr);
if (lastValue.type != Type.VOID_TYPE) {
if (!endsWithReturn(expr)) {
lastValue.put(returnType, v);
v.areturn(returnType);
}
else if (!endsWithReturn(expr)) {
v.areturn(returnType);
}
}
private static boolean endsWithReturn(JetElement bodyExpression) {
@@ -0,0 +1,20 @@
fun foo(): Any? = bar()
fun bar() {}
fun baz(): Any? {
return bar()
}
fun quux(): Unit? = bar()
fun box(): String {
foo()
if (foo() != Unit.VALUE) return "Fail 1"
if (foo() != bar()) return "Fail 2"
if (bar() != baz()) return "Fail 3"
if (baz() != quux()) return "Fail 4"
return "OK"
}
@@ -4764,6 +4764,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/codegen/box/unit"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("kt4212.kt")
public void testKt4212() throws Exception {
doTest("compiler/testData/codegen/box/unit/kt4212.kt");
}
@TestMetadata("kt4265.kt")
public void testKt4265() throws Exception {
doTest("compiler/testData/codegen/box/unit/kt4265.kt");