Skip to content

Make hashcode of enum items stable #23218

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 9 additions & 8 deletions compiler/src/dotty/tools/dotc/transform/SyntheticMembers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
myCaseSymbols = defn.caseClassSynthesized
myCaseModuleSymbols = myCaseSymbols.filter(_ ne defn.Any_equals)
myEnumValueSymbols = List(defn.Product_productPrefix)
myNonJavaEnumValueSymbols = myEnumValueSymbols :+ defn.Any_toString :+ defn.Enum_ordinal
myNonJavaEnumValueSymbols = myEnumValueSymbols :+ defn.Any_toString :+ defn.Enum_ordinal :+ defn.Any_hashCode
}

def valueSymbols(using Context): List[Symbol] = { initSymbols; myValueSymbols }
Expand Down Expand Up @@ -116,6 +116,12 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
def syntheticDefIfMissing(sym: Symbol): List[Tree] =
if (existingDef(sym, clazz).exists) Nil else syntheticDef(sym) :: Nil

def identifierRef: Tree =
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this was just moved from an inner scope. No changes.

if isSimpleEnumValue then // owner is `def $new(_$ordinal: Int, $name: String) = new MyEnum { ... }`
ref(clazz.owner.paramSymss.head.find(_.name == nme.nameDollar).get)
else // assume owner is `val Foo = new MyEnum { def ordinal = 0 }`
Literal(Constant(clazz.owner.name.toString))

def syntheticDef(sym: Symbol): Tree = {
val synthetic = sym.copy(
owner = clazz,
Expand All @@ -135,12 +141,6 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
else
identifierRef

def identifierRef: Tree =
if isSimpleEnumValue then // owner is `def $new(_$ordinal: Int, $name: String) = new MyEnum { ... }`
ref(clazz.owner.paramSymss.head.find(_.name == nme.nameDollar).get)
else // assume owner is `val Foo = new MyEnum { def ordinal = 0 }`
Literal(Constant(clazz.owner.name.toString))

def ordinalRef: Tree =
if isSimpleEnumValue then // owner is `def $new(_$ordinal: Int, $name: String) = new MyEnum { ... }`
ref(clazz.owner.paramSymss.head.find(_.name == nme.ordinalDollar_).get)
Expand Down Expand Up @@ -357,7 +357,8 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
* For case classes with primitive paramters, see [[caseHashCodeBody]].
*/
def chooseHashcode(using Context) =
if (accessors.isEmpty) Literal(Constant(ownName.hashCode))
if (isNonJavaEnumValue) identifierRef.select(nme.hashCode_).appliedToTermArgs(Nil)
else if (accessors.isEmpty) Literal(Constant(ownName.hashCode))
else if (accessors.exists(_.info.finalResultType.classSymbol.isPrimitiveValueClass))
caseHashCodeBody
else
Expand Down
12 changes: 12 additions & 0 deletions tests/run/stable-enum-hashcodes.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
65
65
66
66
67
67
68
68
-1449359058
-1449359058
194551161
194551161
23 changes: 23 additions & 0 deletions tests/run/stable-enum-hashcodes.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
enum Enum:
case A
case B
case C()
case D()
case E(x: Int)

@main def Test =
// Enum values (were not stable from run to run before #23218)
println(Enum.A.hashCode)
println(Enum.A.hashCode)
println(Enum.B.hashCode)
println(Enum.B.hashCode)

// Other enum cases (were already stable from run to run)
println(Enum.C().hashCode)
println(Enum.C().hashCode)
println(Enum.D().hashCode)
println(Enum.D().hashCode)
println(Enum.E(1).hashCode)
println(Enum.E(1).hashCode)
println(Enum.E(2).hashCode)
println(Enum.E(2).hashCode)
Loading