aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Martres <smarter@ubuntu.com>2015-02-15 00:03:58 +0100
committerGuillaume Martres <smarter@ubuntu.com>2015-02-17 13:24:08 +0100
commitb1d6de2e8293c78571d85c09968a26c67cc96619 (patch)
tree75dae5022b6221858cedce3b7ff8334fb05509b8
parentff8779e7d2e8a1baf9b3655f8c0bea0dc3fc3f36 (diff)
downloaddotty-b1d6de2e8293c78571d85c09968a26c67cc96619.tar.gz
dotty-b1d6de2e8293c78571d85c09968a26c67cc96619.tar.bz2
dotty-b1d6de2e8293c78571d85c09968a26c67cc96619.zip
Fix hashCode method for value classes
Before this commit, we used the same implementation than for case classes. After this commit, we use the hashCode of the underlying type as defined in SIP-15.
-rw-r--r--src/dotty/tools/dotc/transform/SyntheticMethods.scala20
1 files changed, 17 insertions, 3 deletions
diff --git a/src/dotty/tools/dotc/transform/SyntheticMethods.scala b/src/dotty/tools/dotc/transform/SyntheticMethods.scala
index 5b27eadd9..4726105c6 100644
--- a/src/dotty/tools/dotc/transform/SyntheticMethods.scala
+++ b/src/dotty/tools/dotc/transform/SyntheticMethods.scala
@@ -76,7 +76,8 @@ class SyntheticMethods extends MiniPhaseTransform with IdentityDenotTransformer
ref(defn.runtimeMethod("_" + sym.name.toString)).appliedToArgs(This(clazz) :: vrefss.head)
def syntheticRHS(implicit ctx: Context): List[List[Tree]] => Tree = synthetic.name match {
- case nme.hashCode_ => vrefss => hashCodeBody
+ case nme.hashCode_ if isDerivedValueClass(clazz) => vrefss => valueHashCodeBody
+ case nme.hashCode_ => vrefss => caseHashCodeBody
case nme.toString_ => forwardToRuntime
case nme.equals_ => vrefss => equalsBody(vrefss.head.head)
case nme.canEqual_ => vrefss => canEqualBody(vrefss.head.head)
@@ -122,9 +123,22 @@ class SyntheticMethods extends MiniPhaseTransform with IdentityDenotTransformer
/** The class
*
+ * class C(x: T) extends AnyVal
+ *
+ * gets the hashCode method:
+ *
+ * def hashCode: Int = x.hashCode()
+ */
+ def valueHashCodeBody(implicit ctx: Context): Tree = {
+ assert(accessors.length == 1)
+ ref(accessors.head).select(nme.hashCode_).ensureApplied
+ }
+
+ /** The class
+ *
* case class C(x: T, y: T)
*
- * get the hashCode method:
+ * gets the hashCode method:
*
* def hashCode: Int = {
* <synthetic> var acc: Int = 0xcafebabe;
@@ -133,7 +147,7 @@ class SyntheticMethods extends MiniPhaseTransform with IdentityDenotTransformer
* Statics.finalizeHash(acc, 2)
* }
*/
- def hashCodeBody(implicit ctx: Context): Tree = {
+ def caseHashCodeBody(implicit ctx: Context): Tree = {
val acc = ctx.newSymbol(ctx.owner, "acc".toTermName, Mutable | Synthetic, defn.IntType, coord = ctx.owner.pos)
val accDef = ValDef(acc, Literal(Constant(0xcafebabe)))
val mixes = for (accessor <- accessors.toList) yield