diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 0d9ab774cd0..47dc409c800 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -3744,9 +3744,10 @@ The "returned" value of try expression with no finally is either the last expres value.put(boxType(value.type), v); if (value.type == Type.VOID_TYPE) { - v.aconst(null); + StackValue.putUnitInstance(v); } - else if (opToken != JetTokens.AS_SAFE) { + + if (opToken != JetTokens.AS_SAFE) { if (!TypeUtils.isNullableType(rightType)) { v.dup(); Label nonnull = new Label(); diff --git a/compiler/testData/codegen/box/casts/asAny.kt b/compiler/testData/codegen/box/casts/asAny.kt deleted file mode 100644 index 5c277379d48..00000000000 --- a/compiler/testData/codegen/box/casts/asAny.kt +++ /dev/null @@ -1,7 +0,0 @@ -fun println(s: String) { -} - -fun box(): String { - println(":Hi!") as Any - return "OK" -} diff --git a/compiler/testData/codegen/box/casts/unitAsAny.kt b/compiler/testData/codegen/box/casts/unitAsAny.kt new file mode 100644 index 00000000000..74326356cba --- /dev/null +++ b/compiler/testData/codegen/box/casts/unitAsAny.kt @@ -0,0 +1,8 @@ +fun println(s: String) { +} + +fun box(): String { + val x = println(":Hi!") as Any + if (x != Unit) return "Fail: $x" + return "OK" +} diff --git a/compiler/testData/codegen/box/casts/unitAsSafeAny.kt b/compiler/testData/codegen/box/casts/unitAsSafeAny.kt new file mode 100644 index 00000000000..81d5a495425 --- /dev/null +++ b/compiler/testData/codegen/box/casts/unitAsSafeAny.kt @@ -0,0 +1,8 @@ +fun println(s: String) { +} + +fun box(): String { + val x = println(":Hi!") as? Any + if (x != Unit) return "Fail: $x" + return "OK" +} diff --git a/compiler/testData/codegen/boxWithStdlib/casts/unitAsInt.kt b/compiler/testData/codegen/boxWithStdlib/casts/unitAsInt.kt new file mode 100644 index 00000000000..39713cf804f --- /dev/null +++ b/compiler/testData/codegen/boxWithStdlib/casts/unitAsInt.kt @@ -0,0 +1,15 @@ +fun foo() {} + +fun box(): String { + try { + foo() as Int? + } + catch (e: ClassCastException) { + return "OK" + } + catch (e: Throwable) { + return "Fail: ClassCastException should have been thrown, but was instead ${e.javaClass.getName()}: ${e.getMessage()}" + } + + return "Fail: no exception was thrown" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java index 908f62af7a2..f1b138005b2 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java @@ -829,12 +829,6 @@ 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"); @@ -889,6 +883,18 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest(fileName); } + @TestMetadata("unitAsAny.kt") + public void testUnitAsAny() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/unitAsAny.kt"); + doTest(fileName); + } + + @TestMetadata("unitAsSafeAny.kt") + public void testUnitAsSafeAny() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/unitAsSafeAny.kt"); + doTest(fileName); + } + @TestMetadata("unitNullableCast.kt") public void testUnitNullableCast() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/unitNullableCast.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java index 675239c3466..e228260576b 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxWithStdlibCodegenTestGenerated.java @@ -888,6 +888,12 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/casts/asWithGeneric.kt"); doTestWithStdlib(fileName); } + + @TestMetadata("unitAsInt.kt") + public void testUnitAsInt() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/casts/unitAsInt.kt"); + doTestWithStdlib(fileName); + } } @TestMetadata("compiler/testData/codegen/boxWithStdlib/classes")