Skip to content

Avoid leaking ReflectImpl/KenelImpl in the compiler #6086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TastyConsumerPhase(consumer: TastyConsumer) extends Phase {

override def run(implicit ctx: Context): Unit = {
val reflect = ReflectionImpl(ctx)
consumer(reflect)(ctx.compilationUnit.tpdTree)
consumer(reflect)(ctx.compilationUnit.tpdTree.asInstanceOf[reflect.Tree])
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class DecompilationPrinter extends Phase {
} else {
val unitFile = unit.source.toString.replace("\\", "/").replace(".class", ".tasty")
out.println(s"/** Decompiled from $unitFile */")
val refl = ReflectionImpl(ctx)
out.println(new refl.SourceCodePrinter().showTree(unit.tpdTree)(ctx))
out.println(ReflectionImpl.showTree(unit.tpdTree))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class IDEDecompilerDriver(val settings: List[String]) extends dotc.Driver {
run.printSummary()
val unit = ctx.run.units.head

val refl = ReflectionImpl(ctx)
val decompiled = new refl.SourceCodePrinter().showTree(unit.tpdTree)
val decompiled = ReflectionImpl.showTree(unit.tpdTree)
val tree = new TastyHTMLPrinter(unit.pickled.head._2).printContents()

reporter.removeBufferedMessages.foreach(message => System.err.println(message))
Expand Down
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class QuoteDriver extends Driver {
val tree1 =
if (ctx.settings.YshowRawQuoteTrees.value) tree
else (new TreeCleaner).transform(tree)
val refl = ReflectionImpl(ctx)
new refl.SourceCodePrinter().showTree(tree1)
ReflectionImpl.showTree(tree1)
}
withTree(expr, show, settings)
}
Expand Down
12 changes: 9 additions & 3 deletions compiler/src/dotty/tools/dotc/tastyreflect/ReflectionImpl.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package dotty.tools.dotc.tastyreflect

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.core._
import dotty.tools.dotc.util.{SourcePosition, Spans}

object ReflectionImpl {

def apply(rootContext: Contexts.Context): scala.tasty.Reflection { val kernel: KernelImpl } =
def apply(rootContext: Contexts.Context): scala.tasty.Reflection =
apply(rootContext, SourcePosition(rootContext.source, Spans.NoSpan))

def apply(rootContext: Contexts.Context, rootPosition: SourcePosition): scala.tasty.Reflection { val kernel: KernelImpl } = {
class ReflectionImpl(val kernel: KernelImpl) extends scala.tasty.Reflection
def apply(rootContext: Contexts.Context, rootPosition: SourcePosition): scala.tasty.Reflection =
new ReflectionImpl(new KernelImpl(rootContext, rootPosition))

def showTree(tree: tpd.Tree)(implicit ctx: Contexts.Context): String = {
val refl = new ReflectionImpl(new KernelImpl(ctx, tree.sourcePos))
new refl.SourceCodePrinter().showTree(tree)
}

private class ReflectionImpl(val kernel: KernelImpl) extends scala.tasty.Reflection

}