From 48b0afd84d75836854165d8f325acf2e47236c4e Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Wed, 4 Mar 2015 19:28:02 +0100 Subject: [PATCH] correctly generate safe casts from Unit type #KT-4494 fixed --- .../org/jetbrains/kotlin/codegen/ExpressionCodegen.java | 3 +++ compiler/testData/codegen/box/casts/unitNullableCast.kt | 7 +++++++ .../codegen/generated/BlackBoxCodegenTestGenerated.java | 6 ++++++ 3 files changed, 16 insertions(+) create mode 100644 compiler/testData/codegen/box/casts/unitNullableCast.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index ac0fb882654..fcc4f00fda8 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -3840,6 +3840,9 @@ 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/unitNullableCast.kt b/compiler/testData/codegen/box/casts/unitNullableCast.kt new file mode 100644 index 00000000000..c0f644e88a5 --- /dev/null +++ b/compiler/testData/codegen/box/casts/unitNullableCast.kt @@ -0,0 +1,7 @@ +fun foo() {} + +fun bar(): Int? = foo() as? Int + +fun box(): String { + return if (bar() == null) "OK" else "fail" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java index 164a70f4dbe..08b3d375407 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/generated/BlackBoxCodegenTestGenerated.java @@ -930,6 +930,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/notIs.kt"); doTest(fileName); } + + @TestMetadata("unitNullableCast.kt") + public void testUnitNullableCast() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/box/casts/unitNullableCast.kt"); + doTest(fileName); + } } @TestMetadata("compiler/testData/codegen/box/classes")