diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 2aecc633e25..fb51d62e79e 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -3918,7 +3918,10 @@ The "returned" value of try expression with no finally is either the last expres public Unit invoke(InstructionAdapter v) { value.put(boxType(value.type), v); - if (opToken != JetTokens.AS_SAFE) { + if (value.type == Type.VOID_TYPE) { + v.aconst(null); + } + else if (opToken != JetTokens.AS_SAFE) { if (!TypeUtils.isNullableType(rightType)) { v.dup(); Label nonnull = new Label(); @@ -3931,9 +3934,6 @@ The "returned" value of try expression with no finally is either the last expres v.mark(nonnull); } } - else if (value.type == Type.VOID_TYPE) { - v.aconst(null); - } else { v.dup(); generateInstanceOfInstruction(rightType); diff --git a/compiler/testData/codegen/box/casts/asAny.kt b/compiler/testData/codegen/box/casts/asAny.kt new file mode 100644 index 00000000000..5c277379d48 --- /dev/null +++ b/compiler/testData/codegen/box/casts/asAny.kt @@ -0,0 +1,7 @@ +fun println(s: String) { +} + +fun box(): String { + println(":Hi!") as Any + return "OK" +} diff --git a/compiler/testData/codegen/box/casts/asUnit.kt b/compiler/testData/codegen/box/casts/asUnit.kt new file mode 100644 index 00000000000..8c11703906c --- /dev/null +++ b/compiler/testData/codegen/box/casts/asUnit.kt @@ -0,0 +1 @@ +fun box() = if (4 as? Unit != null) "Fail" else "OK" \ No newline at end of file diff --git a/compiler/testData/codegen/box/casts/lambdaToUnitCast.kt b/compiler/testData/codegen/box/casts/lambdaToUnitCast.kt new file mode 100644 index 00000000000..94ddfb05c00 --- /dev/null +++ b/compiler/testData/codegen/box/casts/lambdaToUnitCast.kt @@ -0,0 +1,6 @@ +val foo: () -> Unit = {} + +fun box(): String { + foo() as Unit + return "OK" +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java index a6be7a9c335..34c47ad05df 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java @@ -890,6 +890,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("asAny.kt") + public void testAsAny() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/asAny.kt"); + doTest(fileName); + } + @TestMetadata("asForConstants.kt") public void testAsForConstants() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/asForConstants.kt"); @@ -914,6 +920,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("asUnit.kt") + public void testAsUnit() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/asUnit.kt"); + doTest(fileName); + } + @TestMetadata("castGenericNull.kt") public void testCastGenericNull() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/castGenericNull.kt"); @@ -926,6 +938,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("lambdaToUnitCast.kt") + public void testLambdaToUnitCast() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/lambdaToUnitCast.kt"); + doTest(fileName); + } + @TestMetadata("notIs.kt") public void testNotIs() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/notIs.kt");