From 83b93071d34e4630a07c33e5584013d6805780d6 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 7 Feb 2013 16:49:27 +0400 Subject: [PATCH] Fix over-optimized comparison with null --- .../jetbrains/jet/codegen/ExpressionCodegen.java | 4 ++-- .../primitiveTypes/comparisonWithNullCallsFun.kt | 13 +++++++++++++ .../generated/BlackBoxCodegenTestGenerated.java | 5 +++++ 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/codegen/box/primitiveTypes/comparisonWithNullCallsFun.kt diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index 6539c9da624..30d86ffde76 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -2386,11 +2386,11 @@ public class ExpressionCodegen extends JetVisitor implem Type leftType = expressionType(left); Type rightType = expressionType(right); - if (leftType.equals(JET_NOTHING_TYPE)) { + if (JetPsiUtil.isNullConstant(left)) { return genCmpWithNull(right, rightType, opToken); } - if (rightType.equals(JET_NOTHING_TYPE)) { + if (JetPsiUtil.isNullConstant(right)) { return genCmpWithNull(left, leftType, opToken); } diff --git a/compiler/testData/codegen/box/primitiveTypes/comparisonWithNullCallsFun.kt b/compiler/testData/codegen/box/primitiveTypes/comparisonWithNullCallsFun.kt new file mode 100644 index 00000000000..5f83a03b067 --- /dev/null +++ b/compiler/testData/codegen/box/primitiveTypes/comparisonWithNullCallsFun.kt @@ -0,0 +1,13 @@ +var entered = 0 + +fun foo(t: T): T { + entered++ + return t +} + +fun box(): String { + if (foo(null) == null) {} + if (null == foo(null)) {} + if (foo(null) == foo(null)) {} + return if (entered == 4) "OK" else "Fail $entered" +} diff --git a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java index 67f1d10bce6..d4ef0fc3f05 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxCodegenTestGenerated.java @@ -2775,6 +2775,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/codegen/box/primitiveTypes"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("comparisonWithNullCallsFun.kt") + public void testComparisonWithNullCallsFun() throws Exception { + blackBoxFileByFullPath("compiler/testData/codegen/box/primitiveTypes/comparisonWithNullCallsFun.kt"); + } + @TestMetadata("ea35963.kt") public void testEa35963() throws Exception { blackBoxFileByFullPath("compiler/testData/codegen/box/primitiveTypes/ea35963.kt");