Skip to content

Commit c4ae7e9

Browse files
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"
1 parent e2130b9 commit c4ae7e9

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,12 @@ object Splicer {
264264
}
265265

266266
val name = getDirectName(fn.info.finalResultType, fn.name.asTermName)
267-
val method = getMethod(clazz, name, paramsSig(fn))
268-
269-
(args: List[Object]) => stopIfRuntimeException(method.invoke(inst, args: _*))
267+
if (name == nme.asInstanceOfPM)
268+
(args: List[Object]) => interpretModuleAccess(moduleClass)
269+
else {
270+
val method = getMethod(clazz, name, paramsSig(fn))
271+
(args: List[Object]) => stopIfRuntimeException(method.invoke(inst, args: _*))
272+
}
270273
}
271274

272275
private def interpretModuleAccess(fn: Symbol)(implicit env: Env): Object =
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import scala.quoted._
2+
import scala.deriving._
3+
4+
case class Box[A](x: A)
5+
6+
object Macro {
7+
inline def foo[X[_]](implicit inline m: Mirror { type MirroredType = X }): Int =
8+
${ fooImpl }
9+
10+
def fooImpl[X[_]](implicit m: Mirror { type MirroredType = X }, qc: QuoteContext): Expr[Int] =
11+
'{ 1 }
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Test {
2+
def main(args: Array[String]): Unit =
3+
assert(Macro.foo[Box] == 1)
4+
}

0 commit comments

Comments
 (0)