summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2012-10-24 13:23:54 -0700
committerPaul Phillips <paulp@improving.org>2012-10-24 13:52:41 -0700
commita7276d5976f57d4f182a55f92693de97acf1de64 (patch)
treed8aef3889d0ae87cc4ea5ae75ed5addd8b34ea86
parent2c554249fd8e99286134b217027b6e3cb2c92d77 (diff)
downloadscala-a7276d5976f57d4f182a55f92693de97acf1de64.tar.gz
scala-a7276d5976f57d4f182a55f92693de97acf1de64.tar.bz2
scala-a7276d5976f57d4f182a55f92693de97acf1de64.zip
New take on SI-6534, value classes.
Don't prohibit equals and hashCode in universal traits; instead, always override them in value classes.
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala17
-rw-r--r--test/files/neg/t6534.check14
-rw-r--r--test/files/neg/t6534.flags1
-rw-r--r--test/files/neg/t6534.scala7
-rw-r--r--test/files/run/t6534.scala14
5 files changed, 52 insertions, 1 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala b/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala
index cc3d980cf1..0fda025972 100644
--- a/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala
@@ -306,7 +306,22 @@ trait SyntheticMethods extends ast.TreeDSL {
else Nil
)
- def impls = for ((m, impl) <- methods ; if !hasOverridingImplementation(m)) yield impl()
+ /** Always generate overrides for equals and hashCode in value classes,
+ * so they can appear in universal traits without breaking value semantics.
+ */
+ def impls = {
+ if (clazz.isDerivedValueClass)
+ for ((m, impl) <- methods) yield {
+ if (settings.lint.value)
+ (clazz.info nonPrivateMember m.name) filter (m => (m.owner != AnyClass) && (m.owner != clazz) && !m.isDeferred) andAlso { m =>
+ currentUnit.warning(clazz.pos, s"Implementation of ${m.name} inherited from ${m.owner} overridden in $clazz to enforce value class semantics")
+ }
+
+ impl()
+ }
+ else
+ for ((m, impl) <- methods ; if !hasOverridingImplementation(m)) yield impl()
+ }
def extras = (
if (needsReadResolve) {
// Aha, I finally decoded the original comment.
diff --git a/test/files/neg/t6534.check b/test/files/neg/t6534.check
new file mode 100644
index 0000000000..bd7dc4b71c
--- /dev/null
+++ b/test/files/neg/t6534.check
@@ -0,0 +1,14 @@
+t6534.scala:4: warning: Implementation of equals inherited from trait Foo overridden in class Bippy1 to enforce value class semantics
+class Bippy1(val x: Int) extends AnyVal with Foo { } // warn
+ ^
+t6534.scala:5: warning: Implementation of hashCode inherited from trait Ding overridden in class Bippy2 to enforce value class semantics
+class Bippy2(val x: Int) extends AnyVal with Ding { } // warn
+ ^
+t6534.scala:6: error: redefinition of equals method. See SIP-15, criterion 4. is not allowed in value class
+class Bippy3(val x: Int) extends AnyVal { override def equals(x: Any) = false } // error
+ ^
+t6534.scala:7: error: redefinition of hashCode method. See SIP-15, criterion 4. is not allowed in value class
+class Bippy4(val x: Int) extends AnyVal { override def hashCode = -1 } // error
+ ^
+two warnings found
+two errors found
diff --git a/test/files/neg/t6534.flags b/test/files/neg/t6534.flags
new file mode 100644
index 0000000000..1008b0a70c
--- /dev/null
+++ b/test/files/neg/t6534.flags
@@ -0,0 +1 @@
+-Xlint
diff --git a/test/files/neg/t6534.scala b/test/files/neg/t6534.scala
new file mode 100644
index 0000000000..a193179399
--- /dev/null
+++ b/test/files/neg/t6534.scala
@@ -0,0 +1,7 @@
+trait Foo extends Any { override def equals(x: Any) = false }
+trait Ding extends Any { override def hashCode = -1 }
+
+class Bippy1(val x: Int) extends AnyVal with Foo { } // warn
+class Bippy2(val x: Int) extends AnyVal with Ding { } // warn
+class Bippy3(val x: Int) extends AnyVal { override def equals(x: Any) = false } // error
+class Bippy4(val x: Int) extends AnyVal { override def hashCode = -1 } // error
diff --git a/test/files/run/t6534.scala b/test/files/run/t6534.scala
new file mode 100644
index 0000000000..33df97e41e
--- /dev/null
+++ b/test/files/run/t6534.scala
@@ -0,0 +1,14 @@
+trait Foo extends Any { override def equals(x: Any) = false }
+trait Ding extends Any { override def hashCode = -1 }
+
+class Bippy1(val x: Int) extends AnyVal with Foo { } // warn
+class Bippy2(val x: Int) extends AnyVal with Ding { } // warn
+
+object Test {
+ def main(args: Array[String]): Unit = {
+ val b1 = new Bippy1(71)
+ val b2 = new Bippy2(71)
+ assert(b1 == b1 && b1.## == b1.x.##, ((b1, b1.##)))
+ assert(b2 == b2 && b2.## == b2.x.##, ((b2, b2.##)))
+ }
+}