Skip to content

Commit 03b6a47

Browse files
committed
Avoid calling unsupported computeHash in some situations
Some types do not implement `computeHash`, instead they override `myHash` directly. This works fine as long as `myHash` is not equal to `HashUnknown` but this was not guaranteed before this commit, if `myHash` is equal to `HashUnknown` then `computeHash` is called by `CachedGroundType#hash` or `CachedProxyType#hash` causing an exception: https://gist.github.com/smarter/6b642db0495e995d8f3c26d614cf54d6 This commit fixes this by making sure we never compute a hash equal to `HashUnknown`, instead `HashUnknownAlt` should be used.
1 parent 557d448 commit 03b6a47

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/dotty/tools/dotc/core/Hashable.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ trait Hashable {
3131
protected def hashSeed: Int = getClass.hashCode
3232

3333
protected final def finishHash(hashCode: Int, arity: Int): Int =
34-
avoidNotCached(hashing.finalizeHash(hashCode, arity))
34+
avoidSpecialHashes(hashing.finalizeHash(hashCode, arity))
3535

36-
final def identityHash = avoidNotCached(System.identityHashCode(this))
36+
final def identityHash = avoidSpecialHashes(System.identityHashCode(this))
3737

3838
protected def finishHash(seed: Int, arity: Int, tp: Type): Int = {
3939
val elemHash = tp.hash
@@ -94,7 +94,10 @@ trait Hashable {
9494

9595
protected final def addDelta(elemHash: Int, delta: Int) =
9696
if (elemHash == NotCached) NotCached
97-
else avoidNotCached(elemHash + delta)
97+
else avoidSpecialHashes(elemHash + delta)
9898

99-
private def avoidNotCached(h: Int) = if (h == NotCached) NotCachedAlt else h
99+
private def avoidSpecialHashes(h: Int) =
100+
if (h == NotCached) NotCachedAlt
101+
else if (h == HashUnknown) HashUnknownAlt
102+
else h
100103
}

src/dotty/tools/dotc/core/Types.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ object Types {
12781278
final def hash = {
12791279
if (myHash == HashUnknown) {
12801280
myHash = computeHash
1281-
if (myHash == HashUnknown) myHash = HashUnknownAlt
1281+
assert(myHash != HashUnknown)
12821282
}
12831283
myHash
12841284
}
@@ -1293,7 +1293,7 @@ object Types {
12931293
final def hash = {
12941294
if (myHash == HashUnknown) {
12951295
myHash = computeHash
1296-
if (myHash == HashUnknown) myHash = HashUnknownAlt
1296+
assert(myHash != HashUnknown)
12971297
}
12981298
myHash
12991299
}

0 commit comments

Comments
 (0)