Skip to content

Commit 2987afe

Browse files
committed
Fix more according to comments
1 parent 7a68ebb commit 2987afe

File tree

6 files changed

+35
-34
lines changed

6 files changed

+35
-34
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ object Symbols {
9191

9292
private[core] def invalidateDenotCache(): Unit = { checkedPeriod = Nowhere }
9393

94-
/** Set the denotation of this symbol */
94+
/** Set the denotation of this symbol
95+
* `denot` should always be initialized when a new Symbol is created.
96+
*/
9597
private[core] def denot_=(d: SymDenotation): Unit = {
9698
util.Stats.record("Symbol.denot_=")
9799
lastDenot = d

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
3636

3737
protected given [DummySoItsADef]: Context = myContext
3838

39-
protected var state: TyperState | Null = null
40-
def constraint: Constraint = state.nn.constraint
41-
def constraint_=(c: Constraint): Unit = state.nn.constraint = c
39+
protected var state: TyperState = compiletime.uninitialized
40+
def constraint: Constraint = state.constraint
41+
def constraint_=(c: Constraint): Unit = state.constraint = c
4242

4343
def init(c: Context): Unit =
4444
myContext = c
@@ -220,7 +220,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
220220
def monitoredIsSubType = {
221221
if (pendingSubTypes == null) {
222222
pendingSubTypes = util.HashSet[(Type, Type)]()
223-
report.log(s"!!! deep subtype recursion involving ${tp1.show} <:< ${tp2.show}, constraint = ${state.nn.constraint.show}")
223+
report.log(s"!!! deep subtype recursion involving ${tp1.show} <:< ${tp2.show}, constraint = ${state.constraint.show}")
224224
report.log(s"!!! constraint = ${constraint.show}")
225225
//if (ctx.settings.YnoDeepSubtypes.value) {
226226
// new Error("deep subtype").printStackTrace()
@@ -390,7 +390,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
390390
thirdTry
391391
case tp1: TypeParamRef =>
392392
def flagNothingBound = {
393-
if (!frozenConstraint && isBottom(tp2) && state.nn.isGlobalCommittable) {
393+
if (!frozenConstraint && isBottom(tp2) && state.isGlobalCommittable) {
394394
def msg = s"!!! instantiated to Nothing: $tp1, constraint = ${constraint.show}"
395395
if (Config.failOnInstantiationToNothing) assert(false, msg)
396396
else report.log(msg)
@@ -1309,7 +1309,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
13091309
val saved = constraint
13101310
val savedGadt = ctx.gadt.fresh
13111311
inline def restore() =
1312-
state.nn.constraint = saved
1312+
state.constraint = saved
13131313
ctx.gadt.restore(savedGadt)
13141314
val savedSuccessCount = successCount
13151315
try
@@ -1319,7 +1319,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
13191319
recCount -= 1
13201320
if !result then restore()
13211321
else if recCount == 0 && needsGc then
1322-
state.nn.gc()
1322+
state.gc()
13231323
needsGc = false
13241324
if (Stats.monitored) recordStatistics(result, savedSuccessCount)
13251325
result

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ class TyperState() {
253253
Stats.record("typerState.gc")
254254
val toCollect = new mutable.ListBuffer[TypeLambda]
255255
for tvar <- ownedVars do
256-
val tvaros = tvar.owningState.nn.get
257-
assert(tvaros != null && (tvaros eq this), s"Inconsistent state in $this: it owns $tvar whose owningState is ${tvaros}")
256+
val tvarState = tvar.owningState.nn.get
257+
assert(tvarState eqn this, s"Inconsistent state in $this: it owns $tvar whose owningState is ${tvarState}")
258258
assert(!tvar.inst.exists, s"Inconsistent state in $this: it owns $tvar which is already instantiated")
259259
val inst = constraint.instType(tvar)
260260
if inst.exists then

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ object Types {
725725
NoDenotation
726726
}
727727
def goRec(tp: RecType) =
728-
// TODO: test remove this check
728+
// TODO: change tp.parent to nullable or other values
729729
if ((tp.parent: Type | Null) == null) NoDenotation
730730
else if (tp eq pre) go(tp.parent)
731731
else {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import scala.annotation.tailrec
1010

1111
class Uniques extends WeakHashSet[Type](Config.initialUniquesCapacity):
1212
override def hash(x: Type): Int = x.hash
13-
override def isEqual(x: Type | Null, y: Type | Null) =
14-
if x == null then y == null
15-
else y != null && x.eql(y)
13+
override def isEqual(x: Type, y: Type) = x.eql(y)
1614

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

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ abstract class WeakHashSet[A <: AnyRef](initialCapacity: Int = 8, loadFactor: Do
6161
private def computeThreshold: Int = (table.size * loadFactor).ceil.toInt
6262

6363
protected def hash(key: A): Int
64-
protected def isEqual(x: A | Null, y: A | Null): Boolean =
65-
if x == null then y == null else x.equals(y)
64+
protected def isEqual(x: A, y: A): Boolean = x.equals(y)
6665

6766
/** Turn hashcode `x` into a table index */
6867
protected def index(x: Int): Int = x & (table.length - 1)
@@ -135,24 +134,25 @@ abstract class WeakHashSet[A <: AnyRef](initialCapacity: Int = 8, loadFactor: Do
135134
tableLoop(0)
136135
}
137136

138-
def lookup(elem: A): A | Null = {
139-
// case null => throw new NullPointerException("WeakHashSet cannot hold nulls")
140-
// case _ =>
137+
// TODO: remove the `case null` when we can enable explicit nulls in regular compiling,
138+
// since the type `A <: AnyRef` of `elem` can ensure the value is not null.
139+
def lookup(elem: A): A | Null = (elem: A | Null) match {
140+
case null => throw new NullPointerException("WeakHashSet cannot hold nulls")
141+
case _ =>
142+
Stats.record(statsItem("lookup"))
143+
removeStaleEntries()
144+
val bucket = index(hash(elem))
141145

142-
Stats.record(statsItem("lookup"))
143-
removeStaleEntries()
144-
val bucket = index(hash(elem))
145-
146-
@tailrec
147-
def linkedListLoop(entry: Entry[A] | Null): A | Null = entry match {
148-
case null => null
149-
case _ =>
150-
val entryElem = entry.get
151-
if (isEqual(elem, entryElem)) entryElem
152-
else linkedListLoop(entry.tail)
153-
}
146+
@tailrec
147+
def linkedListLoop(entry: Entry[A] | Null): A | Null = entry match {
148+
case null => null
149+
case _ =>
150+
val entryElem = entry.get
151+
if entryElem != null && isEqual(elem, entryElem) then entryElem
152+
else linkedListLoop(entry.tail)
153+
}
154154

155-
linkedListLoop(table(bucket))
155+
linkedListLoop(table(bucket))
156156
}
157157

158158
protected def addEntryAt(bucket: Int, elem: A, elemHash: Int, oldHead: Entry[A] | Null): A = {
@@ -175,7 +175,7 @@ abstract class WeakHashSet[A <: AnyRef](initialCapacity: Int = 8, loadFactor: Do
175175
case null => addEntryAt(bucket, elem, h, oldHead)
176176
case _ =>
177177
val entryElem = entry.get
178-
if (isEqual(elem, entryElem)) entryElem.uncheckedNN
178+
if entryElem != null && isEqual(elem, entryElem) then entryElem.uncheckedNN
179179
else linkedListLoop(entry.tail)
180180
}
181181

@@ -192,7 +192,8 @@ abstract class WeakHashSet[A <: AnyRef](initialCapacity: Int = 8, loadFactor: Do
192192
@tailrec
193193
def linkedListLoop(prevEntry: Entry[A] | Null, entry: Entry[A] | Null): Unit =
194194
if entry != null then
195-
if isEqual(elem, entry.get) then remove(bucket, prevEntry, entry)
195+
val entryElem = entry.get
196+
if entryElem != null && isEqual(elem, entryElem) then remove(bucket, prevEntry, entry)
196197
else linkedListLoop(entry, entry.tail)
197198

198199
linkedListLoop(null, table(bucket))

0 commit comments

Comments
 (0)