Skip to content

Commit bfe0859

Browse files
committed
Fix #4841: Load the compiler toolbox reflectively
1 parent 73c899f commit bfe0859

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+155
-127
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import dotty.tools.io.{AbstractFile, Directory, PlainDirectory, VirtualDirectory
77
import dotty.tools.repl.AbstractFileClassLoader
88

99
import scala.quoted.{Expr, Type}
10+
import scala.quoted.Toolbox
11+
1012
import java.net.URLClassLoader
1113

1214
import dotty.tools.dotc.tastyreflect.TastyImpl
@@ -16,7 +18,7 @@ class QuoteDriver extends Driver {
1618

1719
private[this] val contextBase: ContextBase = new ContextBase
1820

19-
def run[T](expr: Expr[T], settings: ToolboxSettings): T = {
21+
def run[T](expr: Expr[T], settings: Toolbox.Settings): T = {
2022
val outDir: AbstractFile = settings.outDir match {
2123
case Some(out) =>
2224
val dir = Directory(out)
@@ -41,15 +43,15 @@ class QuoteDriver extends Driver {
4143
method.invoke(instance).asInstanceOf[T]
4244
}
4345

44-
def show(expr: Expr[_], settings: ToolboxSettings): String = {
46+
def show(expr: Expr[_], settings: Toolbox.Settings): String = {
4547
def show(tree: Tree, ctx: Context): String = {
46-
val tree1 = if (settings.rawTree) tree else (new TreeCleaner).transform(tree)(ctx)
48+
val tree1 = if (settings.showRawTree) tree else (new TreeCleaner).transform(tree)(ctx)
4749
new TastyImpl(ctx).showSourceCode.showTree(tree1)(ctx)
4850
}
4951
withTree(expr, show, settings)
5052
}
5153

52-
def withTree[T](expr: Expr[_], f: (Tree, Context) => T, settings: ToolboxSettings): T = {
54+
def withTree[T](expr: Expr[_], f: (Tree, Context) => T, settings: Toolbox.Settings): T = {
5355
val (_, ctx: Context) = setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)
5456

5557
var output: Option[T] = None
@@ -61,7 +63,7 @@ class QuoteDriver extends Driver {
6163
output.getOrElse(throw new Exception("Could not extract " + expr))
6264
}
6365

64-
def withTypeTree[T](tpe: Type[_], f: (TypTree, Context) => T, settings: ToolboxSettings): T = {
66+
def withTypeTree[T](tpe: Type[_], f: (TypTree, Context) => T, settings: Toolbox.Settings): T = {
6567
val (_, ctx: Context) = setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)
6668

6769
var output: Option[T] = None

compiler/src/dotty/tools/dotc/quoted/Toolbox.scala renamed to compiler/src/dotty/tools/dotc/quoted/ToolboxImpl.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import scala.quoted.Expr
66
import scala.quoted.Exprs.{LiftedExpr, TastyTreeExpr}
77

88
/** Default runners for quoted expressions */
9-
object Toolbox {
9+
object ToolboxImpl {
1010
import tpd._
1111

12-
implicit def make(implicit settings: ToolboxSettings): scala.quoted.Toolbox = new scala.quoted.Toolbox {
12+
def make(settings: scala.quoted.Toolbox.Settings): scala.quoted.Toolbox = new scala.quoted.Toolbox {
1313

1414
private[this] val driver: QuoteDriver = new QuoteDriver()
1515

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

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

library/src/scala/quoted/Toolbox.scala

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,56 @@ package scala.quoted
22

33
import scala.annotation.implicitNotFound
44

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._`")
5+
@implicitNotFound("Could not find implicit quoted.Toolbox.\n\nDefault toolbox can be instantiated with:\n `implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make`\n\nIf only needed once it can also be imported with:\n `import scala.quoted.Toolbox.Default._`")
66
trait Toolbox {
77
def run[T](expr: Expr[T]): T
88
def show[T](expr: Expr[T]): String
99
}
10+
11+
object Toolbox {
12+
13+
object Default {
14+
// TODO remove? It may be better to only have one way to instantiate the toolbox
15+
implicit def make(implicit settings: Settings): Toolbox = Toolbox.make
16+
}
17+
18+
def make(implicit settings: Settings): Toolbox = {
19+
val cl = getClass.getClassLoader
20+
try {
21+
val toolboxImplCls = cl.loadClass("dotty.tools.dotc.quoted.ToolboxImpl")
22+
val makeMeth = toolboxImplCls.getMethod("make", classOf[Settings])
23+
makeMeth.invoke(null, settings).asInstanceOf[Toolbox]
24+
}
25+
catch {
26+
case ex: ClassNotFoundException =>
27+
throw new ToolboxNotFoundException(
28+
s"""Could not load the Toolbox class `${ex.getMessage}` in the classpath. Make sure that the compiler is on the classpath.""",
29+
ex
30+
)
31+
}
32+
}
33+
34+
/** Setting of the Toolbox instance. */
35+
class Settings private (val outDir: Option[String], val showRawTree: Boolean, val compilerArgs: List[String])
36+
37+
object Settings {
38+
39+
implicit def default: Settings = make()
40+
41+
/** Make toolbox settings
42+
* @param outDir Output directory for the compiled quote. If set to None the output will be in memory
43+
* @param color Print output with colors
44+
* @param showRawTree Do not remove quote tree artifacts
45+
* @param compilerArgs Compiler arguments. Use only if you know what you are doing.
46+
*/
47+
def make( // TODO avoid using default parameters (for binary compat)
48+
color: Boolean = false,
49+
showRawTree: Boolean = false,
50+
outDir: Option[String] = None,
51+
compilerArgs: List[String] = Nil
52+
): Settings =
53+
new Settings(outDir, showRawTree, compilerArgs)
54+
}
55+
56+
class ToolboxNotFoundException(msg: String, cause: ClassNotFoundException) extends Exception(msg, cause)
57+
}

tests/neg/quote-run-in-macro-1/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import scala.quoted._
22

3-
import dotty.tools.dotc.quoted.Toolbox._
3+
import scala.quoted.Toolbox.Default._
44

55
object Macros {
66
inline def foo(i: => Int): Int = ~{

tests/neg/quote-run-in-macro-2/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import scala.quoted._
22

3-
import dotty.tools.dotc.quoted.Toolbox._
3+
import scala.quoted.Toolbox.Default._
44

55
object Macros {
66
inline def foo(i: => Int): Int = ~{

tests/pos/quote-0.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import scala.quoted._
22

3-
import dotty.tools.dotc.quoted.Toolbox._
3+
import scala.quoted.Toolbox.Default._
44

55
object Macros {
66

tests/pos/quote-assert/quoted_2.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import dotty.tools.dotc.quoted.Toolbox._
1+
import scala.quoted.Toolbox.Default._
22
import scala.quoted._
33
import Macros._
44

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ object Test {
673673
.fold('{0}, ((a: Expr[Int], b : Expr[Int]) => '{ ~a + ~b }))
674674

675675
def main(args: Array[String]): Unit = {
676-
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
676+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make
677677

678678
println(test1().run)
679679
println

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import dotty.tools.dotc.quoted.Toolbox._
1+
import scala.quoted.Toolbox.Default._
22
import scala.quoted._
33
object Test {
44
def main(args: Array[String]): Unit = {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import dotty.tools.dotc.quoted.Toolbox._
1+
import scala.quoted.Toolbox.Default._
22
import scala.quoted._
33
object Test {
44
def main(args: Array[String]): Unit = {

tests/run-with-compiler/i3823.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import dotty.tools.dotc.quoted.Toolbox._
1+
import scala.quoted.Toolbox.Default._
22
import scala.quoted._
33
object Test {
44
def main(args: Array[String]): Unit = {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import dotty.tools.dotc.quoted.Toolbox._
1+
import scala.quoted.Toolbox.Default._
22
import scala.quoted._
33
import scala.reflect.ClassTag
44

tests/run-with-compiler/i3847.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import dotty.tools.dotc.quoted.Toolbox._
1+
import scala.quoted.Toolbox.Default._
22
import scala.quoted._
33
import scala.reflect.ClassTag
44

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import dotty.tools.dotc.quoted.Toolbox._
1+
import scala.quoted.Toolbox.Default._
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
5+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make
66

77
val x: Expr[Int] = '(3)
88

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import dotty.tools.dotc.quoted.Toolbox._
1+
import scala.quoted.Toolbox.Default._
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
5+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make
66

77
val x: Expr[Int] = '(3)
88

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import dotty.tools.dotc.quoted.Toolbox._
1+
import scala.quoted.Toolbox.Default._
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
5+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make
66

77
val x: Expr[Int] = '(3)
88

tests/run-with-compiler/i3876.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import dotty.tools.dotc.quoted.Toolbox._
1+
import scala.quoted.Toolbox.Default._
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
5+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make
66

77
val x: Expr[Int] = '(3)
88

tests/run-with-compiler/i3946.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import dotty.tools.dotc.quoted.Toolbox._
1+
import scala.quoted.Toolbox.Default._
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
5+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make
66

77
val u: Expr[Unit] = '()
88
println(u.show)

tests/run-with-compiler/i3947.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
import scala.quoted._
3-
import dotty.tools.dotc.quoted.Toolbox._
3+
import scala.quoted.Toolbox.Default._
44

55
object Test {
66

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

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

tests/run-with-compiler/i3947b.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
import scala.quoted._
3-
import dotty.tools.dotc.quoted.Toolbox._
3+
import scala.quoted.Toolbox.Default._
44

55
object Test {
66

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

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

tests/run-with-compiler/i3947b2.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
import scala.quoted._
3-
import dotty.tools.dotc.quoted.Toolbox._
3+
import scala.quoted.Toolbox.Default._
44

55
object Test {
66

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

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

tests/run-with-compiler/i3947b3.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
import scala.quoted._
3-
import dotty.tools.dotc.quoted.Toolbox._
3+
import scala.quoted.Toolbox.Default._
44

55
object Test {
66

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

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

tests/run-with-compiler/i3947c.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
import scala.quoted._
3-
import dotty.tools.dotc.quoted.Toolbox._
3+
import scala.quoted.Toolbox.Default._
44

55
object Test {
66

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

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

tests/run-with-compiler/i3947d.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
import scala.quoted._
3-
import dotty.tools.dotc.quoted.Toolbox._
3+
import scala.quoted.Toolbox.Default._
44

55
object Test {
66

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

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

tests/run-with-compiler/i3947d2.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
import scala.quoted._
3-
import dotty.tools.dotc.quoted.Toolbox._
3+
import scala.quoted.Toolbox.Default._
44

55
object Test {
66

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

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

tests/run-with-compiler/i3947e.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
import scala.quoted._
3-
import dotty.tools.dotc.quoted.Toolbox._
3+
import scala.quoted.Toolbox.Default._
44

55
object Test {
66

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

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

tests/run-with-compiler/i3947f.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
import scala.quoted._
3-
import dotty.tools.dotc.quoted.Toolbox._
3+
import scala.quoted.Toolbox.Default._
44

55
object Test {
66

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

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

0 commit comments

Comments
 (0)