From f8a5424188e3671e59671b6997b18209e87f3fe4 Mon Sep 17 00:00:00 2001 From: Olivier Blanvillain Date: Fri, 12 Jul 2019 16:03:37 +0200 Subject: [PATCH] Handle $asInstanceOf$ in Splicer Before that commit macro expansion would fail with: "Could not find method Box$.$asInstanceOf$ with parameters (). The most common reason for that is that you apply macros in the compilation run that defines them" --- .../src/dotty/tools/dotc/transform/Splicer.scala | 9 ++++++--- tests/run-macros/dollar-asInstanceOf-dollar/1.scala | 12 ++++++++++++ tests/run-macros/dollar-asInstanceOf-dollar/2.scala | 4 ++++ 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 tests/run-macros/dollar-asInstanceOf-dollar/1.scala create mode 100644 tests/run-macros/dollar-asInstanceOf-dollar/2.scala diff --git a/compiler/src/dotty/tools/dotc/transform/Splicer.scala b/compiler/src/dotty/tools/dotc/transform/Splicer.scala index f37e1d9d1d31..1a1db7b33673 100644 --- a/compiler/src/dotty/tools/dotc/transform/Splicer.scala +++ b/compiler/src/dotty/tools/dotc/transform/Splicer.scala @@ -193,8 +193,12 @@ object Splicer { val staticMethodCall = interpretedStaticMethodCall(fn.symbol.owner, fn.symbol) staticMethodCall(args.flatten.map(interpretTree)) } else if (fn.qualifier.symbol.is(Module) && fn.qualifier.symbol.isStatic) { - val staticMethodCall = interpretedStaticMethodCall(fn.qualifier.symbol.moduleClass, fn.symbol) - staticMethodCall(args.flatten.map(interpretTree)) + if (fn.name == nme.asInstanceOfPM) { + interpretModuleAccess(fn.qualifier.symbol) + } else { + val staticMethodCall = interpretedStaticMethodCall(fn.qualifier.symbol.moduleClass, fn.symbol) + staticMethodCall(args.flatten.map(interpretTree)) + } } else if (env.contains(fn.name)) { env(fn.name) } else if (tree.symbol.is(InlineProxy)) { @@ -265,7 +269,6 @@ object Splicer { val name = getDirectName(fn.info.finalResultType, fn.name.asTermName) val method = getMethod(clazz, name, paramsSig(fn)) - (args: List[Object]) => stopIfRuntimeException(method.invoke(inst, args: _*)) } diff --git a/tests/run-macros/dollar-asInstanceOf-dollar/1.scala b/tests/run-macros/dollar-asInstanceOf-dollar/1.scala new file mode 100644 index 000000000000..482d105c0595 --- /dev/null +++ b/tests/run-macros/dollar-asInstanceOf-dollar/1.scala @@ -0,0 +1,12 @@ +import scala.quoted._ +import scala.deriving._ + +case class Box[A](x: A) + +object Macro { + inline def foo[X[_]](implicit inline m: Mirror { type MirroredType = X }): Int = + ${ fooImpl } + + def fooImpl[X[_]](implicit m: Mirror { type MirroredType = X }, qc: QuoteContext): Expr[Int] = + '{ 1 } +} diff --git a/tests/run-macros/dollar-asInstanceOf-dollar/2.scala b/tests/run-macros/dollar-asInstanceOf-dollar/2.scala new file mode 100644 index 000000000000..fe38b9be821e --- /dev/null +++ b/tests/run-macros/dollar-asInstanceOf-dollar/2.scala @@ -0,0 +1,4 @@ +object Test { + def main(args: Array[String]): Unit = + assert(Macro.foo[Box] == 1) +}