Skip to content

Commit d345609

Browse files
Merge pull request #3838 from dotty-staging/fix-#3106
Fix #3106: Pre-compute hash for case classes with 0 parameters
2 parents 57b857e + 12453a7 commit d345609

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,19 @@ class SyntheticMethods(thisPhase: DenotTransformer) {
214214
* ```
215215
*/
216216
def caseHashCodeBody(implicit ctx: Context): Tree = {
217-
val acc = ctx.newSymbol(ctx.owner, "acc".toTermName, Mutable | Synthetic, defn.IntType, coord = ctx.owner.pos)
218-
val accDef = ValDef(acc, Literal(Constant(clazz.fullName.toString.hashCode)))
219-
val mixes = for (accessor <- accessors.toList) yield
220-
Assign(ref(acc), ref(defn.staticsMethod("mix")).appliedTo(ref(acc), hashImpl(accessor)))
221-
val finish = ref(defn.staticsMethod("finalizeHash")).appliedTo(ref(acc), Literal(Constant(accessors.size)))
222-
Block(accDef :: mixes, finish)
217+
val seed = clazz.fullName.toString.hashCode
218+
if (accessors.nonEmpty) {
219+
val acc = ctx.newSymbol(ctx.owner, "acc".toTermName, Mutable | Synthetic, defn.IntType, coord = ctx.owner.pos)
220+
val accDef = ValDef(acc, Literal(Constant(seed)))
221+
val mixes = for (accessor <- accessors) yield
222+
Assign(ref(acc), ref(defn.staticsMethod("mix")).appliedTo(ref(acc), hashImpl(accessor)))
223+
val finish = ref(defn.staticsMethod("finalizeHash")).appliedTo(ref(acc), Literal(Constant(accessors.size)))
224+
Block(accDef :: mixes, finish)
225+
} else {
226+
// Pre-compute the hash code
227+
val hash = scala.runtime.Statics.finalizeHash(seed, 0)
228+
Literal(Constant(hash))
229+
}
223230
}
224231

225232
/** The `hashCode` implementation for given symbol `sym`. */

0 commit comments

Comments
 (0)