Skip to content

Commit af86110

Browse files
committed
Fix names
1 parent a3aab34 commit af86110

File tree

5 files changed

+46
-41
lines changed

5 files changed

+46
-41
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ object Contexts {
273273
else scope
274274

275275
def nestingLevel: Int =
276-
val sc = effectiveScope
276+
// TODO
277+
val sc: Scope | Null = effectiveScope
277278
if sc != null then sc.nestingLevel else 0
278279

279280
/** Sourcefile corresponding to given abstract file, memoized */

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dotty.tools
22
package dotc
33
package core
44

5-
// import scala.language.{unsafeNulls => _}
5+
import scala.language.{unsafeNulls => _}
66

77
import scala.io.Codec
88
import util.NameTransformer
@@ -167,15 +167,15 @@ object Names {
167167
override def asTermName: TermName = this
168168

169169
@sharable // because it is only modified in the synchronized block of toTypeName.
170-
private var myTypeName: TypeName = null
170+
private var myTypeName: TypeName | Null = null
171171
// Note: no @volatile needed since type names are immutable and therefore safely published
172172

173173
override def toTypeName: TypeName =
174174
if myTypeName == null then
175175
synchronized {
176176
if myTypeName == null then myTypeName = new TypeName(this)
177177
}
178-
myTypeName
178+
myTypeName.nn
179179

180180
override def likeSpaced(name: Name): TermName = name.toTermName
181181

@@ -191,7 +191,7 @@ object Names {
191191
val derivedName = new DerivedName(this, info)
192192
derivedNames = derivedNames.updated(info, derivedName)
193193
derivedName
194-
case derivedName =>
194+
case derivedName: DerivedName =>
195195
derivedName
196196
}
197197

@@ -229,10 +229,10 @@ object Names {
229229
}
230230

231231
@sharable // because it's just a cache for performance
232-
private var myMangledString: String = null
232+
private var myMangledString: String | Null = null
233233

234234
@sharable // because it's just a cache for performance
235-
private var myMangled: Name = null
235+
private var myMangled: Name | Null = null
236236

237237
protected[Names] def mangle: ThisName
238238

@@ -244,7 +244,7 @@ object Names {
244244
final def mangledString: String = {
245245
if (myMangledString == null)
246246
myMangledString = qualToString(_.mangledString, _.mangled.toString)
247-
myMangledString
247+
myMangledString.nn
248248
}
249249

250250
/** If this a qualified name, split it into underlying, last part, and separator
@@ -263,11 +263,11 @@ object Names {
263263

264264
protected def computeToString: String
265265

266-
@sharable private var myToString: String = null
266+
@sharable private var myToString: String | Null = null
267267

268-
override def toString =
268+
override def toString: String =
269269
if myToString == null then myToString = computeToString
270-
myToString
270+
myToString.nn
271271

272272
}
273273

@@ -390,7 +390,7 @@ object Names {
390390
// because asserts are caught in exception handlers which might
391391
// cause other failures. In that case the first, important failure
392392
// is lost.
393-
System.err.println("Backend should not call Name#toString, Name#mangledString should be used instead.")
393+
System.err.nn.println("Backend should not call Name#toString, Name#mangledString should be used instead.")
394394
Thread.dumpStack()
395395
assert(false)
396396
}
@@ -401,8 +401,8 @@ object Names {
401401
* from GenBCode or it also contains one of the whitelisted methods below.
402402
*/
403403
private def toStringOK = {
404-
val trace = Thread.currentThread.getStackTrace
405-
!trace.exists(_.getClassName.endsWith("GenBCode")) ||
404+
val trace: Array[StackTraceElement] = Thread.currentThread.nn.getStackTrace.asInstanceOf[Array[StackTraceElement]]
405+
!trace.exists(_.getClassName.nn.endsWith("GenBCode")) ||
406406
trace.exists(elem =>
407407
List(
408408
"mangledString",
@@ -546,10 +546,10 @@ object Names {
546546
Stats.record(statsItem("put"))
547547
val myTable = currentTable // could be outdated under parallel execution
548548
var idx = hashValue(cs, offset, len) & (myTable.length - 1)
549-
var name = myTable(idx).asInstanceOf[SimpleName]
549+
var name = myTable(idx).asInstanceOf[SimpleName | Null]
550550
while name != null do
551-
if name.length == len && Names.equals(name.start, cs, offset, len) then
552-
return name
551+
if name.nn.length == len && Names.equals(name.nn.start, cs, offset, len) then
552+
return name.nn
553553
Stats.record(statsItem("miss"))
554554
idx = (idx + 1) & (myTable.length - 1)
555555
name = myTable(idx).asInstanceOf[SimpleName]
@@ -567,7 +567,7 @@ object Names {
567567
ensureCapacity(nc + len)
568568
Array.copy(cs, offset, chrs, nc, len)
569569
nc += len
570-
addEntryAt(idx, name)
570+
addEntryAt(idx, name.nn)
571571
else
572572
enterIfNew(cs, offset, len)
573573
}
@@ -630,10 +630,10 @@ object Names {
630630
* See `sliceToTermName` in `Decorators` for a more efficient version
631631
* which however requires a Context for its operation.
632632
*/
633-
def termName(s: String): SimpleName = termName(s.toCharArray, 0, s.length)
633+
def termName(s: String): SimpleName = termName(s.toCharArray.nn, 0, s.length)
634634

635635
/** Create a type name from a string */
636-
def typeName(s: String): TypeName = typeName(s.toCharArray, 0, s.length)
636+
def typeName(s: String): TypeName = typeName(s.toCharArray.nn, 0, s.length)
637637

638638
/** The type name represented by the empty string */
639639
val EmptyTypeName: TypeName = EmptyTermName.toTypeName

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
313313
*/
314314
def isDefinedInCurrentUnit(denot: Denotation)(using Context): Boolean = denot match {
315315
case MultiDenotation(d1, d2) => isDefinedInCurrentUnit(d1) || isDefinedInCurrentUnit(d2)
316-
case denot: SingleDenotation => ctx.compilationUnit != null && denot.symbol.source == ctx.compilationUnit.source
316+
case denot: SingleDenotation => (ctx.compilationUnit: CompilationUnit | Null) != null && denot.symbol.source == ctx.compilationUnit.source
317317
}
318318

319319
/** Is `denot` the denotation of a self symbol? */

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dotty.tools.dotc.util
22

3+
import scala.language.{unsafeNulls => _}
4+
35
object HashSet:
46

57
/** The number of elements up to which dense packing is used.
@@ -29,12 +31,12 @@ class HashSet[T](initialCapacity: Int = 8, capacityMultiple: Int = 2) extends Mu
2931

3032
private var used: Int = _
3133
private var limit: Int = _
32-
private var table: Array[AnyRef] = _
34+
private var table: Array[AnyRef | Null] = _
3335

3436
clear()
3537

3638
private def allocate(capacity: Int) =
37-
table = new Array[AnyRef](capacity)
39+
table = new Array[AnyRef | Null](capacity)
3840
limit = if capacity <= DenseLimit then capacity - 1 else capacity / capacityMultiple
3941

4042
private def roundToPower(n: Int) =
@@ -67,22 +69,22 @@ class HashSet[T](initialCapacity: Int = 8, capacityMultiple: Int = 2) extends Mu
6769
/** Turn hashcode `x` into a table index */
6870
protected def index(x: Int): Int = x & (table.length - 1)
6971

70-
protected def currentTable: Array[AnyRef] = table
72+
protected def currentTable: Array[AnyRef | Null] = table
7173

7274
protected def firstIndex(x: T) = if isDense then 0 else index(hash(x))
7375
protected def nextIndex(idx: Int) =
7476
Stats.record(statsItem("miss"))
7577
index(idx + 1)
7678

77-
protected def entryAt(idx: Int) = table(idx).asInstanceOf[T]
78-
protected def setEntry(idx: Int, x: T) = table(idx) = x.asInstanceOf[AnyRef]
79+
protected def entryAt(idx: Int): T | Null = table(idx).asInstanceOf[T | Null]
80+
protected def setEntry(idx: Int, x: T) = table(idx) = x.asInstanceOf[AnyRef | Null]
7981

8082
def lookup(x: T): T | Null =
8183
Stats.record(statsItem("lookup"))
8284
var idx = firstIndex(x)
83-
var e = entryAt(idx)
85+
var e: T | Null = entryAt(idx)
8486
while e != null do
85-
if isEqual(e, x) then return e
87+
if isEqual(e.asInstanceOf[T], x) then return e.asInstanceOf[T]
8688
idx = nextIndex(idx)
8789
e = entryAt(idx)
8890
null
@@ -100,7 +102,7 @@ class HashSet[T](initialCapacity: Int = 8, capacityMultiple: Int = 2) extends Mu
100102
var idx = firstIndex(x)
101103
var e = entryAt(idx)
102104
while e != null do
103-
if isEqual(e, x) then return e
105+
if isEqual(e.asInstanceOf[T], x) then return e.asInstanceOf[T]
104106
idx = nextIndex(idx)
105107
e = entryAt(idx)
106108
addEntryAt(idx, x)
@@ -112,20 +114,20 @@ class HashSet[T](initialCapacity: Int = 8, capacityMultiple: Int = 2) extends Mu
112114
var idx = firstIndex(x)
113115
var e = entryAt(idx)
114116
while e != null do
115-
if isEqual(e, x) then
117+
if isEqual(e.asInstanceOf[T], x) then
116118
var hole = idx
117119
while
118120
idx = nextIndex(idx)
119121
e = entryAt(idx)
120122
e != null
121123
do
122-
val eidx = index(hash(e))
124+
val eidx = index(hash(e.asInstanceOf[T]))
123125
if isDense
124126
|| index(eidx - (hole + 1)) > index(idx - (hole + 1))
125127
// entry `e` at `idx` can move unless `index(hash(e))` is in
126128
// the (ring-)interval [hole + 1 .. idx]
127129
then
128-
setEntry(hole, e)
130+
setEntry(hole, e.asInstanceOf[T])
129131
hole = idx
130132
table(hole) = null
131133
used -= 1
@@ -146,14 +148,14 @@ class HashSet[T](initialCapacity: Int = 8, capacityMultiple: Int = 2) extends Mu
146148
e = entryAt(idx)
147149
setEntry(idx, x)
148150

149-
def copyFrom(oldTable: Array[AnyRef]): Unit =
151+
def copyFrom(oldTable: Array[AnyRef | Null]): Unit =
150152
if isDense then
151153
Array.copy(oldTable, 0, table, 0, oldTable.length)
152154
else
153155
var idx = 0
154156
while idx < oldTable.length do
155-
val e = oldTable(idx).asInstanceOf[T]
156-
if e != null then addOld(e)
157+
val e = oldTable(idx).asInstanceOf[T | Null]
158+
if e != null then addOld(e.asInstanceOf[T])
157159
idx += 1
158160

159161
protected def growTable(): Unit =
@@ -165,14 +167,14 @@ class HashSet[T](initialCapacity: Int = 8, capacityMultiple: Int = 2) extends Mu
165167
copyFrom(oldTable)
166168

167169
abstract class EntryIterator extends Iterator[T]:
168-
def entry(idx: Int): T
170+
def entry(idx: Int): T | Null
169171
private var idx = 0
170172
def hasNext =
171173
while idx < table.length && table(idx) == null do idx += 1
172174
idx < table.length
173175
def next() =
174176
require(hasNext)
175-
try entry(idx) finally idx += 1
177+
try entry(idx).asInstanceOf[T] finally idx += 1
176178

177179
def iterator: Iterator[T] = new EntryIterator():
178180
def entry(idx: Int) = entryAt(idx)

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
package dotty.tools.dotc.util
22

3+
import scala.language.{unsafeNulls => _}
4+
35
import collection.immutable
46

57
/** A linear map is a map where after an `updated` the previous map
68
* value cannot be used anymore. The map is implemented as an immutable
79
* map for sizes <= 4 (where immutable maps have specialized, compact
810
* representations) and as a HashMap for larger sizes.
911
*/
10-
opaque type LinearMap[K <: AnyRef, V >: Null <: AnyRef] =
12+
opaque type LinearMap[K <: AnyRef, V <: AnyRef | Null] =
1113
immutable.Map[K, V] | HashMap[K, V]
1214

1315
object LinearMap:
1416

15-
def empty[K <: AnyRef, V >: Null <: AnyRef]: LinearMap[K, V] =
17+
def empty[K <: AnyRef, V <: AnyRef | Null]: LinearMap[K, V] =
1618
immutable.Map.empty[K, V]
1719

18-
extension [K <: AnyRef, V >: Null <: AnyRef](m: LinearMap[K, V])
20+
extension [K <: AnyRef, V <: AnyRef | Null](m: LinearMap[K, V])
1921

20-
def lookup(key: K): V /*| Null*/ = (m: @unchecked) match
22+
def lookup(key: K): V | Null = (m: @unchecked) match
2123
case m: immutable.AbstractMap[K, V] @unchecked =>
2224
if m.contains(key) then m(key) else null
2325
case m: HashMap[K, V] @unchecked =>

0 commit comments

Comments
 (0)