Skip to content

Fix #4455: Lift inlined values when quoted #4458

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
May 4, 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
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,9 @@ class Definitions {
lazy val QuotedType_applyR = QuotedTypeModule.requiredMethodRef(nme.apply)
def QuotedType_apply(implicit ctx: Context) = QuotedType_applyR.symbol

lazy val QuotedLiftableType = ctx.requiredClassRef("scala.quoted.Liftable")
def QuotedLiftableClass(implicit ctx: Context) = QuotedLiftableType.symbol.asClass

def Unpickler_unpickleExpr = ctx.requiredMethod("scala.runtime.quoted.Unpickler.unpickleExpr")
def Unpickler_liftedExpr = ctx.requiredMethod("scala.runtime.quoted.Unpickler.liftedExpr")
def Unpickler_unpickleType = ctx.requiredMethod("scala.runtime.quoted.Unpickler.unpickleType")
Expand Down
36 changes: 30 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import dotty.tools.dotc.core.quoted._
* ...
* val y1$1 = args(1).asInstanceOf[Expr[Y]]
* ...
* { ... T1$1.unary_~ ... x ... '(y1$1.unary_~) ... }
* { ... x1$1 .... '{ ... T1$1.unary_~ ... x1$1.toExpr.unary_~ ... y1$1.unary_~ ... } ... }
* }
* ```
* Note: the parameters of `foo` are kept for simple overloading resolution but they are not used in the body of `foo`.
Expand Down Expand Up @@ -375,9 +375,15 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
}
else body match {
case body: RefTree if isCaptured(body, level + 1) =>
// Optimization: avoid the full conversion when capturing `x`
// in '{ x } to '{ x$1.unary_~ } and go directly to `x$1`
capturers(body.symbol)(body)
if (body.symbol.is(Inline)) {
// Optimization: avoid the full conversion when capturing inlined `x`
// in '{ x } to '{ x$1.toExpr.unary_~ } and go directly to `x$1.toExpr`
liftValue(capturers(body.symbol)(body))
} else {
// Optimization: avoid the full conversion when capturing `x`
// in '{ x } to '{ x$1.unary_~ } and go directly to `x$1`
capturers(body.symbol)(body)
}
case _=>
val (body1, splices) = nested(isQuote = true).split(body)
pickledQuote(body1, splices, isType).withPos(quote.pos)
Expand Down Expand Up @@ -546,8 +552,11 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
splice(tree)
case tree: RefTree if isCaptured(tree, level) =>
val capturer = capturers(tree.symbol)
if (tree.symbol.is(Inline)) capturer(tree)
else splice(capturer(tree).select(if (tree.isTerm) nme.UNARY_~ else tpnme.UNARY_~))
def captureAndSplice(t: Tree) =
splice(t.select(if (tree.isTerm) nme.UNARY_~ else tpnme.UNARY_~))
if (tree.symbol.is(Inline) && level == 0) capturer(tree)
else if (tree.symbol.is(Inline)) captureAndSplice(liftValue(capturer(tree)))
else captureAndSplice(capturer(tree))
case Block(stats, _) =>
val last = enteredSyms
stats.foreach(markDef)
Expand Down Expand Up @@ -601,6 +610,21 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
}
}

private def liftValue(tree: Tree)(implicit ctx: Context): Tree = {
val reqType = defn.QuotedLiftableType.appliedTo(tree.tpe.widen)
val liftable = ctx.typer.inferImplicitArg(reqType, tree.pos)
liftable.tpe match {
case fail: SearchFailureType =>
ctx.error(i"""
|
| The access would be accepted with the right Liftable, but
| ${ctx.typer.missingArgMsg(liftable, reqType, "")}""")
EmptyTree
case _ =>
liftable.select("toExpr".toTermName).appliedTo(tree)
}
}

private def liftList(list: List[Tree], tpe: Type)(implicit ctx: Context): Tree = {
list.foldRight[Tree](ref(defn.NilModule)) { (x, acc) =>
acc.select("::".toTermName).appliedToType(tpe).appliedTo(x)
Expand Down
4 changes: 4 additions & 0 deletions tests/run/i4455.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1
3
3
5
8 changes: 8 additions & 0 deletions tests/run/i4455/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.quoted._
object Macros {
inline def foo(inline i: Int): Int = ~bar('(i))

inline def foo2(inline i: Int): Int = ~bar('(i + 1))

def bar(x: Expr[Int]): Expr[Int] = x
}
9 changes: 9 additions & 0 deletions tests/run/i4455/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Macros._
object Test {
def main(args: Array[String]): Unit = {
println(foo(1))
println(foo(3))
println(foo2(2))
println(foo2(4))
}
}