Skip to content

Commit

Permalink
KT-2269 fix: hashCode & equals for primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
alextkachman committed Aug 4, 2012
1 parent bcec8b7 commit 301b557
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
@@ -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<JetExpression> 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);
}
}
Expand Up @@ -26,11 +26,8 @@
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;

Expand All @@ -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<Name> 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<Name> 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"));
Expand Down Expand Up @@ -100,6 +98,8 @@ public void init() {
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);
Expand Down
13 changes: 13 additions & 0 deletions 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"
}
Expand Up @@ -440,4 +440,9 @@ public void testKt1634() {
public void testKt1397() {
blackBoxFile("regressions/kt1397.kt");
}

public void testKt2269() {
blackBoxFile("regressions/kt2269.kt");
System.out.println(generateToText());
}
}

0 comments on commit 301b557

Please sign in to comment.