Skip to content

Remove unnecessary uses of uncheckedNN #23344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions compiler/src/dotty/tools/backend/jvm/BCodeSyncAndTry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ trait BCodeSyncAndTry extends BCodeBodyBuilder {
val acquiredStack = if needStackStash then stack.acquireFullStack() else null
val stashLocals =
if acquiredStack == null then null
else acquiredStack.uncheckedNN.filter(_ != UNIT).map(btpe => locals.makeTempLocal(btpe))
else acquiredStack.filter(_ != UNIT).map(btpe => locals.makeTempLocal(btpe))

val hasFinally = (finalizer != tpd.EmptyTree)

Expand All @@ -240,9 +240,8 @@ trait BCodeSyncAndTry extends BCodeBodyBuilder {
*/

if stashLocals != null then
val stashLocalsNN = stashLocals.uncheckedNN // why is this necessary?
for i <- (stashLocalsNN.length - 1) to 0 by -1 do
val local = stashLocalsNN(i)
for i <- (stashLocals.length - 1) to 0 by -1 do
val local = stashLocals(i)
bc.store(local.idx, local.tk)

/* ------ (1) try-block, protected by:
Expand Down Expand Up @@ -398,17 +397,15 @@ trait BCodeSyncAndTry extends BCodeBodyBuilder {
*/

if stashLocals != null then
val stashLocalsNN = stashLocals.uncheckedNN // why is this necessary?

val resultLoc =
if kind == UNIT then null
else if tmp != null then locals(tmp) // reuse the same local
else locals.makeTempLocal(kind)
if resultLoc != null then
bc.store(resultLoc.idx, kind)

for i <- 0 until stashLocalsNN.size do
val local = stashLocalsNN(i)
for i <- 0 until stashLocals.size do
val local = stashLocals(i)
bc.load(local.idx, local.tk)
if local.tk.isRef then
bc.emit(asm.Opcodes.ACONST_NULL)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/tpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
val previousParamRefs: ListBuffer[TermRef] =
// It is ok to assign `null` here.
// If `isParamDependent == false`, the value of `previousParamRefs` is not used.
if isParamDependent then mutable.ListBuffer[TermRef]() else (null: ListBuffer[TermRef] | Null).uncheckedNN
if isParamDependent then mutable.ListBuffer[TermRef]() else null.asInstanceOf[ListBuffer[TermRef]]

def valueParam(name: TermName, origInfo: Type, isErased: Boolean): TermSymbol =
val maybeImplicit =
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ object Contexts {
finally ctx.base.comparersInUse = saved
end comparing

@sharable val NoContext: Context = new FreshContext((null: ContextBase | Null).uncheckedNN) {
@sharable val NoContext: Context = new FreshContext(null.asInstanceOf[ContextBase]) {
override val implicits: ContextualImplicits = new ContextualImplicits(Nil, null, false)(this: @unchecked)
setSource(NoSource)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5046,7 +5046,7 @@ object Types extends TypeUtils {
assert(currentEntry.bounds.contains(tp),
i"$origin is constrained to be $currentEntry but attempted to instantiate it to $tp")

if ((ctx.typerState eq owningState.nn.get.uncheckedNN) && !TypeComparer.subtypeCheckInProgress)
if ((ctx.typerState eq owningState.nn.get) && !TypeComparer.subtypeCheckInProgress)
setPermanentInst(tp)
ctx.typerState.constraint = ctx.typerState.constraint.replace(origin, tp)
tp
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class TreePickler(pickler: TastyPickler, attributes: Attributes) {
else if label == NoAddr then
pickleForwardSymRef(sym)
else
writeRef(label.uncheckedNN) // !!! Dotty problem: Not clear why nn or uncheckedNN is needed here
writeRef(label)

private def pickleForwardSymRef(sym: Symbol)(using Context) = {
val ref = reserveRef(relative = false)
Expand Down Expand Up @@ -184,7 +184,7 @@ class TreePickler(pickler: TastyPickler, attributes: Attributes) {
}
else {
writeByte(SHAREDtype)
writeRef(prev.uncheckedNN)
writeRef(prev)
}
}
catch {
Expand Down Expand Up @@ -260,7 +260,7 @@ class TreePickler(pickler: TastyPickler, attributes: Attributes) {
writeByte(RECthis)
val binderAddr: Addr | Null = pickledTypes.lookup(tpe.binder)
assert(binderAddr != null, tpe.binder)
writeRef(binderAddr.uncheckedNN)
writeRef(binderAddr)
case tpe: SkolemType =>
pickleType(tpe.info)
case tpe: RefinedType =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,6 @@ class DependencyRecorder {
* This is backed by a cache which is invalidated when `ctx.owner` changes.
*/
private def resolveDependencyFromClass(using Context): Symbol = {
import dotty.tools.uncheckedNN
if (lastOwner != ctx.owner) {
lastOwner = ctx.owner
val source = nonLocalEnclosingClass
Expand Down
10 changes: 4 additions & 6 deletions compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2151,12 +2151,10 @@ sealed class TermRefSet(using Context):
if !that.isEmpty then that.foreach(+=)

def foreach[U](f: TermRef => U): Unit =
def handle(sym: TermSymbol | Null, prefixes: Type | List[Type] | Null): Unit =
// We cannot use `.nn` here due to inference issue.
val prefixes0: Type | List[Type] = prefixes.uncheckedNN
prefixes0 match
case prefix: Type => f(TermRef(prefix, sym.uncheckedNN))
case prefixes: List[Type] => prefixes.foreach(pre => f(TermRef(pre, sym.uncheckedNN)))
def handle(sym: TermSymbol, prefixes: Type | List[Type]): Unit =
prefixes match
case prefix: Type => f(TermRef(prefix, sym))
case prefixes: List[Type] => prefixes.foreach(pre => f(TermRef(pre, sym)))
elems.forEach(handle)

// used only for debugging
Expand Down
11 changes: 4 additions & 7 deletions compiler/src/dotty/tools/dotc/util/EqHashSet.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dotty.tools.dotc.util

import dotty.tools.uncheckedNN

object EqHashSet:

def from[T](xs: IterableOnce[T]): EqHashSet[T] =
Expand Down Expand Up @@ -47,7 +45,7 @@ class EqHashSet[T](initialCapacity: Int = 8, capacityMultiple: Int = 2) extends
var idx = firstIndex(x)
var e: T | Null = entryAt(idx)
while e != null do
if isEqual(e.uncheckedNN, x) then return e
if isEqual(e, x) then return e
idx = nextIndex(idx)
e = entryAt(idx)
null
Expand All @@ -66,7 +64,7 @@ class EqHashSet[T](initialCapacity: Int = 8, capacityMultiple: Int = 2) extends
var idx = firstIndex(x)
var e: T | Null = entryAt(idx)
while e != null do
if isEqual(e.uncheckedNN, x) then return false // already entered
if isEqual(e, x) then return false // already entered
idx = nextIndex(idx)
e = entryAt(idx)
addEntryAt(idx, x)
Expand All @@ -77,8 +75,7 @@ class EqHashSet[T](initialCapacity: Int = 8, capacityMultiple: Int = 2) extends
var idx = firstIndex(x)
var e: T | Null = entryAt(idx)
while e != null do
// TODO: remove uncheckedNN when explicit-nulls is enabled for regule compiling
if isEqual(e.uncheckedNN, x) then return e.uncheckedNN
if isEqual(e, x) then return e
idx = nextIndex(idx)
e = entryAt(idx)
addEntryAt(idx, x)
Expand All @@ -101,6 +98,6 @@ class EqHashSet[T](initialCapacity: Int = 8, capacityMultiple: Int = 2) extends
var idx = 0
while idx < oldTable.length do
val e: T | Null = oldTable(idx).asInstanceOf[T | Null]
if e != null then addOld(e.uncheckedNN)
if e != null then addOld(e)
idx += 1
}
15 changes: 7 additions & 8 deletions compiler/src/dotty/tools/dotc/util/GenericHashSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ abstract class GenericHashSet[T](initialCapacity: Int = 8, capacityMultiple: Int
var idx = firstIndex(x)
var e: T | Null = entryAt(idx)
while e != null do
if isEqual(e.uncheckedNN, x) then return e
if isEqual(e, x) then return e
idx = nextIndex(idx)
e = entryAt(idx)
null
Expand All @@ -93,7 +93,7 @@ abstract class GenericHashSet[T](initialCapacity: Int = 8, capacityMultiple: Int
var idx = firstIndex(x)
var e: T | Null = entryAt(idx)
while e != null do
if isEqual(e.uncheckedNN, x) then return false // already entered
if isEqual(e, x) then return false // already entered
idx = nextIndex(idx)
e = entryAt(idx)
addEntryAt(idx, x)
Expand All @@ -104,8 +104,7 @@ abstract class GenericHashSet[T](initialCapacity: Int = 8, capacityMultiple: Int
var idx = firstIndex(x)
var e: T | Null = entryAt(idx)
while e != null do
// TODO: remove uncheckedNN when explicit-nulls is enabled for regule compiling
if isEqual(e.uncheckedNN, x) then return e.uncheckedNN
if isEqual(e, x) then return e
idx = nextIndex(idx)
e = entryAt(idx)
addEntryAt(idx, x)
Expand All @@ -117,20 +116,20 @@ abstract class GenericHashSet[T](initialCapacity: Int = 8, capacityMultiple: Int
var idx = firstIndex(x)
var e: T | Null = entryAt(idx)
while e != null do
if isEqual(e.uncheckedNN, x) then
if isEqual(e, x) then
var hole = idx
while
idx = nextIndex(idx)
e = entryAt(idx)
e != null
do
val eidx = index(hash(e.uncheckedNN))
val eidx = index(hash(e))
if isDense
|| index(eidx - (hole + 1)) > index(idx - (hole + 1))
// entry `e` at `idx` can move unless `index(hash(e))` is in
// the (ring-)interval [hole + 1 .. idx]
then
setEntry(hole, e.uncheckedNN)
setEntry(hole, e)
hole = idx
table(hole) = null
used -= 1
Expand Down Expand Up @@ -158,7 +157,7 @@ abstract class GenericHashSet[T](initialCapacity: Int = 8, capacityMultiple: Int
var idx = 0
while idx < oldTable.length do
val e: T | Null = oldTable(idx).asInstanceOf[T | Null]
if e != null then addOld(e.uncheckedNN)
if e != null then addOld(e)
idx += 1

protected def growTable(): Unit =
Expand Down
11 changes: 4 additions & 7 deletions compiler/src/dotty/tools/dotc/util/HashSet.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dotty.tools.dotc.util

import dotty.tools.uncheckedNN

object HashSet:

def from[T](xs: IterableOnce[T]): HashSet[T] =
Expand Down Expand Up @@ -51,7 +49,7 @@ class HashSet[T](initialCapacity: Int = 8, capacityMultiple: Int = 2) extends Ge
var idx = firstIndex(x)
var e: T | Null = entryAt(idx)
while e != null do
if isEqual(e.uncheckedNN, x) then return e
if isEqual(e, x) then return e
idx = nextIndex(idx)
e = entryAt(idx)
null
Expand All @@ -69,7 +67,7 @@ class HashSet[T](initialCapacity: Int = 8, capacityMultiple: Int = 2) extends Ge
var idx = firstIndex(x)
var e: T | Null = entryAt(idx)
while e != null do
if isEqual(e.uncheckedNN, x) then return false // already entered
if isEqual(e, x) then return false // already entered
idx = nextIndex(idx)
e = entryAt(idx)
addEntryAt(idx, x)
Expand All @@ -80,8 +78,7 @@ class HashSet[T](initialCapacity: Int = 8, capacityMultiple: Int = 2) extends Ge
var idx = firstIndex(x)
var e: T | Null = entryAt(idx)
while e != null do
// TODO: remove uncheckedNN when explicit-nulls is enabled for regule compiling
if isEqual(e.uncheckedNN, x) then return e.uncheckedNN
if isEqual(e, x) then return e
idx = nextIndex(idx)
e = entryAt(idx)
addEntryAt(idx, x)
Expand All @@ -104,6 +101,6 @@ class HashSet[T](initialCapacity: Int = 8, capacityMultiple: Int = 2) extends Ge
var idx = 0
while idx < oldTable.length do
val e: T | Null = oldTable(idx).asInstanceOf[T | Null]
if e != null then addOld(e.uncheckedNN)
if e != null then addOld(e)
idx += 1
}
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/util/ReadOnlyMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ abstract class ReadOnlyMap[Key, Value]:

def get(key: Key): Option[Value] = lookup(key) match
case null => None
case v => Some(v.uncheckedNN)
case v => Some(v)

def getOrElse(key: Key, value: => Value) = lookup(key) match
case null => value
case v => v.uncheckedNN
case v => v

def contains(key: Key): Boolean = lookup(key) != null

def apply(key: Key): Value = lookup(key) match
case null => throw new NoSuchElementException(s"$key")
case v => v.uncheckedNN
case v => v

def toArray: Array[(Key, Value)] =
val result = new Array[(Key, Value)](size)
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/util/WeakHashSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ abstract class WeakHashSet[A <: AnyRef](initialCapacity: Int = 8, loadFactor: Do
case null => addEntryAt(bucket, elem, h, oldHead)
case _ =>
val entryElem = entry.get
if entryElem != null && isEqual(elem, entryElem) then entryElem.uncheckedNN
if entryElem != null && isEqual(elem, entryElem) then entryElem
else linkedListLoop(entry.tail)
}

Expand Down Expand Up @@ -296,7 +296,7 @@ abstract class WeakHashSet[A <: AnyRef](initialCapacity: Int = 8, loadFactor: Do
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")
computedCount += 1
val cachedHash = entry.hash
val realHash = hash(entry.get.uncheckedNN)
val realHash = hash(entry.get)
assert(cachedHash == realHash, s"for $entry cached hash was $cachedHash but should have been $realHash")
val computedBucket = index(realHash)
assert(computedBucket == bucket, s"for $entry the computed bucket was $computedBucket but should have been $bucket")
Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/io/FileExtension.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dotty.tools.io

import dotty.tools.uncheckedNN
import dotty.tools.dotc.util.EnumFlags.FlagSet

enum FileExtension(val toLowerCase: String):
Expand Down
4 changes: 2 additions & 2 deletions compiler/test/dotty/tools/vulpix/TestConfiguration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ object TestConfiguration {
val bestEffortBaselineOptions = TestFlags(basicClasspath, noCheckOptions)
val unindentOptions = TestFlags(basicClasspath, Array("-no-indent") ++ checkOptions ++ noCheckOptions ++ yCheckOptions)
val withCompilerOptions =
defaultOptions.withClasspath(withCompilerClasspath).withRunClasspath(withCompilerClasspath)
defaultOptions.and("-Yexplicit-nulls").withClasspath(withCompilerClasspath).withRunClasspath(withCompilerClasspath)
lazy val withStagingOptions =
defaultOptions.withClasspath(withStagingClasspath).withRunClasspath(withStagingClasspath)
lazy val withTastyInspectorOptions =
Expand All @@ -89,7 +89,7 @@ object TestConfiguration {
"-Yprint-pos-syms"
)
val picklingWithCompilerOptions =
picklingOptions.withClasspath(withCompilerClasspath).withRunClasspath(withCompilerClasspath)
picklingOptions.and("-Yexplicit-nulls").withClasspath(withCompilerClasspath).withRunClasspath(withCompilerClasspath)

val explicitNullsOptions = defaultOptions and "-Yexplicit-nulls"

Expand Down
2 changes: 1 addition & 1 deletion tests/pos-with-compiler/Patterns.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import dotty.tools.dotc.ast.Trees.*
import dotty.tools.dotc.core.Types.*

object Patterns {
val d: Object = null
val d: Object = Object()
private def rebase(tp: NamedType): Type = {
def rebaseFrom(prefix: Type): Type = ???
tp.prefix match {
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-with-compiler/lazyValsSepComp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import dotty.tools.dotc.core.Contexts.*

/** A test to trigger issue with separate compilation and lazy vals */
object Foo {
val definitions: Definitions = null
val definitions: Definitions = null.asInstanceOf[Definitions]
def defn = definitions
def go = defn.ScalaBoxedClasses
}
Loading