Skip to content

Commit c6acb0f

Browse files
committed
Make KernelNodes.HashNode and HashingNodes.ToHashByHashCode consistent
(cherry picked from commit 423aa6e)
1 parent e7b1cba commit c6acb0f

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

src/main/java/org/truffleruby/core/hash/HashingNodes.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ protected int hashCompareByIdentity(Object key, boolean compareByIdentity,
5858
}
5959

6060
// MRI: any_hash
61+
/** Keep consistent with {@link org.truffleruby.core.kernel.KernelNodes.HashNode} */
6162
@GenerateUncached
6263
public abstract static class ToHashByHashCode extends RubyBaseNode {
6364

@@ -116,7 +117,7 @@ protected int hashSymbol(RubySymbol value,
116117
}
117118

118119
@Fallback
119-
protected int hash(Object value,
120+
protected int hashOther(Object value,
120121
@Cached DispatchNode callHash,
121122
@Cached HashCastResultNode cast) {
122123
return cast.execute(callHash.call(value, "hash"));

src/main/java/org/truffleruby/core/kernel/KernelNodes.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.concurrent.TimeUnit;
1818

1919
import com.oracle.truffle.api.dsl.CachedContext;
20+
import com.oracle.truffle.api.dsl.Fallback;
2021
import com.oracle.truffle.api.frame.Frame;
2122
import com.oracle.truffle.api.utilities.AssumedValue;
2223
import org.jcodings.specific.UTF8Encoding;
@@ -83,6 +84,7 @@
8384
import org.truffleruby.core.support.TypeNodes.ObjectInstanceVariablesNode;
8485
import org.truffleruby.core.support.TypeNodesFactory.ObjectInstanceVariablesNodeFactory;
8586
import org.truffleruby.core.symbol.RubySymbol;
87+
import org.truffleruby.core.symbol.SymbolNodes;
8688
import org.truffleruby.core.symbol.SymbolTable;
8789
import org.truffleruby.core.thread.GetCurrentRubyThreadNode;
8890
import org.truffleruby.core.thread.RubyThread;
@@ -930,6 +932,7 @@ protected boolean isFrozen(Object self,
930932

931933
}
932934

935+
/** Keep consistent with {@link org.truffleruby.core.hash.HashingNodes.ToHashByHashCode} */
933936
@CoreMethod(names = "hash")
934937
public abstract static class HashNode extends CoreMethodArrayArgumentsNode {
935938

@@ -940,37 +943,50 @@ public static HashNode create() {
940943
public abstract Object execute(Object value);
941944

942945
@Specialization
943-
protected long hash(int value) {
944-
return HashOperations.hashLong(value, getContext(), this);
946+
protected long hashBoolean(boolean value) {
947+
return HashOperations.hashBoolean(value, getContext(), this);
945948
}
946949

947950
@Specialization
948-
protected long hash(long value) {
951+
protected long hashInt(int value) {
949952
return HashOperations.hashLong(value, getContext(), this);
950953
}
951954

952955
@Specialization
953-
protected long hash(double value) {
954-
return HashOperations.hashDouble(value, getContext(), this);
956+
protected long hashLong(long value) {
957+
return HashOperations.hashLong(value, getContext(), this);
955958
}
956959

957960
@Specialization
958-
protected long hash(boolean value) {
959-
return HashOperations.hashBoolean(value, getContext(), this);
961+
protected long hashDouble(double value) {
962+
return HashOperations.hashDouble(value, getContext(), this);
960963
}
961964

962965
@Specialization
963966
protected long hashBignum(RubyBignum value) {
964967
return HashOperations.hashBignum(value, getContext(), this);
965968
}
966969

967-
@Specialization(guards = "!isRubyBignum(self)")
968-
protected int hash(ImmutableRubyObject self) {
969-
return System.identityHashCode(self);
970+
@Specialization
971+
protected long hashString(RubyString value,
972+
@Cached StringNodes.HashStringNode stringHashNode) {
973+
return stringHashNode.execute(value);
974+
}
975+
976+
@Specialization
977+
protected long hashImmutableString(ImmutableRubyString value,
978+
@Cached StringNodes.HashStringNode stringHashNode) {
979+
return stringHashNode.execute(value);
970980
}
971981

972982
@Specialization
973-
protected int hash(RubyDynamicObject self) {
983+
protected long hashSymbol(RubySymbol value,
984+
@Cached SymbolNodes.HashSymbolNode symbolHashNode) {
985+
return symbolHashNode.execute(value);
986+
}
987+
988+
@Fallback
989+
protected int hashOtherUsingIdentity(Object self) {
974990
return System.identityHashCode(self);
975991
}
976992
}

0 commit comments

Comments
 (0)