Skip to content

Fix #3140: Generate better equals for case/value classes #4169

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 2 commits into from
Mar 24, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ class SyntheticMethods(thisPhase: DenotTransformer) {
def wildcardAscription(tp: Type) = Typed(Underscore(tp), TypeTree(tp))
val pattern = Bind(thatAsClazz, wildcardAscription(clazzType)) // x$0 @ (_: C)
val comparisons = accessors map { accessor =>
This(clazz).select(accessor).select(defn.Any_==).appliedTo(ref(thatAsClazz).select(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 _)
val matchingCase = CaseDef(pattern, EmptyTree, rhs) // case x$0 @ (_: C) => this.x == this$0.x && this.y == x$0.y
val defaultCase = CaseDef(wildcardAscription(defn.AnyType), EmptyTree, Literal(Constant(false))) // case _ => false
val defaultCase = CaseDef(Underscore(defn.AnyType), EmptyTree, Literal(Constant(false))) // case _ => false
val matchExpr = Match(that, List(matchingCase, defaultCase))
if (isDerivedValueClass(clazz)) matchExpr
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class VCElideAllocations extends MiniPhase with IdentityDenotTransformer {
case BinaryOp(NewWithArgs(tp1, List(u1)), op, NewWithArgs(tp2, List(u2)))
if (tp1 eq tp2) && (op eq defn.Any_==) && isDerivedValueClass(tp1.typeSymbol) =>
// == is overloaded in primitive classes
applyOverloaded(u1, nme.EQ, List(u2), Nil, defn.BooleanType)
u1.equal(u2)

// (new V(u)).underlying() => u
case ValueClassUnbox(NewWithArgs(_, List(u))) =>
Expand Down
23 changes: 23 additions & 0 deletions compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,27 @@ class TestBCode extends DottyBytecodeTest {
assert(!hasInstanceof, "Try case should not issue INSTANCEOF opcode\n")
}
}

@Test def noBoxingInSyntheticEquals = {
val source =
"""
|case class Case(x: Long)
|class Value(val x: Long) extends AnyVal
""".stripMargin

checkBCode(source) { dir =>
for ((clsName, methodName) <- List(("Case", "equals"), ("Value$", "equals$extension"))) {
val moduleIn = dir.lookupName(s"$clsName.class", directory = false)
val moduleNode = loadClassNode(moduleIn.input)
val equalsMethod = getMethod(moduleNode, methodName)

val callsEquals = instructionsFromMethod(equalsMethod).exists {
case i @ Invoke(_, _, "equals", _, _) => true
case i => false
}

assert(!callsEquals, s"equals method should not be called in the definition of $clsName#$methodName\n")
}
}
}
}