From 6276dd148351a052191968b18009ed9bbfa8707f Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 4 Dec 2013 19:41:02 +0400 Subject: [PATCH] Return Unit.VALUE if Unit or Any is expected StackValue.coerce will do its job in casting void to Unit correctly #KT-4212 Fixed --- .../jet/codegen/ExpressionCodegen.java | 5 +---- compiler/testData/codegen/box/unit/kt4212.kt | 20 +++++++++++++++++++ .../BlackBoxCodegenTestGenerated.java | 5 +++++ 3 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/codegen/box/unit/kt4212.kt diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index f7649be6416..dc9a9a31f93 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -1607,13 +1607,10 @@ public class ExpressionCodegen extends JetVisitor implem public void returnExpression(JetExpression expr) { StackValue lastValue = gen(expr); - if (lastValue.type != Type.VOID_TYPE) { + if (!endsWithReturn(expr)) { lastValue.put(returnType, v); v.areturn(returnType); } - else if (!endsWithReturn(expr)) { - v.areturn(returnType); - } } private static boolean endsWithReturn(JetElement bodyExpression) { diff --git a/compiler/testData/codegen/box/unit/kt4212.kt b/compiler/testData/codegen/box/unit/kt4212.kt new file mode 100644 index 00000000000..d8edbfacdf5 --- /dev/null +++ b/compiler/testData/codegen/box/unit/kt4212.kt @@ -0,0 +1,20 @@ +fun foo(): Any? = bar() + +fun bar() {} + +fun baz(): Any? { + return bar() +} + +fun quux(): Unit? = bar() + +fun box(): String { + foo() + + if (foo() != Unit.VALUE) return "Fail 1" + if (foo() != bar()) return "Fail 2" + if (bar() != baz()) return "Fail 3" + if (baz() != quux()) return "Fail 4" + + 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 8b07fd0f152..44bfd68e6a7 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java @@ -4764,6 +4764,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("kt4212.kt") + public void testKt4212() throws Exception { + doTest("compiler/testData/codegen/box/unit/kt4212.kt"); + } + @TestMetadata("kt4265.kt") public void testKt4265() throws Exception { doTest("compiler/testData/codegen/box/unit/kt4265.kt");