From 50d7f431773ed53a4089cbd4bea6e3660069ef9d Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Mon, 15 Nov 2021 12:31:52 +0100 Subject: [PATCH] Initialize quote cache on tasty run This fixes #13919 --- .../sbt-dotty/tasty-inspector-cache/build.sbt | 24 +++++++++++++++++++ .../inspector/src/main/scala/main.scala | 14 +++++++++++ .../lib/src/main/scala/dummy.scala | 1 + sbt-test/sbt-dotty/tasty-inspector-cache/test | 1 + .../tasty/inspector/TastyInspector.scala | 7 +++++- 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 sbt-test/sbt-dotty/tasty-inspector-cache/build.sbt create mode 100644 sbt-test/sbt-dotty/tasty-inspector-cache/inspector/src/main/scala/main.scala create mode 100644 sbt-test/sbt-dotty/tasty-inspector-cache/lib/src/main/scala/dummy.scala create mode 100644 sbt-test/sbt-dotty/tasty-inspector-cache/test diff --git a/sbt-test/sbt-dotty/tasty-inspector-cache/build.sbt b/sbt-test/sbt-dotty/tasty-inspector-cache/build.sbt new file mode 100644 index 000000000000..3bece1b43fa7 --- /dev/null +++ b/sbt-test/sbt-dotty/tasty-inspector-cache/build.sbt @@ -0,0 +1,24 @@ +lazy val dottyVersion = sys.props("plugin.scalaVersion") + +lazy val lib = project + .in(file("lib")) + .settings( + scalaVersion := dottyVersion + ) + +val jarDest = file("target") / "app.jar" + +val runTest = Def.taskKey[Unit]("run tests") + +lazy val inspector = project + .in(file("inspector")) + .settings( + scalaVersion := dottyVersion, + libraryDependencies += "org.scala-lang" %% "scala3-tasty-inspector" % scalaVersion.value, + runTest := + Def.sequential( + Def.task(IO.copyFile((lib/Compile/packageBin).value, jarDest)), + (Compile/run).toTask(" " + jarDest.getAbsolutePath) + ).value + ) + .dependsOn(lib) diff --git a/sbt-test/sbt-dotty/tasty-inspector-cache/inspector/src/main/scala/main.scala b/sbt-test/sbt-dotty/tasty-inspector-cache/inspector/src/main/scala/main.scala new file mode 100644 index 000000000000..8335a016578f --- /dev/null +++ b/sbt-test/sbt-dotty/tasty-inspector-cache/inspector/src/main/scala/main.scala @@ -0,0 +1,14 @@ +import scala.quoted.Quotes +import scala.quoted.quotes +import scala.tasty.inspector as ins + +// Test for https://github.com/lampepfl/dotty/issues/13919 +class MyInspector extends ins.Inspector: + def inspect(using Quotes)(tastys: List[ins.Tasty[quotes.type]]): Unit = + import quotes.reflect._ + TypeRepr.of[Int] + TypeRepr.of[String] + + +@main def main(args: String*): Unit = + ins.TastyInspector.inspectTastyFilesInJar(args.head)(new MyInspector) diff --git a/sbt-test/sbt-dotty/tasty-inspector-cache/lib/src/main/scala/dummy.scala b/sbt-test/sbt-dotty/tasty-inspector-cache/lib/src/main/scala/dummy.scala new file mode 100644 index 000000000000..52d946a2a65d --- /dev/null +++ b/sbt-test/sbt-dotty/tasty-inspector-cache/lib/src/main/scala/dummy.scala @@ -0,0 +1 @@ +class Dummy // we need at least one file for the tasty inspector to run diff --git a/sbt-test/sbt-dotty/tasty-inspector-cache/test b/sbt-test/sbt-dotty/tasty-inspector-cache/test new file mode 100644 index 000000000000..8fa783e886e2 --- /dev/null +++ b/sbt-test/sbt-dotty/tasty-inspector-cache/test @@ -0,0 +1 @@ +> inspector/runTest diff --git a/tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala b/tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala index ab3f3e86357f..4c6440530ba2 100644 --- a/tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala +++ b/tasty-inspector/src/scala/tasty/inspector/TastyInspector.scala @@ -10,6 +10,7 @@ import dotty.tools.dotc.core.Contexts.Context import dotty.tools.dotc.core.Mode import dotty.tools.dotc.core.Phases.Phase import dotty.tools.dotc.fromtasty._ +import dotty.tools.dotc.quoted.QuotesCache import dotty.tools.dotc.util.ClasspathFromClassloader import dotty.tools.dotc.CompilationUnit import dotty.tools.unsupported @@ -64,7 +65,11 @@ object TastyInspector: class TastyInspectorPhase extends Phase: override def phaseName: String = "tastyInspector" - override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] = + override def runOn(units: List[CompilationUnit])(using ctx0: Context): List[CompilationUnit] = + val ctx = QuotesCache.init(ctx0.fresh) + runOnImpl(units)(using ctx) + + private def runOnImpl(units: List[CompilationUnit])(using Context): List[CompilationUnit] = val quotesImpl = QuotesImpl() class TastyImpl(val path: String, val ast: quotesImpl.reflect.Tree) extends Tasty[quotesImpl.type] { val quotes = quotesImpl