Skip to content

Commit 38d07d9

Browse files
Merge pull request #4643 from dotty-staging/cache-tasty-driver
Cache context base base in quote driver
2 parents 69b539c + f781701 commit 38d07d9

40 files changed

+147
-93
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ package dotty.tools.dotc.quoted
22

33
import dotty.tools.dotc.ast.tpd
44
import dotty.tools.dotc.Driver
5-
import dotty.tools.dotc.core.Contexts.Context
5+
import dotty.tools.dotc.core.Contexts.{Context, ContextBase}
66
import dotty.tools.io.{AbstractFile, Directory, PlainDirectory, VirtualDirectory}
77
import dotty.tools.repl.AbstractFileClassLoader
88

99
import scala.quoted.{Expr, Type}
1010
import java.net.URLClassLoader
1111

12-
import Toolbox.{Run, Settings, Show}
1312
import dotty.tools.dotc.tastyreflect.TastyImpl
1413

1514
class QuoteDriver extends Driver {
1615
import tpd._
1716

18-
def run[T](expr: Expr[T], settings: Settings[Run]): T = {
17+
private[this] val contextBase: ContextBase = new ContextBase
18+
19+
def run[T](expr: Expr[T], settings: ToolboxSettings): T = {
1920
val (_, ctx: Context) = setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)
2021

2122
val outDir: AbstractFile = settings.outDir match {
@@ -39,15 +40,15 @@ class QuoteDriver extends Driver {
3940
method.invoke(instance).asInstanceOf[T]
4041
}
4142

42-
def show(expr: Expr[_], settings: Settings[Show]): String = {
43+
def show(expr: Expr[_], settings: ToolboxSettings): String = {
4344
def show(tree: Tree, ctx: Context): String = {
4445
val tree1 = if (settings.rawTree) tree else (new TreeCleaner).transform(tree)(ctx)
4546
new TastyImpl(ctx).showSourceCode.showTree(tree1)(ctx)
4647
}
4748
withTree(expr, show, settings)
4849
}
4950

50-
def withTree[T](expr: Expr[_], f: (Tree, Context) => T, settings: Settings[_]): T = {
51+
def withTree[T](expr: Expr[_], f: (Tree, Context) => T, settings: ToolboxSettings): T = {
5152
val (_, ctx: Context) = setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)
5253

5354
var output: Option[T] = None
@@ -59,7 +60,7 @@ class QuoteDriver extends Driver {
5960
output.getOrElse(throw new Exception("Could not extract " + expr))
6061
}
6162

62-
def withTypeTree[T](tpe: Type[_], f: (TypTree, Context) => T, settings: Settings[_]): T = {
63+
def withTypeTree[T](tpe: Type[_], f: (TypTree, Context) => T, settings: ToolboxSettings): T = {
6364
val (_, ctx: Context) = setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)
6465

6566
var output: Option[T] = None
@@ -72,7 +73,7 @@ class QuoteDriver extends Driver {
7273
}
7374

7475
override def initCtx: Context = {
75-
val ictx = super.initCtx.fresh
76+
val ictx = contextBase.initialCtx
7677
var classpath = System.getProperty("java.class.path")
7778
this.getClass.getClassLoader match {
7879
case cl: URLClassLoader =>

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

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,25 @@ import dotty.tools.dotc.ast.tpd
44

55
import scala.quoted.Expr
66
import scala.quoted.Exprs.{LiftedExpr, TastyTreeExpr}
7-
import scala.runtime.quoted._
87

98
/** Default runners for quoted expressions */
109
object Toolbox {
1110
import tpd._
1211

13-
type Run
14-
type Show
12+
implicit def make(implicit settings: ToolboxSettings): scala.quoted.Toolbox = new scala.quoted.Toolbox {
1513

16-
implicit def toolbox[T](implicit
17-
runSettings: Settings[Run] = Settings.run(),
18-
showSettings: Settings[Show] = Settings.show()
19-
): Toolbox[T] = new Toolbox[T] {
14+
private[this] val driver: QuoteDriver = new QuoteDriver()
2015

21-
def run(expr: Expr[T]): T = expr match {
16+
def run[T](expr: Expr[T]): T = expr match {
2217
case expr: LiftedExpr[T] =>
2318
expr.value
2419
case expr: TastyTreeExpr[Tree] @unchecked =>
2520
throw new Exception("Cannot call `Expr.run` on an `Expr` that comes from an inline macro argument.")
2621
case _ =>
27-
new QuoteDriver().run(expr, runSettings)
22+
driver.run(expr, settings)
2823
}
2924

30-
def show(expr: Expr[T]): String = new QuoteDriver().show(expr, showSettings)
25+
def show[T](expr: Expr[T]): String = driver.show(expr, settings)
3126

3227
}
33-
34-
class Settings[T] private (val outDir: Option[String], val rawTree: Boolean, val compilerArgs: List[String])
35-
36-
object Settings {
37-
38-
/** Quote run settings
39-
* @param optimise Enable optimisation when compiling the quoted code
40-
* @param outDir Output directory for the compiled quote. If set to None the output will be in memory
41-
* @param compilerArgs Compiler arguments. Use only if you know what you are doing.
42-
*/
43-
def run(
44-
optimise: Boolean = false,
45-
outDir: Option[String] = None,
46-
compilerArgs: List[String] = Nil
47-
): Settings[Run] = {
48-
var compilerArgs1 = compilerArgs
49-
if (optimise) compilerArgs1 = "-optimise" :: compilerArgs1
50-
new Settings(outDir, false, compilerArgs1)
51-
}
52-
53-
/** Quote show settings
54-
* @param color Print output with colors
55-
* @param rawTree Do not remove quote tree artifacts
56-
* @param compilerArgs Compiler arguments. Use only if you know what you are doing.
57-
*/
58-
def show(
59-
color: Boolean = false,
60-
rawTree: Boolean = false,
61-
compilerArgs: List[String] = Nil
62-
): Settings[Show] = {
63-
var compilerArgs1 = compilerArgs
64-
compilerArgs1 = s"-color:${if (color) "always" else "never"}" :: compilerArgs1
65-
new Settings(None, rawTree, compilerArgs1)
66-
}
67-
68-
}
69-
7028
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dotty.tools.dotc.quoted
2+
3+
class ToolboxSettings private (val outDir: Option[String], val rawTree: Boolean, val compilerArgs: List[String])
4+
5+
object ToolboxSettings {
6+
7+
implicit def default: ToolboxSettings = make()
8+
9+
/** Make toolbox settings
10+
* @param optimise Enable optimisation when compiling the quoted code
11+
* @param outDir Output directory for the compiled quote. If set to None the output will be in memory
12+
* @param color Print output with colors
13+
* @param rawTree Do not remove quote tree artifacts
14+
* @param compilerArgs Compiler arguments. Use only if you know what you are doing.
15+
*/
16+
def make(
17+
optimise: Boolean = false,
18+
color: Boolean = false,
19+
rawTree: Boolean = false,
20+
outDir: Option[String] = None,
21+
compilerArgs: List[String] = Nil
22+
): ToolboxSettings = {
23+
var compilerArgs1 = compilerArgs
24+
if (optimise) compilerArgs1 = "-optimise" :: compilerArgs1
25+
new ToolboxSettings(outDir, rawTree, compilerArgs1)
26+
}
27+
28+
}

library/src/scala/quoted/Expr.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package scala.quoted
22

3-
import scala.runtime.quoted.Toolbox
43
import scala.runtime.quoted.Unpickler.Pickled
54

65
sealed abstract class Expr[T] {
@@ -10,10 +9,10 @@ sealed abstract class Expr[T] {
109
*
1110
* May throw a FreeVariableError on expressions that came from an inline macro.
1211
*/
13-
final def run(implicit toolbox: Toolbox[T]): T = toolbox.run(this)
12+
final def run(implicit toolbox: Toolbox): T = toolbox.run(this)
1413

1514
/** Show a source code like representation of this expression */
16-
final def show(implicit toolbox: Toolbox[T]): String = toolbox.show(this)
15+
final def show(implicit toolbox: Toolbox): String = toolbox.show(this)
1716
}
1817

1918
object Expr {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package scala.quoted
2+
3+
import scala.annotation.implicitNotFound
4+
5+
@implicitNotFound("Could not find implicit quoted.Toolbox.\n\nDefault toolbox can be instantiated with:\n `implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make`\n\nIf only needed once it can also be imported with:\n `import dotty.tools.dotc.quoted.Toolbox._`")
6+
trait Toolbox {
7+
def run[T](expr: Expr[T]): T
8+
def show[T](expr: Expr[T]): String
9+
}

library/src/scala/runtime/quoted/Toolbox.scala

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/run-with-compiler-custom-args/staged-streams_1.check

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,18 @@
22

33
12
44

5+
36
6+
7+
2
8+
9+
3
10+
11+
7
12+
13+
12
14+
15+
15
16+
17+
15
18+
19+
72

tests/run-with-compiler-custom-args/staged-streams_1.scala

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import dotty.tools.dotc.quoted.Toolbox._
21
import scala.quoted._
32

43
/**
@@ -674,26 +673,27 @@ object Test {
674673
.fold('{0}, ((a: Expr[Int], b : Expr[Int]) => '{ ~a + ~b }))
675674

676675
def main(args: Array[String]): Unit = {
676+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
677+
677678
println(test1().run)
678679
println
679680
println(test2().run)
680681
println
681-
// FIXME re-enable in #4643 when we can cache the compiler context
682-
// println(test3().run)
683-
// println
684-
// println(test4().run)
685-
// println
686-
// println(test5().run)
687-
// println
688-
// println(test6().run)
689-
// println
690-
// println(test7().run)
691-
// println
692-
// println(test8().run)
693-
// println
694-
// println(test9().run)
695-
// println
696-
// println(test10().run)
682+
println(test3().run)
683+
println
684+
println(test4().run)
685+
println
686+
println(test5().run)
687+
println
688+
println(test6().run)
689+
println
690+
println(test7().run)
691+
println
692+
println(test8().run)
693+
println
694+
println(test9().run)
695+
println
696+
println(test10().run)
697697
}
698698
}
699699

tests/run-with-compiler/i3876-b.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import dotty.tools.dotc.quoted.Toolbox._
22
import scala.quoted._
33
object Test {
44
def main(args: Array[String]): Unit = {
5+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
6+
57
val x: Expr[Int] = '(3)
68

79
val f2: Expr[Int => Int] = '{

tests/run-with-compiler/i3876-c.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import dotty.tools.dotc.quoted.Toolbox._
22
import scala.quoted._
33
object Test {
44
def main(args: Array[String]): Unit = {
5+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
6+
57
val x: Expr[Int] = '(3)
68

79
val f3: Expr[Int => Int] = '{

tests/run-with-compiler/i3876-d.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import dotty.tools.dotc.quoted.Toolbox._
22
import scala.quoted._
33
object Test {
44
def main(args: Array[String]): Unit = {
5+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
6+
57
val x: Expr[Int] = '(3)
68

79
val f4: Expr[Int => Int] = '{

tests/run-with-compiler/i3876.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import dotty.tools.dotc.quoted.Toolbox._
22
import scala.quoted._
33
object Test {
44
def main(args: Array[String]): Unit = {
5+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
6+
57
val x: Expr[Int] = '(3)
68

79
val f: Expr[Int => Int] = '{ (x: Int) => x + x }

tests/run-with-compiler/i3946.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import dotty.tools.dotc.quoted.Toolbox._
22
import scala.quoted._
33
object Test {
44
def main(args: Array[String]): Unit = {
5+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
6+
57
val u: Expr[Unit] = '()
68
println(u.show)
79
println(u.run)

tests/run-with-compiler/i3947.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dotty.tools.dotc.quoted.Toolbox._
55
object Test {
66

77
def main(args: Array[String]): Unit = {
8+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
89

910
def test[T](clazz: java.lang.Class[T]): Unit = {
1011
val lclazz = clazz.toExpr

tests/run-with-compiler/i3947b.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dotty.tools.dotc.quoted.Toolbox._
55
object Test {
66

77
def main(args: Array[String]): Unit = {
8+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
89

910
def test[T](clazz: java.lang.Class[T]): Unit = {
1011
val lclazz = clazz.toExpr

tests/run-with-compiler/i3947b2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dotty.tools.dotc.quoted.Toolbox._
55
object Test {
66

77
def main(args: Array[String]): Unit = {
8+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
89

910
def test[T](clazz: java.lang.Class[T]): Unit = {
1011
val lclazz = clazz.toExpr

tests/run-with-compiler/i3947b3.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dotty.tools.dotc.quoted.Toolbox._
55
object Test {
66

77
def main(args: Array[String]): Unit = {
8+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
89

910
def test[T](clazz: java.lang.Class[T]): Unit = {
1011
val lclazz = clazz.toExpr

tests/run-with-compiler/i3947c.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dotty.tools.dotc.quoted.Toolbox._
55
object Test {
66

77
def main(args: Array[String]): Unit = {
8+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
89

910
def test[T](clazz: java.lang.Class[T]): Unit = {
1011
val lclazz = clazz.toExpr

tests/run-with-compiler/i3947d.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dotty.tools.dotc.quoted.Toolbox._
55
object Test {
66

77
def main(args: Array[String]): Unit = {
8+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
89

910
def test[T](clazz: java.lang.Class[T]): Unit = {
1011
val lclazz = clazz.toExpr

tests/run-with-compiler/i3947d2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dotty.tools.dotc.quoted.Toolbox._
55
object Test {
66

77
def main(args: Array[String]): Unit = {
8+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
89

910
def test[T](clazz: java.lang.Class[T]): Unit = {
1011
val lclazz = clazz.toExpr

tests/run-with-compiler/i3947e.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dotty.tools.dotc.quoted.Toolbox._
55
object Test {
66

77
def main(args: Array[String]): Unit = {
8+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
89

910
def test[T](clazz: java.lang.Class[T]): Unit = {
1011
val lclazz = clazz.toExpr

tests/run-with-compiler/i3947f.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dotty.tools.dotc.quoted.Toolbox._
55
object Test {
66

77
def main(args: Array[String]): Unit = {
8+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
89

910
def test[T](clazz: java.lang.Class[T]): Unit = {
1011
val lclazz = clazz.toExpr

tests/run-with-compiler/i3947g.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dotty.tools.dotc.quoted.Toolbox._
55
object Test {
66

77
def main(args: Array[String]): Unit = {
8+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
89

910
def test[T](clazz: java.lang.Class[T]): Unit = {
1011
val lclazz = clazz.toExpr

0 commit comments

Comments
 (0)