Skip to content

Commit e959e89

Browse files
committed
Cache compilere driver in quote.Toolbox
* Cache the driver itself * Cache context base in the driver * Addapt toolbox settings * Use cached toolbox in tests
1 parent d82b651 commit e959e89

38 files changed

+124
-76
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
TastyImpl.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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package scala.quoted
2+
3+
import scala.annotation.implicitNotFound
4+
5+
@implicitNotFound(
6+
"""Could not find implicit quoted.Toolbox.
7+
|
8+
|Default toolbox can be instantiated with:
9+
| `implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make`
10+
|
11+
|If only needed once it can also be imported with:
12+
| `import dotty.tools.dotc.quoted.Toolbox._`
13+
""".stripMargin
14+
)
15+
trait Toolbox {
16+
def run[T](expr: Expr[T]): T
17+
def show[T](expr: Expr[T]): String
18+
}

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

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

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

tests/run-with-compiler/i3947i.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/i3947j.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/i4350.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Foo[T: Type] {
88

99
object Test {
1010
def main(args: Array[String]): Unit = {
11+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
1112
println((new Foo[Object]).q.show)
1213
println((new Foo[String]).q.show)
1314
}

tests/run-with-compiler/quote-lib.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import liftable.Exprs._
1111

1212
object Test {
1313
def main(args: Array[String]): Unit = {
14+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
1415

1516
val liftedUnit: Expr[Unit] = '()
1617

tests/run-with-compiler/quote-owners-2.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import dotty.tools.dotc.quoted.Toolbox._
44

55
object Test {
66
def main(args: Array[String]): Unit = {
7+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
8+
79
val q = f(g(Type.IntTag))
810
println(q.run)
911
println(q.show)

tests/run-with-compiler/quote-owners.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import dotty.tools.dotc.quoted.Toolbox._
33

44
object Test {
55
def main(args: Array[String]): Unit = {
6+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
7+
68
val q = f
79
println(q.run)
810
println(q.show)

tests/run-with-compiler/quote-run-2.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import scala.quoted._
55

66
object Test {
77
def main(args: Array[String]): Unit = {
8+
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
9+
810
def powerCode(n: Int, x: Expr[Double]): Expr[Double] =
911
if (n == 0) '(1.0)
1012
else if (n == 1) x

0 commit comments

Comments
 (0)