@@ -214,12 +214,19 @@ class SyntheticMethods(thisPhase: DenotTransformer) {
214
214
* ```
215
215
*/
216
216
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
+ }
223
230
}
224
231
225
232
/** The `hashCode` implementation for given symbol `sym`. */
0 commit comments