-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should modify There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -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)) { | ||
|
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 } | ||
} |
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) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.