Skip to content

Support erased arguments in splicer #6830

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
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
42 changes: 31 additions & 11 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,20 @@ object Splicer {
protected def interpretNew(fn: Symbol, args: => List[Result])(implicit env: Env): Result
protected def unexpectedTree(tree: Tree)(implicit env: Env): Result

private final def removeErasedArguments(args: List[List[Tree]], fnTpe: Type): List[List[Tree]] =
fnTpe match {
case tp: TermRef => removeErasedArguments(args, tp.underlying)
case tp: PolyType => removeErasedArguments(args, tp.resType)
case tp: ExprType => removeErasedArguments(args, tp.resType)
case tp: MethodType =>
val tail = removeErasedArguments(args.tail, tp.resType)
if (tp.isErasedMethod) tail else args.head :: tail
case tp: AppliedType if defn.isImplicitFunctionType(tp) =>
val tail = removeErasedArguments(args.tail, tp.args.last)
if (defn.isErasedFunctionType(tp)) tail else args.head :: tail
case tp => assert(args.isEmpty, tp); Nil
}

protected final def interpretTree(tree: Tree)(implicit env: Env): Result = tree match {
case Apply(TypeApply(fn, _), quoted :: Nil) if fn.symbol == defn.InternalQuoted_exprQuote =>
val quoted1 = quoted match {
Expand All @@ -346,15 +360,17 @@ object Splicer {

case Call(fn, args) =>
if (fn.symbol.isConstructor && fn.symbol.owner.owner.is(Package)) {
interpretNew(fn.symbol, args.map(interpretTree))
interpretNew(fn.symbol, args.flatten.map(interpretTree))
} else if (fn.symbol.is(Module)) {
interpretModuleAccess(fn.symbol)
} else if (fn.symbol.isStatic) {
val module = fn.symbol.owner
interpretStaticMethodCall(module, fn.symbol, args.map(arg => interpretTree(arg)))
def interpretedArgs = removeErasedArguments(args, fn.tpe).flatten.map(interpretTree)
interpretStaticMethodCall(module, fn.symbol, interpretedArgs)
} else if (fn.qualifier.symbol.is(Module) && fn.qualifier.symbol.isStatic) {
val module = fn.qualifier.symbol.moduleClass
interpretStaticMethodCall(module, fn.symbol, args.map(arg => interpretTree(arg)))
def interpretedArgs = removeErasedArguments(args, fn.tpe).flatten.map(interpretTree)
interpretStaticMethodCall(module, fn.symbol, interpretedArgs)
} else if (env.contains(fn.name)) {
env(fn.name)
} else if (tree.symbol.is(InlineProxy)) {
Expand Down Expand Up @@ -394,15 +410,19 @@ object Splicer {
}

object Call {
def unapply(arg: Tree): Option[(RefTree, List[Tree])] = arg match {
case Select(Call(fn, args), nme.apply) if defn.isImplicitFunctionType(fn.tpe.widenDealias.finalResultType) =>
Some((fn, args))
case fn: RefTree => Some((fn, Nil))
case Apply(Call(fn, args1), args2) => Some((fn, args1 ::: args2)) // TODO improve performance
case TypeApply(Call(fn, args), _) => Some((fn, args))
case _ => None
def unapply(arg: Tree): Option[(RefTree, List[List[Tree]])] =
Call0.unapply(arg).map((fn, args) => (fn, args.reverse))

object Call0 {
def unapply(arg: Tree): Option[(RefTree, List[List[Tree]])] = arg match {
case Select(Call0(fn, args), nme.apply) if defn.isImplicitFunctionType(fn.tpe.widenDealias.finalResultType) =>
Some((fn, args))
case fn: RefTree => Some((fn, Nil))
case Apply(Call0(fn, args1), args2) => Some((fn, args2 :: args1))
case TypeApply(Call0(fn, args), _) => Some((fn, args))
case _ => None
}
}
}
}

}
21 changes: 21 additions & 0 deletions tests/run-macros/erased-arg-macro/1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import scala.quoted._

object Macro {
inline def foo1(i: Int) = $ { case1('{ i }) }
inline def foo2(i: Int) = $ { case2(1)('{ i }) }
inline def foo3(i: Int) = $ { case3('{ i })(1) }
inline def foo4(i: Int) = $ { case4(1)('{ i }, '{ i }) }
inline def foo5(i: Int) = $ { case5('{ i }, '{ i })(1) }
inline def foo6(i: Int) = $ { case6(1)('{ i })('{ i }) }
inline def foo7(i: Int) = $ { case7('{ i })(1)('{ i }) }
inline def foo8(i: Int) = $ { case8('{ i })('{ i })(1) }

def case1 erased (i: Expr[Int]) given (QuoteContext): Expr[Int] = '{ 0 }
def case2 (i: Int) erased (j: Expr[Int]) given (QuoteContext): Expr[Int] = '{ 0 }
def case3 erased (i: Expr[Int]) (j: Int) given (QuoteContext): Expr[Int] = '{ 0 }
def case4 (h: Int) erased (i: Expr[Int], j: Expr[Int]) given (QuoteContext): Expr[Int] = '{ 0 }
def case5 erased (i: Expr[Int], j: Expr[Int]) (h: Int) given (QuoteContext): Expr[Int] = '{ 0 }
def case6 (h: Int) erased (i: Expr[Int]) erased (j: Expr[Int]) given (QuoteContext): Expr[Int] = '{ 0 }
def case7 erased (i: Expr[Int]) (h: Int) erased (j: Expr[Int]) given (QuoteContext): Expr[Int] = '{ 0 }
def case8 erased (i: Expr[Int]) erased (j: Expr[Int]) (h: Int) given (QuoteContext): Expr[Int] = '{ 0 }
}
12 changes: 12 additions & 0 deletions tests/run-macros/erased-arg-macro/2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
object Test {
def main(args: Array[String]): Unit = {
assert(Macro.foo1(1) == 0)
assert(Macro.foo2(1) == 0)
assert(Macro.foo3(1) == 0)
assert(Macro.foo4(1) == 0)
assert(Macro.foo5(1) == 0)
assert(Macro.foo6(1) == 0)
assert(Macro.foo7(1) == 0)
assert(Macro.foo8(1) == 0)
}
}