Fix codegen for closures returning 'Unit?'

Only do a checkcast when we need to coerce Object to Unit: the code was
hopefully type-checked so that it'll only be necessary when the value is either
Unit.VALUE or null
This commit is contained in:
Alexander Udalov
2013-12-11 17:56:10 +04:00
parent 9e5c68a8f9
commit 0576851102
4 changed files with 34 additions and 1 deletions
@@ -242,7 +242,10 @@ public abstract class StackValue {
}
}
else if (toType.equals(JET_UNIT_TYPE)) {
if (!fromType.equals(getType(Void.class))) {
if (fromType.equals(getType(Object.class))) {
v.checkcast(JET_UNIT_TYPE);
}
else if (!fromType.equals(getType(Void.class))) {
pop(v, fromType);
putUnitInstance(v);
}
@@ -0,0 +1,8 @@
fun isNull(x: Unit?) = x == null
fun box(): String {
val closure: () -> Unit? = { null }
if (!isNull(closure())) return "Fail 1"
return "OK"
}
@@ -0,0 +1,12 @@
fun foo() {}
fun box(): String {
val x = when ("A") {
"B" -> foo()
else -> null
}
foo()
return if (x == null) "OK" else "Fail"
}
@@ -4868,6 +4868,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("closureReturnsNullableUnit.kt")
public void testClosureReturnsNullableUnit() throws Exception {
doTest("compiler/testData/codegen/box/unit/closureReturnsNullableUnit.kt");
}
@TestMetadata("kt3634.kt")
public void testKt3634() throws Exception {
doTest("compiler/testData/codegen/box/unit/kt3634.kt");
@@ -4898,6 +4903,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
doTest("compiler/testData/codegen/box/unit/nullableUnitInWhen2.kt");
}
@TestMetadata("nullableUnitInWhen3.kt")
public void testNullableUnitInWhen3() throws Exception {
doTest("compiler/testData/codegen/box/unit/nullableUnitInWhen3.kt");
}
@TestMetadata("unitClassObject.kt")
public void testUnitClassObject() throws Exception {
doTest("compiler/testData/codegen/box/unit/unitClassObject.kt");