Skip to content

Fix #3876: Implement Expr.AsFunction #3880

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 4 commits into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
11 changes: 10 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/TreeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,18 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
}
}

/** An extractor for closures with their def contained in a block. */
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rename it to closureDef and return just the def. The closure part is used nowhere.

object closureWithDef {
def unapply(tree: Tree): Option[(DefDef, Closure)] = tree match {
case Block((meth @ DefDef(nme.ANON_FUN, _, _, _, _)) :: Nil, closure: Closure) =>
Some(meth, closure)
case _ => None
}
}

/** If tree is a closure, its body, otherwise tree itself */
def closureBody(tree: Tree)(implicit ctx: Context): Tree = tree match {
case Block((meth @ DefDef(nme.ANON_FUN, _, _, _, _)) :: Nil, Closure(_, _, _)) => meth.rhs
case closureWithDef(meth, _) => meth.rhs
case _ => tree
}

Expand Down
64 changes: 49 additions & 15 deletions compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package dotty.tools.dotc.core.quoted

import dotty.tools.dotc.ast.Trees._
import dotty.tools.dotc.ast.{tpd, untpd}
import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.config.Printers._
import dotty.tools.dotc.core.Constants.Constant
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Decorators._
import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.NameKinds
import dotty.tools.dotc.core.StdNames._
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.tasty.{TastyPickler, TastyPrinter, TastyString}
Expand All @@ -33,21 +34,16 @@ object PickledQuotes {

/** Transform the expression into its fully spliced Tree */
def quotedToTree(expr: quoted.Quoted)(implicit ctx: Context): Tree = expr match {
case expr: quoted.TastyQuoted => unpickleQuote(expr)
case expr: quoted.Liftable.ConstantExpr[_] => Literal(Constant(expr.value))
case expr: quoted.TastyQuoted =>
unpickleQuote(expr)
case expr: quoted.Liftable.ConstantExpr[_] =>
Literal(Constant(expr.value))
case expr: quoted.Expr.FunctionAppliedTo[_, _] =>
functionAppliedTo(quotedToTree(expr.f), quotedToTree(expr.x))
case expr: quoted.Type.TaggedPrimitive[_] =>
val tpe = expr.ct match {
case ClassTag.Unit => defn.UnitType
case ClassTag.Byte => defn.ByteType
case ClassTag.Char => defn.CharType
case ClassTag.Short => defn.ShortType
case ClassTag.Int => defn.IntType
case ClassTag.Long => defn.LongType
case ClassTag.Float => defn.FloatType
case ClassTag.Double => defn.FloatType
}
TypeTree(tpe)
case expr: RawQuoted => expr.tree
classTagToTypeTree(expr.ct)
case expr: RawQuoted =>
expr.tree
}

/** Unpickle the tree contained in the TastyQuoted */
Expand Down Expand Up @@ -111,4 +107,42 @@ object PickledQuotes {
}
tree
}

private def classTagToTypeTree(ct: ClassTag[_])(implicit ctx: Context): TypeTree = {
val tpe = ct match {
case ClassTag.Unit => defn.UnitType
case ClassTag.Byte => defn.ByteType
case ClassTag.Char => defn.CharType
case ClassTag.Short => defn.ShortType
case ClassTag.Int => defn.IntType
case ClassTag.Long => defn.LongType
case ClassTag.Float => defn.FloatType
case ClassTag.Double => defn.FloatType
}
TypeTree(tpe)
}

private def functionAppliedTo(f: Tree, x: Tree)(implicit ctx: Context): Tree = {
val x1 = SyntheticValDef(NameKinds.UniqueName.fresh("x".toTermName), x)
def x1Ref() = ref(x1.symbol)
def rec(f: Tree): Tree = f match {
case closureWithDef(ddef, _) =>
new TreeMap() {
private val paramSym = ddef.vparamss.head.head.symbol
override def transform(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = tree match {
case tree: Ident if tree.symbol == paramSym => x1Ref().withPos(tree.pos)
case _ => super.transform(tree)
}
}.transform(ddef.rhs)
case Block(stats, expr) =>
val applied = rec(expr)
if (stats.isEmpty) applied
else Block(stats, applied)
case Inlined(call, bindings, expansion) =>
Inlined(call, bindings, rec(expansion))
case _ =>
f.select(nme.apply).appliedTo(x1Ref())
}
Block(x1 :: Nil, rec(f))
}
}
4 changes: 3 additions & 1 deletion library/src/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ object Expr {
ev.toExpr(x)

implicit class AsFunction[T, U](private val f: Expr[T => U]) extends AnyVal {
def apply(x: Expr[T]): Expr[U] = ???
def apply(x: Expr[T]): Expr[U] = new FunctionAppliedTo[T, U](f, x)
}

final class FunctionAppliedTo[T, U] private[Expr](val f: Expr[T => U], val x: Expr[T]) extends Expr[U]
}
39 changes: 39 additions & 0 deletions tests/run-with-compiler/i3876.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
6
{
val x$1: Int = 3
{
x$1.+(x$1)
}
}
6
{
val x$1: Int = 3
{
def f(x: Int): Int = x.+(x)
f(x$1)
}
}
6
{
val x$1: Int = 3
{
val f:
Function1[Int, Int]
{
def apply(x: Int): Int
}
=
{
(x: Int) => x.+(x)
}
(f: (x: Int) => Int).apply(x$1)
}
}
6
{
val x$1: Int = 3
/* inlined from Test*/
{
x$1.+(x$1)
}
}
33 changes: 33 additions & 0 deletions tests/run-with-compiler/i3876.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import dotty.tools.dotc.quoted.Runners._
import scala.quoted._
object Test {
def main(args: Array[String]): Unit = {
val x: Expr[Int] = '(3)

val f: Expr[Int => Int] = '{ (x: Int) => x + x }
println(f(x).run)
println(f(x).show)

val f2: Expr[Int => Int] = '{
def f(x: Int): Int = x + x
f
}
println(f2(x).run)
println(f2(x).show)

val f3: Expr[Int => Int] = '{
val f: (x: Int) => Int = x => x + x
f
}
println(f3(x).run)
println(f3(x).show) // TODO improve printer

val f4: Expr[Int => Int] = '{
inlineLambda
}
println(f4(x).run)
println(f4(x).show)
}

inline def inlineLambda: Int => Int = x => x + x
}