Skip to content

Commit 34aaca5

Browse files
committed
fix some errors
1 parent d2e4924 commit 34aaca5

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

compiler/src/dotty/tools/dotc/core/Uniques.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import scala.annotation.tailrec
1212

1313
class Uniques extends WeakHashSet[Type](Config.initialUniquesCapacity):
1414
override def hash(x: Type): Int = x.hash
15-
override def isEqual(x: Type, y: Type) = x.eql(y)
15+
override def isEqual(x: Type | Null, y: Type | Null) =
16+
if x == null then y == null
17+
else y != null && x.eql(y)
1618

1719
/** Defines operation `unique` for hash-consing types.
1820
* Also defines specialized hash sets for hash consing uniques of a specific type.

compiler/src/dotty/tools/dotc/util/WeakHashSet.scala

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import java.lang.ref.{ReferenceQueue, WeakReference}
99
import scala.annotation.{ constructorOnly, tailrec }
1010
import scala.collection.mutable
1111

12+
import dotty.tools.uncheckedNN
13+
1214
/**
1315
* A HashSet where the elements are stored weakly. Elements in this set are eligible for GC if no other
1416
* hard references are associated with them. Its primary use case is as a canonical reference
@@ -61,7 +63,7 @@ abstract class WeakHashSet[A <: AnyRef](initialCapacity: Int = 8, loadFactor: Do
6163

6264
private def computeThreshold: Int = (table.size * loadFactor).ceil.toInt
6365

64-
protected def hash(key: A | Null): Int
66+
protected def hash(key: A): Int
6567
protected def isEqual(x: A | Null, y: A | Null): Boolean =
6668
if x == null then y == null else x.equals(y)
6769

@@ -149,7 +151,7 @@ abstract class WeakHashSet[A <: AnyRef](initialCapacity: Int = 8, loadFactor: Do
149151
case null => null
150152
case _ =>
151153
val entryElem = entry.get
152-
if (isEqual(elem, entryElem.asInstanceOf[A])) entryElem
154+
if (isEqual(elem, entryElem)) entryElem
153155
else linkedListLoop(entry.tail)
154156
}
155157

@@ -165,8 +167,6 @@ abstract class WeakHashSet[A <: AnyRef](initialCapacity: Int = 8, loadFactor: Do
165167
}
166168

167169
def put(elem: A): A = {
168-
// case null => throw new NullPointerException("WeakHashSet cannot hold nulls")
169-
// case _ =>
170170
Stats.record(statsItem("put"))
171171
removeStaleEntries()
172172
val h = hash(elem)
@@ -177,8 +177,8 @@ abstract class WeakHashSet[A <: AnyRef](initialCapacity: Int = 8, loadFactor: Do
177177
def linkedListLoop(entry: Entry[A] | Null): A = entry match {
178178
case null => addEntryAt(bucket, elem, h, oldHead)
179179
case _ =>
180-
val entryElem = entry.get.asInstanceOf[A]
181-
if (isEqual(elem, entryElem)) entryElem
180+
val entryElem = entry.get
181+
if (isEqual(elem, entryElem)) entryElem.uncheckedNN
182182
else linkedListLoop(entry.tail)
183183
}
184184

@@ -294,7 +294,7 @@ abstract class WeakHashSet[A <: AnyRef](initialCapacity: Int = 8, loadFactor: Do
294294
assert(entry.get != null, s"$entry had a null value indicated that gc activity was happening during diagnostic validation or that a null value was inserted")
295295
computedCount += 1
296296
val cachedHash = entry.hash
297-
val realHash = hash(entry.get)
297+
val realHash = hash(entry.get.uncheckedNN)
298298
assert(cachedHash == realHash, s"for $entry cached hash was $cachedHash but should have been $realHash")
299299
val computedBucket = index(realHash)
300300
assert(computedBucket == bucket, s"for $entry the computed bucket was $computedBucket but should have been $bucket")

0 commit comments

Comments
 (0)