Skip to content

Commit d28ba06

Browse files
Merge pull request #4777 from dotty-staging/fix-inline-parameters-lifted-in-macro
Fix inline parameters lifted for 0 to 1
2 parents 346b8fa + b3d3834 commit d28ba06

File tree

6 files changed

+50
-16
lines changed

6 files changed

+50
-16
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,19 @@ class Definitions {
655655
lazy val QuotedType_applyR = QuotedTypeModule.requiredMethodRef(nme.apply)
656656
def QuotedType_apply(implicit ctx: Context) = QuotedType_applyR.symbol
657657

658+
lazy val QuotedLiftableModule = ctx.requiredModule("scala.quoted.Liftable")
659+
def QuotedLiftableModuleClass(implicit ctx: Context) = QuotedLiftableModule.asClass
660+
661+
def QuotedLiftable_BooleanIsLiftable = QuotedLiftableModule.requiredMethodRef("BooleanIsLiftable")
662+
def QuotedLiftable_ByteIsLiftable = QuotedLiftableModule.requiredMethodRef("ByteIsLiftable")
663+
def QuotedLiftable_CharIsLiftable = QuotedLiftableModule.requiredMethodRef("CharIsLiftable")
664+
def QuotedLiftable_ShortIsLiftable = QuotedLiftableModule.requiredMethodRef("ShortIsLiftable")
665+
def QuotedLiftable_IntIsLiftable = QuotedLiftableModule.requiredMethodRef("IntIsLiftable")
666+
def QuotedLiftable_LongIsLiftable = QuotedLiftableModule.requiredMethodRef("LongIsLiftable")
667+
def QuotedLiftable_FloatIsLiftable = QuotedLiftableModule.requiredMethodRef("FloatIsLiftable")
668+
def QuotedLiftable_DoubleIsLiftable = QuotedLiftableModule.requiredMethodRef("DoubleIsLiftable")
669+
def QuotedLiftable_StringIsLiftable = QuotedLiftableModule.requiredMethodRef("StringIsLiftable")
670+
658671
lazy val QuotedLiftableType = ctx.requiredClassRef("scala.quoted.Liftable")
659672
def QuotedLiftableClass(implicit ctx: Context) = QuotedLiftableType.symbol.asClass
660673

compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
387387
if (isStage0Value(body.symbol)) {
388388
// Optimization: avoid the full conversion when capturing inlined `x`
389389
// in '{ x } to '{ x$1.toExpr.unary_~ } and go directly to `x$1.toExpr`
390-
liftValue(capturers(body.symbol)(body))
390+
liftInlineParamValue(capturers(body.symbol)(body))
391391
} else {
392392
// Optimization: avoid the full conversion when capturing `x`
393393
// in '{ x } to '{ x$1.unary_~ } and go directly to `x$1`
@@ -578,7 +578,7 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
578578
splice(t.select(if (tree.isTerm) nme.UNARY_~ else tpnme.UNARY_~))
579579
if (!isStage0Value(tree.symbol)) captureAndSplice(capturer(tree))
580580
else if (level == 0) capturer(tree)
581-
else captureAndSplice(liftValue(capturer(tree)))
581+
else captureAndSplice(liftInlineParamValue(capturer(tree)))
582582
case Block(stats, _) =>
583583
val last = enteredSyms
584584
stats.foreach(markDef)
@@ -633,19 +633,22 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
633633
}
634634
}
635635

636-
private def liftValue(tree: Tree)(implicit ctx: Context): Tree = {
637-
val reqType = defn.QuotedLiftableType.appliedTo(tree.tpe.widen)
638-
val liftable = ctx.typer.inferImplicitArg(reqType, tree.pos)
639-
liftable.tpe match {
640-
case fail: SearchFailureType =>
641-
ctx.error(i"""
642-
|
643-
| The access would be accepted with the right Liftable, but
644-
| ${ctx.typer.missingArgMsg(liftable, reqType, "")}""")
645-
EmptyTree
646-
case _ =>
647-
liftable.select("toExpr".toTermName).appliedTo(tree)
648-
}
636+
/** Takes a reference to an inline parameter `tree` and lifts it to an Expr */
637+
private def liftInlineParamValue(tree: Tree)(implicit ctx: Context): Tree = {
638+
val tpSym = tree.tpe.widenDealias.classSymbol
639+
640+
val lifter =
641+
if (tpSym eq defn.BooleanClass) defn.QuotedLiftable_BooleanIsLiftable
642+
else if (tpSym eq defn.ByteClass) defn.QuotedLiftable_ByteIsLiftable
643+
else if (tpSym eq defn.CharClass) defn.QuotedLiftable_CharIsLiftable
644+
else if (tpSym eq defn.ShortClass) defn.QuotedLiftable_ShortIsLiftable
645+
else if (tpSym eq defn.IntClass) defn.QuotedLiftable_IntIsLiftable
646+
else if (tpSym eq defn.LongClass) defn.QuotedLiftable_LongIsLiftable
647+
else if (tpSym eq defn.FloatClass) defn.QuotedLiftable_FloatIsLiftable
648+
else if (tpSym eq defn.DoubleClass) defn.QuotedLiftable_DoubleIsLiftable
649+
else defn.QuotedLiftable_StringIsLiftable
650+
651+
ref(lifter).select("toExpr".toTermName).appliedTo(tree)
649652
}
650653

651654
private def isStage0Value(sym: Symbol)(implicit ctx: Context): Boolean =

library/src/scala/quoted/Liftable.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ abstract class Liftable[T] {
1616
*/
1717
object Liftable {
1818
implicit def BooleanIsLiftable: Liftable[Boolean] = (x: Boolean) => liftedExpr(x)
19-
implicit def ByteLiftable: Liftable[Byte] = (x: Byte) => liftedExpr(x)
19+
implicit def ByteIsLiftable: Liftable[Byte] = (x: Byte) => liftedExpr(x)
2020
implicit def CharIsLiftable: Liftable[Char] = (x: Char) => liftedExpr(x)
2121
implicit def ShortIsLiftable: Liftable[Short] = (x: Short) => liftedExpr(x)
2222
implicit def IntIsLiftable: Liftable[Int] = (x: Int) => liftedExpr(x)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.quoted.Expr
2+
object Macro {
3+
inline def foo(inline n: Int): Int = ~{
4+
import quoted.Liftable.{IntIsLiftable => _}
5+
'(n)
6+
}
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
object App {
3+
Macro.foo(3)
4+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.quoted.Expr
2+
object Macro {
3+
inline def foo(inline n: Int): Int = ~{
4+
import quoted.Liftable.{IntIsLiftable => _}
5+
'(n)
6+
}
7+
}

0 commit comments

Comments
 (0)