Skip to content

Commit db1c8ab

Browse files
committed
fix hashCode for value classes with null argument: return 0
1 parent bf013e2 commit db1c8ab

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

src/dotty/runtime/vc/VCPrototype.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ final class VCFloatArray[T <: VCFloatPrototype] (val arr: Array[Float], val ct:
8585

8686
abstract class VCObjectPrototype(val underlying: Object) extends VCPrototype {
8787
override def hashCode(): Int = {
88-
underlying.hashCode()
88+
if (underlying == null) 0 else underlying.hashCode()
8989
}
9090
}
9191

@@ -94,7 +94,7 @@ abstract class VCObjectCasePrototype(underlying: Object) extends VCObjectPrototy
9494
final def _1: Object = underlying
9595

9696
override final def hashCode(): Int = {
97-
underlying.hashCode()
97+
if (underlying == null) 0 else underlying.hashCode()
9898
}
9999

100100
override def toString: String = {

test/dotc/tests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class tests extends CompilerTest {
112112
@Test def pos_vc_array_int = compileFile(runDir, "valueclasses-array-int", args = "-Ycheck:all" :: Nil)
113113
@Test def pos_vc_array_object = compileFile(runDir, "valueclasses-array-object", args = "-Ycheck:all" :: Nil)
114114
@Test def pos_vc_array_newarray = compileFile(runDir, "valueclasses-array-newarray", args = "-Ycheck:all" :: Nil)
115+
@Test def pos_vc_underlying_null = compileFile(runDir, "valueclasses-underlying-null", args = "-Ycheck:all" :: Nil)
115116

116117
@Test def pos_all = compileFiles(posDir) // twice omitted to make tests run faster
117118

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
0
2+
0
3+
Set(Y@0)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class X(val x: Object) extends AnyVal
2+
class Y(val y: String) extends AnyVal
3+
object Test {
4+
def main(args: Array[String]) = {
5+
val x = new X(null)
6+
val y = new Y(null)
7+
println(x.hashCode())
8+
println(y.hashCode())
9+
10+
val r = Set(new Y(null))
11+
println(r.toString)
12+
}
13+
}

0 commit comments

Comments
 (0)