-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compare productPrefix in synthetic case class canEqual
Since 2.13, case class `hashCode` mixes in the hash code of the `productPrefix` string. The synthetic `equals` method has fallen out of sync in that regard, so two instances can be equal but have different hash codes. This commit changes the synthetic `canEqual` method to also compare the `productPrefix` of the two instances.
- Loading branch information
Showing
2 changed files
with
31 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
case class C(x: Int) | ||
class D extends C(1) { override def productPrefix = "D" } | ||
|
||
abstract case class C1(a: Int) | ||
class C2(a: Int) extends C1(a) { override def productPrefix = "C2" } | ||
class C3(a: Int) extends C1(a) { override def productPrefix = "C3" } | ||
|
||
case class VCC(x: Int) extends AnyVal | ||
|
||
object Test extends App { | ||
val c = C(1) | ||
assert(c != new D) | ||
assert(c == C(1)) | ||
assert(!c.canEqual(new D)) | ||
|
||
val c2 = new C2(1) | ||
val c3 = new C3(1) | ||
assert(c2 != c3) | ||
assert(c2.hashCode != c3.hashCode) | ||
assert(!c2.canEqual(c3)) | ||
|
||
assert(VCC(1).canEqual(VCC(1))) | ||
assert(!VCC(1).canEqual(1)) | ||
} |