Skip to content

Enable optimisations in Expr.run #3830

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 3 commits into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 2 additions & 3 deletions compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import java.nio.charset.StandardCharsets

class QuoteDriver extends Driver {

def run[T](expr: Expr[T]): T = {
def run[T](expr: Expr[T], optimise: Boolean): T = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will work for now, but in the future if you want to be able to pass more options, it might make more sense to pass a Settings object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add it now to avoid future changes on the API.

val ctx: Context = initCtx.fresh
// TODO enable optimisation?
// ctx.settings.optimise.update(true)(ctx)
ctx.settings.optimise.update(optimise)(ctx)

val outDir = new VirtualDirectory("(memory)", None)

Expand Down
10 changes: 6 additions & 4 deletions compiler/src/dotty/tools/dotc/quoted/Runners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ object Runners {

implicit def runner[T]: Runner[T] = new Runner[T] {

def run(expr: Expr[T]): T = expr match {
case expr: ConstantExpr[T] => expr.value
case _ => new QuoteDriver().run(expr)
}
def run(expr: Expr[T]): T = Runners.run(expr, optimise = false)

def show(expr: Expr[T]): String = expr match {
case expr: ConstantExpr[T] =>
Expand All @@ -27,4 +24,9 @@ object Runners {
case _ => new QuoteDriver().show(expr)
}
}

def run[T](expr: Expr[T], optimise: Boolean): T = expr match {
case expr: ConstantExpr[T] => expr.value
case _ => new QuoteDriver().run(expr, optimise)
}
}
9 changes: 9 additions & 0 deletions tests/run-with-compiler/quote-run-optimise.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
val a: Int = 3
println("foo")
2.+(a)
}
foo
5
foo
5
17 changes: 17 additions & 0 deletions tests/run-with-compiler/quote-run-optimise.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

import dotty.tools.dotc.quoted.Runners._

import scala.quoted._

object Test {
def main(args: Array[String]): Unit = {
val expr = '{
val a = 3
println("foo")
2 + a
}
println(expr.show)
println(run(expr.run, optimise = true))
println(expr.run)
}
}