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 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
28 changes: 26 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,28 @@ 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 removeEraisedArguments(args: List[Tree], fnTpe: Type): List[Tree] = {
var result = args
var index = 0
def loop(tp: Type): Unit = tp match {
Copy link
Contributor Author

@OlivierBlanvillain OlivierBlanvillain Jul 9, 2019

Choose a reason for hiding this comment

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

It's not the most beautiful code I've written but it's reasonable in its non erased path and (IMO) pretty clear

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should modify Call to return a List[List[Tree]] for the arguments. It would make this logic much simpler as each element of the outer list corresponds to one of the method types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, it's much simpler! :)

case tp: TermRef => loop(tp.underlying)
case tp: PolyType => loop(tp.resType)
case tp: MethodType if tp.isErasedMethod =>
tp.paramInfos.foreach { _ =>
result = result.updated(index, null)
index += 1
}
loop(tp.resType)
case tp: MethodType =>
index += tp.paramInfos.size
loop(tp.resType)
case _ => ()
}
loop(fnTpe)
assert(index == args.size)
result.filterNot(null.eq)
}

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 Down Expand Up @@ -351,10 +373,12 @@ object Splicer {
interpretModuleAccess(fn.symbol)
} else if (fn.symbol.isStatic) {
val module = fn.symbol.owner
interpretStaticMethodCall(module, fn.symbol, args.map(arg => interpretTree(arg)))
def interpretedArgs = removeEraisedArguments(args, fn.tpe).map(arg => interpretTree(arg))
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 = removeEraisedArguments(args, fn.tpe).map(arg => interpretTree(arg))
interpretStaticMethodCall(module, fn.symbol, interpretedArgs)
} else if (env.contains(fn.name)) {
env(fn.name)
} else if (tree.symbol.is(InlineProxy)) {
Expand Down
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 }
}
10 changes: 10 additions & 0 deletions tests/run-macros/erased-arg-macro/2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object Test {
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)
}