Skip to content

Commit

Permalink
Don't apply memory optimization for constructor calls
Browse files Browse the repository at this point in the history
Otherwise, the following program causes weird address not found error:

    import scala.collection.immutable.HashSet

    object A:
      val a = HashSet(1, 2, 3)

The error looks like the following:

    -- Warning: out/bootstrap/scala2-library-bootstrapped/scala-3.6.0-RC1-bin-SNAPSHOT-nonbootstrapped/src_managed/main/scala-library-src/scala/collection/immutable/ChampCommon.scala:155:24
    155 |    currentStackLevel = currentStackLevel - 1
        |                        ^^^^^^^^^^^^^^^^^
        |[Internal error] Address not found FieldAddr(List(),val currentStackLevel,module class A$). Trace:
        |Calling trace:
        |├── object A:	[ HashSet.scala:3 ]
        |│   ^
        |├── val a = HashSet(1, 2, 3)	[ HashSet.scala:4 ]
        |│           ^^^^^^^^^^^^^^^^
        |├── def apply[A](elems: A*): CC[A] = from(elems)	[ Factory.scala:103 ]
        |│                                    ^^^^^^^^^^^
        |├── def from[A](source: collection.IterableOnce[A]): HashSet[A] =	[ HashSet.scala:1939 ]
        |│   ^
        |├── case _ => (newBuilder[A] ++= source).result()	[ HashSet.scala:1943 ]
        |│              ^^^^^^^^^^^^^^^^^^^^^^^^
        |├── @inline final def ++= (@deprecatedName("xs") elems: IterableOnce[A]): this.type = addAll(elems)	[ Growable.scala:69 ]
        |│                                                                                     ^^^^^^^^^^^^^
        |├── override def addAll(xs: IterableOnce[A]) = {	[ HashSet.scala:2079 ]
        |│   ^
        |├── }	[ HashSet.scala:2096 ]
        |│    ^
        |├── new ChampBaseIterator[A, SetNode[A]](hm.rootNode) {	[ HashSet.scala:2083 ]
        |│   ^
        |├── while(hasNext) {	[ HashSet.scala:2084 ]
        |│         ^^^^^^^
        |├── final def hasNext = (currentValueCursor < currentValueLength) || searchNextValueNode()	[ ChampCommon.scala:185 ]
        |│                                                                    ^^^^^^^^^^^^^^^^^^^^^
        |├── private final def searchNextValueNode(): Boolean = {	[ ChampCommon.scala:162 ]
        |│   ^
        |├── popNode()	[ ChampCommon.scala:178 ]
        |│   ^^^^^^^^^
        |├── private final def popNode(): Unit = {	[ ChampCommon.scala:154 ]
        |│   ^
        |└── currentStackLevel = currentStackLevel - 1	[ ChampCommon.scala:155 ]
        |                        ^^^^^^^^^^^^^^^^^
  • Loading branch information
liufengyun committed Sep 9, 2024
1 parent dafdd83 commit 5fd1d5d
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/transform/init/Objects.scala
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ class Objects(using Context @constructorOnly):
given regions: Regions.Data = Regions.empty // explicit name to avoid naming conflict

val obj = ObjectRef(classSym)
log("Iteration " + count) {
log("Iteration " + count + " for " + classSym) {
data.checkingObjects += obj
init(tpl, obj, classSym)
assert(data.checkingObjects.last.klass == classSym, "Expect = " + classSym.show + ", found = " + data.checkingObjects.last.klass)
Expand Down Expand Up @@ -699,7 +699,7 @@ class Objects(using Context @constructorOnly):
val config = Config(thisV, env, footprint)

Heap.update(footprint, changeSet = Set.empty)
val cacheResult = ctx == EvalContext.Method || ctx == EvalContext.Function || ctx == EvalContext.LazyVal
val cacheResult = ctx != EvalContext.Other
val result = super.cachedEval(config, expr, cacheResult, default = Res(Bottom, footprint, Set.empty)) { expr =>
val value = fun(expr)
val heapAfter = Heap.getHeapData()
Expand Down Expand Up @@ -966,7 +966,7 @@ class Objects(using Context @constructorOnly):
// No usage of `return` is possible in constructors --- still install
// return handler for uniform handling of method context.
Returns.installHandler(ctor)
val res = eval(tree, ref, cls, EvalContext.Method)
val res = eval(tree, ref, cls, EvalContext.Construtor)
Returns.popHandler(ctor)
res

Expand Down Expand Up @@ -1259,7 +1259,7 @@ class Objects(using Context @constructorOnly):
accessObject(classSym)

enum EvalContext:
case Method, Function, LazyVal, Other
case Method, Construtor, Function, LazyVal, Other

/** Evaluate an expression with the given value for `this` in a given class `klass`
*
Expand Down

0 comments on commit 5fd1d5d

Please sign in to comment.