From a4ebbaa1a0030344f268c265d63c4bb30de8e227 Mon Sep 17 00:00:00 2001 From: Alex Tkachman Date: Mon, 2 Apr 2012 13:32:15 +0300 Subject: [PATCH] KT-1441 proper identityEquals --- .../codegen/intrinsics/IdentityEquals.java | 45 +++++++++++++++++++ .../codegen/intrinsics/IntrinsicMethods.java | 3 +- .../testData/codegen/regressions/kt1441.kt | 16 +++++++ .../jet/codegen/ControlStructuresTest.java | 4 ++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IdentityEquals.java create mode 100644 compiler/testData/codegen/regressions/kt1441.kt diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IdentityEquals.java b/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IdentityEquals.java new file mode 100644 index 00000000000..062475d49f8 --- /dev/null +++ b/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IdentityEquals.java @@ -0,0 +1,45 @@ +/* + * Copyright 2010-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.codegen.intrinsics; + +import com.intellij.psi.PsiElement; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.codegen.ExpressionCodegen; +import org.jetbrains.jet.codegen.GenerationState; +import org.jetbrains.jet.codegen.JetTypeMapper; +import org.jetbrains.jet.codegen.StackValue; +import org.jetbrains.jet.lang.psi.JetCallExpression; +import org.jetbrains.jet.lang.psi.JetExpression; +import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.jet.lang.types.JetType; +import org.jetbrains.jet.lexer.JetTokens; +import org.objectweb.asm.Type; +import org.objectweb.asm.commons.InstructionAdapter; + +import java.util.List; + +/** + * @author alex.tkachman + */ +public class IdentityEquals implements IntrinsicMethod { + @Override + public StackValue generate(ExpressionCodegen codegen, InstructionAdapter v, Type expectedType, PsiElement element, List arguments, StackValue receiver, @NotNull GenerationState state) { + receiver.put(JetTypeMapper.TYPE_OBJECT, v); + codegen.gen(arguments.get(0)).put(JetTypeMapper.TYPE_OBJECT, v); + return StackValue.cmp(JetTokens.EQEQEQ, JetTypeMapper.TYPE_OBJECT); + } +} diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java b/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java index 3eea7b0b88f..07ce098f0da 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java @@ -59,6 +59,7 @@ public class IntrinsicMethods { public static final IntrinsicMethod ARRAY_SIZE = new ArraySize(); public static final IntrinsicMethod ARRAY_INDICES = new ArrayIndices(); public static final Equals EQUALS = new Equals(); + public static final IdentityEquals IDENTITY_EQUALS = new IdentityEquals(); public static final IteratorNext ITERATOR_NEXT = new IteratorNext(); public static final ArraySet ARRAY_SET = new ArraySet(); public static final ArrayGet ARRAY_GET = new ArrayGet(); @@ -129,7 +130,7 @@ public class IntrinsicMethods { declareOverload(myStdLib.getLibraryScope().getFunctions("toString"), 0, new ToString()); declareOverload(myStdLib.getLibraryScope().getFunctions("equals"), 1, EQUALS); - declareOverload(myStdLib.getLibraryScope().getFunctions("identityEquals"), 1, EQUALS); + declareOverload(myStdLib.getLibraryScope().getFunctions("identityEquals"), 1, IDENTITY_EQUALS); declareOverload(myStdLib.getLibraryScope().getFunctions("plus"), 1, STRING_PLUS); declareOverload(myStdLib.getLibraryScope().getFunctions("Array"), 1, new NewArray()); declareOverload(myStdLib.getLibraryScope().getFunctions("sure"), 0, new Sure()); diff --git a/compiler/testData/codegen/regressions/kt1441.kt b/compiler/testData/codegen/regressions/kt1441.kt new file mode 100644 index 00000000000..bcaa78766c6 --- /dev/null +++ b/compiler/testData/codegen/regressions/kt1441.kt @@ -0,0 +1,16 @@ +class Foo { + var rnd = 10 + + public fun equals(that : Any) : Boolean = that is Foo && (that.rnd == rnd) +} + +fun box() : String { + val a = Foo() + val b = Foo() + if (!a.identityEquals(a)) return "fail 1" + if (!b.identityEquals(b)) return "fail 2" + if (b.identityEquals(a)) return "fail 3" + if (a.identityEquals(b)) return "fail 4" + if( a !=b ) return "fail5" + return "OK" +} diff --git a/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java b/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java index c77fc11498b..ed7c393f87a 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java @@ -290,4 +290,8 @@ public class ControlStructuresTest extends CodegenTestCase { public void testKt628() throws Exception { blackBoxFile("regressions/kt628.kt"); } + + public void testKt1441() throws Exception { + blackBoxFile("regressions/kt1441.kt"); + } }