Skip to content

Commit

Permalink
Fix bug in init checker while compiling scodec-bits community project (
Browse files Browse the repository at this point in the history
…#21574)

This PR fixes a bug related to heap management in the global
initialization checker that occurs when compiling the scodec-bits
community project with the -Ysafe-init-global flag.

[test_scala2_library_tasty]
  • Loading branch information
olhotak authored Sep 13, 2024
2 parents e7763eb + 4298199 commit a4b162d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/init/Objects.scala
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ class Objects(using Context @constructorOnly):

def getHeapData()(using mutable: MutableData): Data = mutable.heap

def setHeap(newHeap: Data)(using mutable: MutableData): Unit = mutable.heap = newHeap

/** Cache used to terminate the check */
object Cache:
case class Config(thisV: Value, env: Env.Data, heap: Heap.Data)
Expand All @@ -538,6 +540,7 @@ class Objects(using Context @constructorOnly):
val result = super.cachedEval(config, expr, cacheResult, default = Res(Bottom, Heap.getHeapData())) { expr =>
Res(fun(expr), Heap.getHeapData())
}
Heap.setHeap(result.heap)
result.value
end Cache

Expand Down
17 changes: 17 additions & 0 deletions tests/init-global/pos/scodec-bits.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
abstract class A {
def a: Long
}

object O {
case class B() extends A {
def a = 5L
}
case class C(a2: A) extends A {
var c: Long = a2.a
def a = c
}
def f(a: A): A = C(f(a))
def g(): A = f(B())

val x = g()
}

0 comments on commit a4b162d

Please sign in to comment.