Skip to content

Commit 5021c76

Browse files
authored
Merge pull request #2159 from dotty-staging/fix-hashcode
Make case class hashCode take class into account
2 parents 23709ef + 710e700 commit 5021c76

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

compiler/src/dotty/tools/dotc/transform/SyntheticMethods.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,21 @@ class SyntheticMethods(thisTransformer: DenotTransformer) {
144144

145145
/** The class
146146
*
147+
* package p
147148
* case class C(x: T, y: T)
148149
*
149150
* gets the hashCode method:
150151
*
151152
* def hashCode: Int = {
152-
* <synthetic> var acc: Int = 0xcafebabe;
153+
* <synthetic> var acc: Int = "p.C".hashCode // constant folded
153154
* acc = Statics.mix(acc, x);
154155
* acc = Statics.mix(acc, Statics.this.anyHash(y));
155156
* Statics.finalizeHash(acc, 2)
156157
* }
157158
*/
158159
def caseHashCodeBody(implicit ctx: Context): Tree = {
159160
val acc = ctx.newSymbol(ctx.owner, "acc".toTermName, Mutable | Synthetic, defn.IntType, coord = ctx.owner.pos)
160-
val accDef = ValDef(acc, Literal(Constant(0xcafebabe)))
161+
val accDef = ValDef(acc, Literal(Constant(clazz.fullName.toString.hashCode)))
161162
val mixes = for (accessor <- accessors.toList) yield
162163
Assign(ref(acc), ref(defn.staticsMethod("mix")).appliedTo(ref(acc), hashImpl(accessor)))
163164
val finish = ref(defn.staticsMethod("finalizeHash")).appliedTo(ref(acc), Literal(Constant(accessors.size)))

tests/run/caseClassHash.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Foo(true,-1,-1,d,-5,-10,500.0,500.0,List(),5.0)
22
Foo(true,-1,-1,d,-5,-10,500.0,500.0,List(),5)
3-
1383698062
4-
1383698062
3+
205963949
4+
205963949
55
true
6-
## method 1: 1383698062
7-
## method 2: 1383698062
6+
## method 1: 205963949
7+
## method 2: 205963949
88
Murmur 1: 1383698062
99
Murmur 2: 1383698062

0 commit comments

Comments
 (0)