diff --git a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala index 3ecb33972d53..96d03bdaf8de 100644 --- a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala +++ b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala @@ -613,7 +613,7 @@ class Objects(using Context @constructorOnly): if !visited.contains(value) then toVisit += value - case objRef: ObjectRef => + case objRef: ObjectRef if objRef != currentObj => // Thanks to initialization-time irrelevance, there is no need to // visit the heap regions owned by other global objects. // @@ -707,7 +707,7 @@ class Objects(using Context @constructorOnly): val heapGC = if cacheResult then Heap.gc(value, footprint, heapAfter, changeSetNew) else heapAfter - Res(value, heapAfter, changeSetNew) + Res(value, heapGC, changeSetNew) } Heap.update(heapBefore ++ result.heap, changeSetBefore ++ result.changeSet) diff --git a/tests/init-global/pos/footprint-local.scala b/tests/init-global/pos/footprint-local.scala new file mode 100644 index 000000000000..954984662eeb --- /dev/null +++ b/tests/init-global/pos/footprint-local.scala @@ -0,0 +1,6 @@ +object A: + val f = foo() + val a = f() + def foo(): () => Int = + var x = 10 + () => x