Skip to content

Commit a4b162d

Browse files
authored
Fix bug in init checker while compiling scodec-bits community project (#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]
2 parents e7763eb + 4298199 commit a4b162d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,8 @@ class Objects(using Context @constructorOnly):
523523

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

526+
def setHeap(newHeap: Data)(using mutable: MutableData): Unit = mutable.heap = newHeap
527+
526528
/** Cache used to terminate the check */
527529
object Cache:
528530
case class Config(thisV: Value, env: Env.Data, heap: Heap.Data)
@@ -538,6 +540,7 @@ class Objects(using Context @constructorOnly):
538540
val result = super.cachedEval(config, expr, cacheResult, default = Res(Bottom, Heap.getHeapData())) { expr =>
539541
Res(fun(expr), Heap.getHeapData())
540542
}
543+
Heap.setHeap(result.heap)
541544
result.value
542545
end Cache
543546

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
abstract class A {
2+
def a: Long
3+
}
4+
5+
object O {
6+
case class B() extends A {
7+
def a = 5L
8+
}
9+
case class C(a2: A) extends A {
10+
var c: Long = a2.a
11+
def a = c
12+
}
13+
def f(a: A): A = C(f(a))
14+
def g(): A = f(B())
15+
16+
val x = g()
17+
}

0 commit comments

Comments
 (0)