Skip to content

Fix #10527: Generate canEqual test for case class equals #10600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions compiler/src/dotty/tools/dotc/transform/SyntheticMembers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
def enumValueSymbols(using Context): List[Symbol] = { initSymbols; myEnumValueSymbols }
def nonJavaEnumValueSymbols(using Context): List[Symbol] = { initSymbols; myNonJavaEnumValueSymbols }

private def existingDef(sym: Symbol, clazz: ClassSymbol)(using Context): Symbol = {
private def existingDef(sym: Symbol, clazz: ClassSymbol)(using Context): Symbol =
val existing = sym.matchingMember(clazz.thisType)
if (existing != sym && !existing.is(Deferred)) existing
else NoSymbol
}
if existing != sym && !existing.is(Deferred) then existing else NoSymbol

private def synthesizeDef(sym: TermSymbol, rhsFn: List[List[Tree]] => Context ?=> Tree)(using Context): Tree =
DefDef(sym, rhsFn(_)(using ctx.withOwner(sym))).withSpan(ctx.owner.span.focus)
Expand Down Expand Up @@ -236,12 +234,13 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
* def equals(that: Any): Boolean =
* (this eq that) || {
* that match {
* case x$0 @ (_: C @unchecked) => this.x == this$0.x && this.y == x$0.y
* case x$0 @ (_: C @unchecked) => this.x == this$0.x && this.y == x$0.y && that.canEqual(this)
* case _ => false
* }
* ```
*
* If `C` is a value class the initial `eq` test is omitted.
* If `C` is a value class, the initial `eq` test is omitted.
* The `canEqual` test can be omitted if it is known that `canEqual` return always true.
*
* `@unchecked` is needed for parametric case classes.
*
Expand All @@ -254,8 +253,14 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
val sortedAccessors = accessors.sortBy(accessor => if (accessor.info.typeSymbol.isPrimitiveValueClass) 0 else 1)
val comparisons = sortedAccessors.map { accessor =>
This(clazz).select(accessor).equal(ref(thatAsClazz).select(accessor)) }
val rhs = // this.x == this$0.x && this.y == x$0.y
if (comparisons.isEmpty) Literal(Constant(true)) else comparisons.reduceLeft(_ and _)
var rhs = // this.x == this$0.x && this.y == x$0.y && that.canEqual(this)
if comparisons.isEmpty then Literal(Constant(true)) else comparisons.reduceLeft(_ and _)
val canEqualMeth = existingDef(defn.Product_canEqual, clazz)
if !clazz.is(Final) || canEqualMeth.exists && !canEqualMeth.is(Synthetic) then
rhs = rhs.and(
ref(thatAsClazz)
.select(canEqualMeth.orElse(defn.Product_canEqual))
.appliedTo(This(clazz)))
val matchingCase = CaseDef(pattern, EmptyTree, rhs) // case x$0 @ (_: C) => this.x == this$0.x && this.y == x$0.y
val defaultCase = CaseDef(Underscore(defn.AnyType), EmptyTree, Literal(Constant(false))) // case _ => false
val matchExpr = Match(that, List(matchingCase, defaultCase))
Expand Down
8 changes: 8 additions & 0 deletions tests/run/i10527.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
case class C(x: Int)
class CC(x: Int) extends C(x) { override def canEqual(o: Any) = o.isInstanceOf[CC] }
final case class D(x: Int)
final case class E(x: Int) { override def canEqual(o: Any) = false }
@main def Test =
assert(C(1) != new CC(1))
assert(D(1) == D(1))
assert(E(1) != E(1))