diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/HashCode.java b/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/HashCode.java new file mode 100644 index 00000000000..bc9e0e0a222 --- /dev/null +++ b/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/HashCode.java @@ -0,0 +1,49 @@ +/* + * 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.annotations.Nullable; +import org.jetbrains.asm4.Opcodes; +import org.jetbrains.asm4.Type; +import org.jetbrains.asm4.commons.InstructionAdapter; +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.JetExpression; + +import java.util.List; + +/** + * @author alex.tkachman + */ +public class HashCode implements IntrinsicMethod { + @Override + public StackValue generate(ExpressionCodegen codegen, + InstructionAdapter v, + @NotNull Type expectedType, + @Nullable PsiElement element, + @Nullable List arguments, + StackValue receiver, + @NotNull GenerationState state) { + receiver.put(JetTypeMapper.TYPE_OBJECT, v); + v.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "hashCode", "()I"); + return StackValue.onStack(Type.INT_TYPE); + } +} 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 0149ecd1fe9..c3dad7f1755 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/intrinsics/IntrinsicMethods.java @@ -26,11 +26,8 @@ import org.jetbrains.jet.lang.resolve.DescriptorUtils; import org.jetbrains.jet.lang.resolve.java.JvmPrimitiveType; import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe; import org.jetbrains.jet.lang.resolve.name.Name; -import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.types.lang.JetStandardClasses; -import org.jetbrains.jet.lang.types.lang.JetStandardLibrary; import org.jetbrains.jet.lang.types.lang.PrimitiveType; -import org.jetbrains.jet.lang.types.TypeProjection; import org.jetbrains.jet.lang.types.expressions.OperatorConventions; import org.jetbrains.asm4.Opcodes; @@ -51,6 +48,7 @@ public class IntrinsicMethods { private static final IntrinsicMethod DOWN_TO = new UpTo(false); private static final IntrinsicMethod INC = new Increment(1); private static final IntrinsicMethod DEC = new Increment(-1); + private static final IntrinsicMethod HASH_CODE = new HashCode(); private static final List PRIMITIVE_TYPES = ImmutableList.of(Name.identifier("Boolean"), Name.identifier("Byte"), Name.identifier("Char"), Name.identifier("Short"), Name.identifier("Int"), Name.identifier("Float"), Name.identifier("Long"), Name.identifier("Double")); private static final List PRIMITIVE_NUMBER_TYPES = ImmutableList.of(Name.identifier("Byte"), Name.identifier("Char"), Name.identifier("Short"), Name.identifier("Int"), Name.identifier("Float"), Name.identifier("Long"), Name.identifier("Double")); @@ -100,6 +98,8 @@ public class IntrinsicMethods { declareIntrinsicFunction(type, Name.identifier("downto"), 1, DOWN_TO); declareIntrinsicFunction(type, Name.identifier("inc"), 0, INC); declareIntrinsicFunction(type, Name.identifier("dec"), 0, DEC); + declareIntrinsicFunction(type, Name.identifier("hashCode"), 0, HASH_CODE); + declareIntrinsicFunction(type, Name.identifier("equals"), 1, EQUALS); } declareBinaryOp(Name.identifier("plus"), Opcodes.IADD); diff --git a/compiler/testData/codegen/regressions/kt2269.kt b/compiler/testData/codegen/regressions/kt2269.kt new file mode 100644 index 00000000000..a836753105f --- /dev/null +++ b/compiler/testData/codegen/regressions/kt2269.kt @@ -0,0 +1,13 @@ +fun box() : String { + 230?.toByte()?.hashCode() + 9.hashCode() + + if(230.equals(9.toByte())) { + return "fail" + } + + if(230 == 9.toByte().toInt()) { + return "fail" + } + return "OK" +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/codegen/PrimitiveTypesTest.java b/compiler/tests/org/jetbrains/jet/codegen/PrimitiveTypesTest.java index 66e631db3f9..16c1afcc361 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/PrimitiveTypesTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/PrimitiveTypesTest.java @@ -440,4 +440,9 @@ public class PrimitiveTypesTest extends CodegenTestCase { public void testKt1397() { blackBoxFile("regressions/kt1397.kt"); } + + public void testKt2269() { + blackBoxFile("regressions/kt2269.kt"); + System.out.println(generateToText()); + } }