Skip to content

Commit 1c0bf3f

Browse files
committed
Avoid memory leaks in InlineAccessors
Make sure that `AccessProxies.accessedBy` can be GCed.
1 parent bd83faf commit 1c0bf3f

File tree

6 files changed

+19
-7
lines changed

6 files changed

+19
-7
lines changed

compiler/src/dotty/tools/dotc/CompilationUnit.scala

-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ class CompilationUnit protected (val source: SourceFile) {
5656
*/
5757
var needsQuotePickling: Boolean = false
5858

59-
/** A structure containing a temporary map for generating inline accessors */
60-
val inlineAccessors: InlineAccessors = new InlineAccessors
61-
6259
var suspended: Boolean = false
6360
var suspendedAtInliningPhase: Boolean = false
6461

compiler/src/dotty/tools/dotc/transform/Inlining.scala

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import SymUtils._
1515
import NameKinds._
1616
import dotty.tools.dotc.ast.tpd
1717
import typer.Implicits.SearchFailureType
18+
import typer.PrepareInlineable
1819

1920
import scala.collection.mutable
2021
import dotty.tools.dotc.core.Annotations._

compiler/src/dotty/tools/dotc/typer/PrepareInlineable.scala

+14-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ import dotty.tools.dotc.transform.TreeMapWithStages._
2626
object PrepareInlineable {
2727
import tpd._
2828

29+
private val InlineAccessorsKey = new Property.Key[InlineAccessors]
30+
31+
def initContext(ctx: Context): Context =
32+
ctx.fresh.setProperty(InlineAccessorsKey, new InlineAccessors)
33+
34+
def makeInlineable(tree: Tree)(using Context): Tree =
35+
ctx.property(InlineAccessorsKey).get.makeInlineable(tree)
36+
37+
def addAccessorDefs(cls: Symbol, body: List[Tree])(using Context): List[Tree] =
38+
ctx.property(InlineAccessorsKey) match
39+
case Some(inlineAccessors) => inlineAccessors.addAccessorDefs(cls, body)
40+
case _ => body
41+
2942
class InlineAccessors extends AccessProxies {
3043

3144
/** If an inline accessor name wraps a unique inline name, this is taken as indication
@@ -251,7 +264,7 @@ object PrepareInlineable {
251264
if inlined.isInlineMethod then
252265
inlinedBody = dropInlineIfError(inlined,
253266
checkInlineMethod(inlined,
254-
ctx.compilationUnit.inlineAccessors.makeInlineable(inlinedBody)))
267+
PrepareInlineable.makeInlineable(inlinedBody)))
255268
inlining.println(i"Body to inline for $inlined: $inlinedBody")
256269
inlinedBody
257270
})

compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ trait QuotesAndSplices {
7070
}
7171

7272
private def makeInlineable(tree: Tree)(using Context): Tree =
73-
ctx.compilationUnit.inlineAccessors.makeInlineable(tree)
73+
PrepareInlineable.makeInlineable(tree)
7474

7575
/** Translate `${ t: Expr[T] }` into expression `t.splice` while tracking the quotation level in the context */
7676
def typedSplice(tree: untpd.Splice, pt: Type)(using Context): Tree = {

compiler/src/dotty/tools/dotc/typer/Typer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -2461,7 +2461,7 @@ class Typer extends Namer
24612461
// 4. Polymorphic type defs override nothing.
24622462

24632463
protected def addAccessorDefs(cls: Symbol, body: List[Tree])(using Context): List[Tree] =
2464-
ctx.compilationUnit.inlineAccessors.addAccessorDefs(cls, body)
2464+
PrepareInlineable.addAccessorDefs(cls, body)
24652465

24662466
/** If this is a real class, make sure its first parent is a
24672467
* constructor call. Cannot simply use a type. Overridden in ReTyper.

compiler/src/dotty/tools/dotc/typer/TyperPhase.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ class TyperPhase(addRootImports: Boolean = true) extends Phase {
8181
override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] =
8282
val unitContexts =
8383
for unit <- units yield
84-
val newCtx = ctx.fresh.setPhase(this.start).setCompilationUnit(unit)
84+
val newCtx0 = ctx.fresh.setPhase(this.start).setCompilationUnit(unit)
85+
val newCtx = PrepareInlineable.initContext(newCtx0)
8586
report.inform(s"typing ${unit.source}")
8687
if (addRootImports)
8788
newCtx.withRootImports

0 commit comments

Comments
 (0)