diff options
author | Paul Phillips <paulp@improving.org> | 2011-07-01 15:28:03 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-07-01 15:28:03 +0000 |
commit | ab3e6f21aebeb24075483fbefab48a13e65d48a4 (patch) | |
tree | f87501c307951fd46b046c3ddf65cc240c65914a /src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala | |
parent | 3c5f893b78e5cfc7d0124cece34daea257e649ff (diff) | |
download | scala-ab3e6f21aebeb24075483fbefab48a13e65d48a4.tar.gz scala-ab3e6f21aebeb24075483fbefab48a13e65d48a4.tar.bz2 scala-ab3e6f21aebeb24075483fbefab48a13e65d48a4.zip |
Stable hashCodes for case objects.
Somehow case objects fell through the cracks and have had default
hashCode implementations, leading to new hashCodes on each jvm run. Now
we use the productPrefix, and inline it right into the bytecode.
scala> None.## == "None".##
res0: Boolean = true
Closes #4752, no review.
Diffstat (limited to 'src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala')
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala b/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala index c855abd7fb..0917646f76 100644 --- a/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala +++ b/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala @@ -126,6 +126,13 @@ trait SyntheticMethods extends ast.TreeDSL { val method = syntheticMethod(nme.toString_, FINAL, makeNoArgConstructor(StringClass.tpe)) typer typed { DEF(method) === LIT(clazz.name.decode) } } + def moduleHashCodeMethod: Tree = { + val method = syntheticMethod(nme.hashCode_, FINAL, makeNoArgConstructor(IntClass.tpe)) + // The string being used as hashcode basis is also productPrefix. + val code = clazz.name.decode.hashCode + + typer typed { DEF(method) === LIT(code) } + } def forwardingMethod(name: Name, targetName: Name): Tree = { val target = getMember(ScalaRunTimeModule, targetName) @@ -262,6 +269,7 @@ trait SyntheticMethods extends ast.TreeDSL { ) // methods for case objects only def objectMethods = List( + Object_hashCode -> (() => moduleHashCodeMethod), Object_toString -> (() => moduleToStringMethod) ) // methods for both classes and objects |