From 00da5fe1fbdf2eb94b391f4a1e6f9c17d92d1b84 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 10 Dec 2013 21:07:05 +0400 Subject: [PATCH] Fix VerifyError for 'when' of type 'Unit?' --- .../src/org/jetbrains/jet/codegen/StackValue.java | 10 +++++----- .../testData/codegen/box/unit/nullableUnitInWhen1.kt | 12 ++++++++++++ .../testData/codegen/box/unit/nullableUnitInWhen2.kt | 12 ++++++++++++ .../generated/BlackBoxCodegenTestGenerated.java | 10 ++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 compiler/testData/codegen/box/unit/nullableUnitInWhen1.kt create mode 100644 compiler/testData/codegen/box/unit/nullableUnitInWhen2.kt diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/StackValue.java b/compiler/backend/src/org/jetbrains/jet/codegen/StackValue.java index c69abf2286f..4a4cb7af35f 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/StackValue.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/StackValue.java @@ -234,8 +234,10 @@ public abstract class StackValue { } } else if (toType.equals(JET_UNIT_TYPE)) { - pop(v, fromType); - putUnitInstance(v); + if (!fromType.equals(getType(Void.class))) { + pop(v, fromType); + putUnitInstance(v); + } } else if (toType.getSort() == Type.ARRAY) { v.checkcast(toType); @@ -431,9 +433,7 @@ public abstract class StackValue { v.aconst(value); } - if (value != null) { - coerceTo(type, v); - } + coerceTo(type, v); } @Override diff --git a/compiler/testData/codegen/box/unit/nullableUnitInWhen1.kt b/compiler/testData/codegen/box/unit/nullableUnitInWhen1.kt new file mode 100644 index 00000000000..2d6768af748 --- /dev/null +++ b/compiler/testData/codegen/box/unit/nullableUnitInWhen1.kt @@ -0,0 +1,12 @@ +fun foo() {} + +fun box(): String { + when ("A") { + "B" -> foo() + else -> null + } + + foo() + + return "OK" +} diff --git a/compiler/testData/codegen/box/unit/nullableUnitInWhen2.kt b/compiler/testData/codegen/box/unit/nullableUnitInWhen2.kt new file mode 100644 index 00000000000..47807e50f19 --- /dev/null +++ b/compiler/testData/codegen/box/unit/nullableUnitInWhen2.kt @@ -0,0 +1,12 @@ +fun foo() {} + +fun box(): String { + when ("A") { + "B" -> null + else -> foo() + } + + foo() + + return "OK" +} diff --git a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java index f401e51c6d3..83ee8ffdf79 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java @@ -4812,6 +4812,16 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { doTest("compiler/testData/codegen/box/unit/nullableUnit.kt"); } + @TestMetadata("nullableUnitInWhen1.kt") + public void testNullableUnitInWhen1() throws Exception { + doTest("compiler/testData/codegen/box/unit/nullableUnitInWhen1.kt"); + } + + @TestMetadata("nullableUnitInWhen2.kt") + public void testNullableUnitInWhen2() throws Exception { + doTest("compiler/testData/codegen/box/unit/nullableUnitInWhen2.kt"); + } + @TestMetadata("unitClassObject.kt") public void testUnitClassObject() throws Exception { doTest("compiler/testData/codegen/box/unit/unitClassObject.kt");