Skip to content

Commit e9ce033

Browse files
committed
refactor, fix caseClassHash test
Static.mix(acc, this.productPrefix.hashCode()) in synthetic case class hash code updated to add parens.
1 parent 7586fe7 commit e9ce033

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ class Definitions {
357357
@tu lazy val ScalaRuntimeModule: Symbol = ctx.requiredModule("scala.runtime.ScalaRunTime")
358358
def runtimeMethodRef(name: PreName): TermRef = ScalaRuntimeModule.requiredMethodRef(name)
359359
def ScalaRuntime_drop: Symbol = runtimeMethodRef(nme.drop).symbol
360+
@tu lazy val ScalaRuntime__hashCode: Symbol = runtimeMethodRef("_hashCode").symbol
360361

361362
@tu lazy val BoxesRunTimeModule: Symbol = ctx.requiredModule("scala.runtime.BoxesRunTime")
362363
@tu lazy val ScalaStaticsModule: Symbol = ctx.requiredModule("scala.runtime.Statics")

compiler/src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ object StdNames {
141141
val INITIALIZER_PREFIX: N = "initial$"
142142
val BOUNDTYPE_ANNOT: N = "$boundType$"
143143
val QUOTE: N = "'"
144-
val TYPE_QUOTE: N = "type_'"
144+
val TYPE_QUOTE: N = "type_'"
145145
val TRAIT_SETTER_SEPARATOR: N = str.TRAIT_SETTER_SEPARATOR
146146

147147
// value types (and AnyRef) are all used as terms as well
@@ -368,6 +368,7 @@ object StdNames {
368368
val TypeRef: N = "TypeRef"
369369
val UNIT : N = "UNIT"
370370
val add_ : N = "add"
371+
val acc: N = "acc"
371372
val annotation: N = "annotation"
372373
val anyHash: N = "anyHash"
373374
val anyValClass: N = "anyValClass"

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
238238
* gets the `hashCode` method:
239239
*
240240
* ```
241-
* "C".hashCode // constant folded
241+
* def hashCode: Int = "C".hashCode // constant folded
242242
* ```
243243
*
244244
* The class
@@ -247,7 +247,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
247247
* case class C(x: T, y: U)
248248
* ```
249249
*
250-
* if non of `T` or `U` are primitive types, gets the `hashCode` method:
250+
* if none of `T` or `U` are primitive types, gets the `hashCode` method:
251251
*
252252
* ```
253253
* def hashCode: Int = ScalaRunTime._hashCode(this)
@@ -256,12 +256,12 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
256256
* else if either `T` or `U` are primitive, gets the `hashCode` method implemented by [[caseHashCodeBody]]
257257
*/
258258
def chooseHashcode(implicit ctx: Context) = {
259-
if clazz.is(ModuleClass) then
259+
if (clazz.is(ModuleClass))
260260
Literal(Constant(clazz.name.stripModuleClassSuffix.toString.hashCode))
261-
else if accessors `exists` (_.info.finalResultType.classSymbol.isPrimitiveValueClass) then
261+
else if (accessors `exists` (_.info.finalResultType.classSymbol.isPrimitiveValueClass))
262262
caseHashCodeBody
263263
else
264-
ref(defn.ScalaRuntimeModule).select("_hashCode".toTermName).appliedTo(This(clazz))
264+
ref(defn.ScalaRuntimeModule).select(defn.ScalaRuntime__hashCode).appliedTo(This(clazz))
265265
}
266266

267267
/** The class
@@ -275,19 +275,19 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
275275
* ```
276276
* def hashCode: Int = {
277277
* <synthetic> var acc: Int = 0xcafebabe
278-
* acc = Statics.mix(acc, this.productPrefix.hashCode);
278+
* acc = Statics.mix(acc, this.productPrefix.hashCode());
279279
* acc = Statics.mix(acc, x);
280280
* acc = Statics.mix(acc, Statics.this.anyHash(y));
281281
* Statics.finalizeHash(acc, 2)
282282
* }
283283
* ```
284284
*/
285285
def caseHashCodeBody(implicit ctx: Context): Tree = {
286-
val acc = ctx.newSymbol(ctx.owner, "acc".toTermName, Mutable | Synthetic, defn.IntType, coord = ctx.owner.span)
287-
val accDef = ValDef(acc, Literal(Constant(0xcafebabe)))
286+
val acc = ctx.newSymbol(ctx.owner, nme.acc, Mutable | Synthetic, defn.IntType, coord = ctx.owner.span)
287+
val accDef = ValDef(acc, Literal(Constant(0xcafebabe)))
288288
val mixPrefix = Assign(ref(acc),
289-
ref(defn.staticsMethod("mix")).appliedTo(ref(acc), This(clazz).select(defn.Product_productPrefix).select(defn.Any_hashCode)))
290-
val mixes = for accessor <- accessors yield
289+
ref(defn.staticsMethod("mix")).appliedTo(ref(acc), This(clazz).select(defn.Product_productPrefix).select(defn.Any_hashCode).appliedToNone))
290+
val mixes = for (accessor <- accessors) yield
291291
Assign(ref(acc), ref(defn.staticsMethod("mix")).appliedTo(ref(acc), hashImpl(accessor)))
292292
val finish = ref(defn.staticsMethod("finalizeHash")).appliedTo(ref(acc), Literal(Constant(accessors.size)))
293293
Block(accDef :: mixPrefix :: mixes, finish)

tests/run/caseClassHash.check

Lines changed: 6 additions & 6 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-
205963949
4-
205963949
3+
930449446
4+
930449446
55
true
6-
## method 1: 205963949
7-
## method 2: 205963949
8-
Murmur 1: 1383698062
9-
Murmur 2: 1383698062
6+
## method 1: 930449446
7+
## method 2: 930449446
8+
Murmur 1: 930449446
9+
Murmur 2: 930449446

0 commit comments

Comments
 (0)