Skip to content

Commit

Permalink
Modification to SI-6534 patch.
Browse files Browse the repository at this point in the history
Only exclude hashCode and equals from being overridden in
value classes, not other synthetics which may turn up such
as case class methods.
  • Loading branch information
paulp committed Oct 30, 2012
1 parent a7276d5 commit c3e2a81
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
20 changes: 11 additions & 9 deletions src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala
Expand Up @@ -310,17 +310,19 @@ trait SyntheticMethods extends ast.TreeDSL {
* 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")
def shouldGenerate(m: Symbol) = {
!hasOverridingImplementation(m) || {
clazz.isDerivedValueClass && (m == Any_hashCode || m == Any_equals) && {
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()
true
}
}
else
for ((m, impl) <- methods ; if !hasOverridingImplementation(m)) yield impl()
}
for ((m, impl) <- methods ; if shouldGenerate(m)) yield impl()
}
def extras = (
if (needsReadResolve) {
Expand Down
5 changes: 4 additions & 1 deletion test/files/neg/t6534.check
Expand Up @@ -10,5 +10,8 @@ class Bippy3(val x: Int) extends AnyVal { override def equals(x: Any) = false }
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
^
t6534.scala:9: error: redefinition of equals method. See SIP-15, criterion 4. is not allowed in value class
case class Bippy6(val x: Int) extends AnyVal { override def productPrefix = "Dingo" ; override def equals(x: Any) = false } // error
^
two warnings found
two errors found
three errors found
3 changes: 3 additions & 0 deletions test/files/neg/t6534.scala
Expand Up @@ -5,3 +5,6 @@ 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
case class Bippy5(val x: Int) extends AnyVal { override def productPrefix = "Dingo" } // nothing
case class Bippy6(val x: Int) extends AnyVal { override def productPrefix = "Dingo" ; override def equals(x: Any) = false } // error

0 comments on commit c3e2a81

Please sign in to comment.