Skip to content

Fix #4841: Load the compiler toolbox reflectively #4849

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 1 commit into from
Jul 27, 2018
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
12 changes: 7 additions & 5 deletions compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import dotty.tools.io.{AbstractFile, Directory, PlainDirectory, VirtualDirectory
import dotty.tools.repl.AbstractFileClassLoader

import scala.quoted.{Expr, Type}
import scala.quoted.Toolbox

import java.net.URLClassLoader

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

private[this] val contextBase: ContextBase = new ContextBase

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

def show(expr: Expr[_], settings: ToolboxSettings): String = {
def show(expr: Expr[_], settings: Toolbox.Settings): String = {
def show(tree: Tree, ctx: Context): String = {
val tree1 = if (settings.rawTree) tree else (new TreeCleaner).transform(tree)(ctx)
val tree1 = if (settings.showRawTree) tree else (new TreeCleaner).transform(tree)(ctx)
new TastyImpl(ctx).showSourceCode.showTree(tree1)(ctx)
}
withTree(expr, show, settings)
}

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

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

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

var output: Option[T] = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import scala.quoted.Expr
import scala.quoted.Exprs.{LiftedExpr, TastyTreeExpr}

/** Default runners for quoted expressions */
object Toolbox {
object ToolboxImpl {
import tpd._

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

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

Expand Down
22 changes: 0 additions & 22 deletions compiler/src/dotty/tools/dotc/quoted/ToolboxSettings.scala

This file was deleted.

50 changes: 49 additions & 1 deletion library/src/scala/quoted/Toolbox.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,56 @@ package scala.quoted

import scala.annotation.implicitNotFound

@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._`")
@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._`")
trait Toolbox {
def run[T](expr: Expr[T]): T
def show[T](expr: Expr[T]): String
}

object Toolbox {

object Default {
// TODO remove? It may be better to only have one way to instantiate the toolbox
implicit def make(implicit settings: Settings): Toolbox = Toolbox.make
}

def make(implicit settings: Settings): Toolbox = {
val cl = getClass.getClassLoader
try {
val toolboxImplCls = cl.loadClass("dotty.tools.dotc.quoted.ToolboxImpl")
val makeMeth = toolboxImplCls.getMethod("make", classOf[Settings])
makeMeth.invoke(null, settings).asInstanceOf[Toolbox]
}
catch {
case ex: ClassNotFoundException =>
throw new ToolboxNotFoundException(
s"""Could not load the Toolbox class `${ex.getMessage}` from the JVM classpath. Make sure that the compiler is on the JVM classpath.""",
ex
)
}
}

/** Setting of the Toolbox instance. */
class Settings private (val outDir: Option[String], val showRawTree: Boolean, val compilerArgs: List[String])

object Settings {

implicit def default: Settings = make()

/** Make toolbox settings
* @param outDir Output directory for the compiled quote. If set to None the output will be in memory
* @param color Print output with colors
* @param showRawTree Do not remove quote tree artifacts
* @param compilerArgs Compiler arguments. Use only if you know what you are doing.
*/
def make( // TODO avoid using default parameters (for binary compat)
color: Boolean = false,
showRawTree: Boolean = false,
outDir: Option[String] = None,
compilerArgs: List[String] = Nil
): Settings =
new Settings(outDir, showRawTree, compilerArgs)
}

class ToolboxNotFoundException(msg: String, cause: ClassNotFoundException) extends Exception(msg, cause)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import scala.quoted._

import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._

object Macros {
transparent def foo(i: => Int): Int = ~{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import scala.quoted._

import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._

object Macros {
transparent def foo(i: => Int): Int = ~{
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-with-compiler/quote-0.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import scala.quoted._

import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._

object Macros {

Expand Down
2 changes: 1 addition & 1 deletion tests/pos-with-compiler/quote-assert/quoted_2.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._
import scala.quoted._
import Macros._

Expand Down
2 changes: 1 addition & 1 deletion tests/run-with-compiler-custom-args/staged-streams_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ object Test {
.fold('{0}, ((a: Expr[Int], b : Expr[Int]) => '{ ~a + ~b }))

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

println(test1().run)
println
Expand Down
2 changes: 1 addition & 1 deletion tests/run-with-compiler/i3823-b.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._
import scala.quoted._
object Test {
def main(args: Array[String]): Unit = {
Expand Down
2 changes: 1 addition & 1 deletion tests/run-with-compiler/i3823-c.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._
import scala.quoted._
object Test {
def main(args: Array[String]): Unit = {
Expand Down
2 changes: 1 addition & 1 deletion tests/run-with-compiler/i3823.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._
import scala.quoted._
object Test {
def main(args: Array[String]): Unit = {
Expand Down
2 changes: 1 addition & 1 deletion tests/run-with-compiler/i3847-b.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._
import scala.quoted._
import scala.reflect.ClassTag

Expand Down
2 changes: 1 addition & 1 deletion tests/run-with-compiler/i3847.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._
import scala.quoted._
import scala.reflect.ClassTag

Expand Down
4 changes: 2 additions & 2 deletions tests/run-with-compiler/i3876-b.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._
import scala.quoted._
object Test {
def main(args: Array[String]): Unit = {
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make

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

Expand Down
4 changes: 2 additions & 2 deletions tests/run-with-compiler/i3876-c.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._
import scala.quoted._
object Test {
def main(args: Array[String]): Unit = {
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make

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

Expand Down
4 changes: 2 additions & 2 deletions tests/run-with-compiler/i3876-d.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._
import scala.quoted._
object Test {
def main(args: Array[String]): Unit = {
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make

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

Expand Down
4 changes: 2 additions & 2 deletions tests/run-with-compiler/i3876.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._
import scala.quoted._
object Test {
def main(args: Array[String]): Unit = {
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make

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

Expand Down
4 changes: 2 additions & 2 deletions tests/run-with-compiler/i3946.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._
import scala.quoted._
object Test {
def main(args: Array[String]): Unit = {
implicit val toolbox: scala.quoted.Toolbox = dotty.tools.dotc.quoted.Toolbox.make
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make

val u: Expr[Unit] = '()
println(u.show)
Expand Down
4 changes: 2 additions & 2 deletions tests/run-with-compiler/i3947.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

import scala.quoted._
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._

object Test {

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

def test[T](clazz: java.lang.Class[T]): Unit = {
val lclazz = clazz.toExpr
Expand Down
4 changes: 2 additions & 2 deletions tests/run-with-compiler/i3947b.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

import scala.quoted._
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._

object Test {

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

def test[T](clazz: java.lang.Class[T]): Unit = {
val lclazz = clazz.toExpr
Expand Down
4 changes: 2 additions & 2 deletions tests/run-with-compiler/i3947b2.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

import scala.quoted._
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._

object Test {

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

def test[T](clazz: java.lang.Class[T]): Unit = {
val lclazz = clazz.toExpr
Expand Down
4 changes: 2 additions & 2 deletions tests/run-with-compiler/i3947b3.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

import scala.quoted._
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._

object Test {

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

def test[T](clazz: java.lang.Class[T]): Unit = {
val lclazz = clazz.toExpr
Expand Down
4 changes: 2 additions & 2 deletions tests/run-with-compiler/i3947c.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

import scala.quoted._
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._

object Test {

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

def test[T](clazz: java.lang.Class[T]): Unit = {
val lclazz = clazz.toExpr
Expand Down
4 changes: 2 additions & 2 deletions tests/run-with-compiler/i3947d.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

import scala.quoted._
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._

object Test {

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

def test[T](clazz: java.lang.Class[T]): Unit = {
val lclazz = clazz.toExpr
Expand Down
4 changes: 2 additions & 2 deletions tests/run-with-compiler/i3947d2.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

import scala.quoted._
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._

object Test {

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

def test[T](clazz: java.lang.Class[T]): Unit = {
val lclazz = clazz.toExpr
Expand Down
4 changes: 2 additions & 2 deletions tests/run-with-compiler/i3947e.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

import scala.quoted._
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._

object Test {

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

def test[T](clazz: java.lang.Class[T]): Unit = {
val lclazz = clazz.toExpr
Expand Down
4 changes: 2 additions & 2 deletions tests/run-with-compiler/i3947f.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

import scala.quoted._
import dotty.tools.dotc.quoted.Toolbox._
import scala.quoted.Toolbox.Default._

object Test {

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

def test[T](clazz: java.lang.Class[T]): Unit = {
val lclazz = clazz.toExpr
Expand Down
Loading