diff --git a/compiler/src/dotty/tools/dotc/quoted/Toolbox.scala b/compiler/src/dotty/tools/dotc/quoted/Toolbox.scala index b5decbd5b6f4..2462d5e5967c 100644 --- a/compiler/src/dotty/tools/dotc/quoted/Toolbox.scala +++ b/compiler/src/dotty/tools/dotc/quoted/Toolbox.scala @@ -17,11 +17,26 @@ object Toolbox { type Run type Show - implicit def toolbox[T]: Toolbox[T] = new Toolbox[T] { - - def run(expr: Expr[T]): T = Toolbox.run(expr, Settings.run()) + implicit def toolbox[T](implicit + runSettings: Settings[Run] = Settings.run(), + showSettings: Settings[Show] = Settings.show() + ): Toolbox[T] = new Toolbox[T] { + + def run(expr: Expr[T]): T = expr match { + case expr: ValueExpr[T] => expr.value + case _ => new QuoteDriver().run(expr, runSettings) + } - def show(expr: Expr[T]): String = Toolbox.show(expr, Settings.show()) + def show(expr: Expr[T]): String = expr match { + case expr: ValueExpr[T] => + implicit val ctx = new QuoteDriver().initCtx + if (showSettings.compilerArgs.contains("-color:never")) + ctx.settings.color.update("never") + val printer = new RefinedPrinter(ctx) + if (expr.value == BoxedUnit.UNIT) "()" + else printer.toText(Literal(Constant(expr.value))).mkString(Int.MaxValue, false) + case _ => new QuoteDriver().show(expr, showSettings) + } def toConstantOpt(expr: Expr[T]): Option[T] = { def toConstantOpt(tree: Tree): Option[T] = tree match { @@ -38,22 +53,6 @@ object Toolbox { } - def run[T](expr: Expr[T], settings: Settings[Run]): T = expr match { - case expr: ValueExpr[T] => expr.value - case _ => new QuoteDriver().run(expr, settings) - } - - def show[T](expr: Expr[T], settings: Settings[Show]): String = expr match { - case expr: ValueExpr[T] => - implicit val ctx = new QuoteDriver().initCtx - if (settings.compilerArgs.contains("-color:never")) - ctx.settings.color.update("never") - val printer = new RefinedPrinter(ctx) - if (expr.value == BoxedUnit.UNIT) "()" - else printer.toText(Literal(Constant(expr.value))).mkString(Int.MaxValue, false) - case _ => new QuoteDriver().show(expr, settings) - } - class Settings[T] private (val outDir: Option[String], val compilerArgs: List[String]) object Settings { diff --git a/tests/run-with-compiler/quote-run-with-settings.scala b/tests/run-with-compiler/quote-run-with-settings.scala index c40fb0c4cab4..770a51901e9a 100644 --- a/tests/run-with-compiler/quote-run-with-settings.scala +++ b/tests/run-with-compiler/quote-run-with-settings.scala @@ -21,9 +21,10 @@ object Test { Files.deleteIfExists(classFile) - val settings = Settings.run(optimise = true, outDir = Some(outDir.toString)) - - println(run(expr, settings)) - assert(Files.exists(classFile)) + { + implicit val settings = Settings.run(optimise = true, outDir = Some(outDir.toString)) + println(expr.run) + assert(Files.exists(classFile)) + } } }