Skip to content

Commit 0d86d2b

Browse files
Merge pull request #6086 from dotty-staging/tasty-reflect-internal-avoid-leak
Avoid leaking ReflectImpl/KenelImpl in the compiler
2 parents d06beff + 3d47314 commit 0d86d2b

File tree

5 files changed

+13
-10
lines changed

5 files changed

+13
-10
lines changed

compiler/src/dotty/tools/dotc/consumetasty/TastyConsumerPhase.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class TastyConsumerPhase(consumer: TastyConsumer) extends Phase {
1212

1313
override def run(implicit ctx: Context): Unit = {
1414
val reflect = ReflectionImpl(ctx)
15-
consumer(reflect)(ctx.compilationUnit.tpdTree)
15+
consumer(reflect)(ctx.compilationUnit.tpdTree.asInstanceOf[reflect.Tree])
1616
}
1717

1818
}

compiler/src/dotty/tools/dotc/decompiler/DecompilationPrinter.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ class DecompilationPrinter extends Phase {
4343
} else {
4444
val unitFile = unit.source.toString.replace("\\", "/").replace(".class", ".tasty")
4545
out.println(s"/** Decompiled from $unitFile */")
46-
val refl = ReflectionImpl(ctx)
47-
out.println(new refl.SourceCodePrinter().showTree(unit.tpdTree)(ctx))
46+
out.println(ReflectionImpl.showTree(unit.tpdTree))
4847
}
4948
}
5049
}

compiler/src/dotty/tools/dotc/decompiler/IDEDecompilerDriver.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ class IDEDecompilerDriver(val settings: List[String]) extends dotc.Driver {
3535
run.printSummary()
3636
val unit = ctx.run.units.head
3737

38-
val refl = ReflectionImpl(ctx)
39-
val decompiled = new refl.SourceCodePrinter().showTree(unit.tpdTree)
38+
val decompiled = ReflectionImpl.showTree(unit.tpdTree)
4039
val tree = new TastyHTMLPrinter(unit.pickled.head._2).printContents()
4140

4241
reporter.removeBufferedMessages.foreach(message => System.err.println(message))

compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ class QuoteDriver extends Driver {
4747
val tree1 =
4848
if (ctx.settings.YshowRawQuoteTrees.value) tree
4949
else (new TreeCleaner).transform(tree)
50-
val refl = ReflectionImpl(ctx)
51-
new refl.SourceCodePrinter().showTree(tree1)
50+
ReflectionImpl.showTree(tree1)
5251
}
5352
withTree(expr, show, settings)
5453
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package dotty.tools.dotc.tastyreflect
22

3+
import dotty.tools.dotc.ast.tpd
34
import dotty.tools.dotc.core._
45
import dotty.tools.dotc.util.{SourcePosition, Spans}
56

67
object ReflectionImpl {
78

8-
def apply(rootContext: Contexts.Context): scala.tasty.Reflection { val kernel: KernelImpl } =
9+
def apply(rootContext: Contexts.Context): scala.tasty.Reflection =
910
apply(rootContext, SourcePosition(rootContext.source, Spans.NoSpan))
1011

11-
def apply(rootContext: Contexts.Context, rootPosition: SourcePosition): scala.tasty.Reflection { val kernel: KernelImpl } = {
12-
class ReflectionImpl(val kernel: KernelImpl) extends scala.tasty.Reflection
12+
def apply(rootContext: Contexts.Context, rootPosition: SourcePosition): scala.tasty.Reflection =
1313
new ReflectionImpl(new KernelImpl(rootContext, rootPosition))
14+
15+
def showTree(tree: tpd.Tree)(implicit ctx: Contexts.Context): String = {
16+
val refl = new ReflectionImpl(new KernelImpl(ctx, tree.sourcePos))
17+
new refl.SourceCodePrinter().showTree(tree)
1418
}
1519

20+
private class ReflectionImpl(val kernel: KernelImpl) extends scala.tasty.Reflection
21+
1622
}

0 commit comments

Comments
 (0)