Skip to content

Commit 4b59e10

Browse files
committed
Handle class constants in hashing
1 parent 62dfdaf commit 4b59e10

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

compiler/src/dotty/tools/dotc/sbt/ExtractAPI.scala

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,16 +632,30 @@ private class ExtractAPICollector(using Context) extends ThunkHolder {
632632
end positionedHash
633633

634634
def iteratorHash(it: Iterator[Any], initHash: Int): Int =
635+
import core.Constants._
635636
var h = initHash
636637
while it.hasNext do
637638
it.next() match
638639
case p: Positioned =>
639640
h = positionedHash(p, h)
640641
case xs: List[?] =>
641642
h = iteratorHash(xs.iterator, h)
642-
case c: core.Constants.Constant =>
643+
case c: Constant =>
643644
h = MurmurHash3.mix(h, c.tag)
644-
h = MurmurHash3.mix(h, c.value.##) // We can't use `value.hashCode` since value might be null
645+
c.tag match
646+
case NullTag =>
647+
// No value to hash, the tag is enough.
648+
case ClazzTag =>
649+
// Go through `apiType` to get a value with a stable hash, it'd
650+
// be better to use Murmur here too instead of relying on
651+
// `hashCode`, but that would essentially mean duplicating
652+
// https://github.com/sbt/zinc/blob/develop/internal/zinc-apiinfo/src/main/scala/xsbt/api/HashAPI.scala
653+
// and at that point we might as well do type hashing on our own
654+
// representation.
655+
val apiValue = apiType(c.typeValue)
656+
h = MurmurHash3.mix(h, apiValue.hashCode)
657+
case _ =>
658+
h = MurmurHash3.mix(h, c.value.hashCode)
645659
case n: Name =>
646660
// The hashCode of the name itself is not stable across compiler instances
647661
h = MurmurHash3.mix(h, n.toString.hashCode)

sbt-dotty/sbt-test/source-dependencies/inline-inherited/A.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class A {
33
class Local {
44
private val y: Int = 1
55
}
6+
val a = scala.reflect.classTag[Integer]
67
println(new Local)
78
val x = 1
89
x.toString

0 commit comments

Comments
 (0)