Skip to content

Commit 87b2ddb

Browse files
author
EnzeXing
committed
Extend whitelist and add tests
1 parent 02533ed commit 87b2ddb

File tree

7 files changed

+49
-5
lines changed

7 files changed

+49
-5
lines changed

compiler/src/dotty/tools/dotc/transform/init/Objects.scala

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import scala.collection.mutable
2929
import scala.annotation.tailrec
3030
import scala.annotation.constructorOnly
3131
import dotty.tools.dotc.core.Flags.AbstractOrTrait
32+
import Decorators.*
3233

3334
/** Check initialization safety of static objects
3435
*
@@ -68,11 +69,21 @@ import dotty.tools.dotc.core.Flags.AbstractOrTrait
6869
*
6970
*/
7071
class Objects(using Context @constructorOnly):
71-
val immutableHashSetBuider: Symbol = requiredClass("scala.collection.immutable.HashSetBuilder")
72+
val immutableHashSetNode: Symbol = requiredClass("scala.collection.immutable.SetNode")
7273
// TODO: this should really be an annotation on the rhs of the field initializer rather than the field itself.
73-
val HashSetBuilder_rootNode: Symbol = immutableHashSetBuider.requiredValue("rootNode")
74-
75-
val whiteList = Set(HashSetBuilder_rootNode)
74+
val SetNode_EmptySetNode: Symbol = Denotations.staticRef("scala.collection.immutable.SetNode.EmptySetNode".toTermName).symbol
75+
val immutableHashSet: Symbol = requiredModule("scala.collection.immutable.HashSet")
76+
val HashSet_EmptySet: Symbol = Denotations.staticRef("scala.collection.immutable.HashSet.EmptySet".toTermName).symbol
77+
val immutableVector: Symbol = requiredModule("scala.collection.immutable.Vector")
78+
val Vector_EmptyIterator: Symbol = immutableVector.requiredValue("emptyIterator")
79+
val immutableMapNode: Symbol = requiredModule("scala.collection.immutable.MapNode")
80+
val MapNode_EmptyMapNode: Symbol = immutableMapNode.requiredValue("EmptyMapNode")
81+
val immutableHashMap: Symbol = requiredModule("scala.collection.immutable.HashMap")
82+
val HashMap_EmptyMap: Symbol = immutableHashMap.requiredValue("EmptyMap")
83+
val immutableLazyList: Symbol = requiredModule("scala.collection.immutable.LazyList")
84+
val LazyList_empty: Symbol = immutableLazyList.requiredValue("_empty")
85+
86+
val whiteList: Set[Symbol] = Set()
7687

7788
// ----------------------------- abstract domain -----------------------------
7889

@@ -162,7 +173,7 @@ class Objects(using Context @constructorOnly):
162173
extends Ref(valsMap = mutable.Map.empty, varsMap = mutable.Map.empty, outersMap = mutable.Map.empty):
163174
val owner = klass
164175

165-
def show(using Context) = "ObjectRef(" + klass.show + ")"
176+
def show(using Context) = "ObjectRef(" + klass.show + ")" + "valMap = " + vals + "varMap = " + vars
166177

167178
/**
168179
* Represents values that are instances of the specified class.
@@ -821,6 +832,7 @@ class Objects(using Context @constructorOnly):
821832
errorReadOtherStaticObject(State.currentObject, addr)
822833
Bottom
823834
else if ref.isObjectRef && ref.klass.hasSource then
835+
println(s"Uninitialized field Position 2, ref = $ref, target = $target")
824836
report.warning("Access uninitialized field " + field.show + ". " + Trace.show, Trace.position)
825837
Bottom
826838
else
@@ -829,6 +841,7 @@ class Objects(using Context @constructorOnly):
829841
else if ref.hasVal(target) then
830842
ref.valValue(target)
831843
else if ref.isObjectRef && ref.klass.hasSource then
844+
println(s"Uninitialized field Position 2, ref = $ref, target = $target")
832845
report.warning("Access uninitialized field " + field.show + ". " + Trace.show, Trace.position)
833846
Bottom
834847
else

tests/init-global/pos/EmptyMap.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import scala.collection.immutable.HashMap
2+
3+
object O {
4+
val emptyMap: HashMap[Int, Int] = HashMap.empty
5+
val key = emptyMap.get(0)
6+
}

tests/init-global/pos/EmptyMap2.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import scala.collection.immutable.HashMap
2+
3+
object A:
4+
val a = HashMap.empty[Int, Int].updated(1, 2)

tests/init-global/pos/EmptySet.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import scala.collection.immutable.HashSet
2+
3+
object O {
4+
val emptySet = HashSet.empty
5+
val emptySetSize = emptySet.size
6+
}

tests/init-global/pos/EmptySet2.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import scala.collection.immutable.HashSet
2+
3+
object A:
4+
val a = HashSet.empty[Int] + 1
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.collection.immutable.Vector
2+
3+
object O {
4+
val emptyVector = Vector.empty
5+
val emptyVectorIterator = emptyVector.iterator
6+
val hasNext = emptyVectorIterator.hasNext
7+
}

tests/init-global/pos/LazyList.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import scala.collection.immutable.LazyList
2+
3+
object A:
4+
val a = LazyList.empty[Int] :+ 1

0 commit comments

Comments
 (0)