Closed
Description
Minimized code
X.scala
package x
import scala.quoted._
object FastPath {
inline def sum(x:Int):Int =
x + 1
}
object SlowPath {
def sum(x:Int):Int =
x + 1
}
object X {
inline def transform[A](inline x:A):A = ${
transformImpl[A]('x)
}
def transformImpl[A:Type](x:Expr[A])(using qctx: QuoteContext):Expr[A] = {
import qctx.tasty._
val slowPath = '{ SlowPath }.unseal
val fastPath = '{ FastPath }.unseal
val transformer = new TreeMap() {
override def transformTerm(term:Term)(using ctx:Context):Term = {
term match
case Apply(sel@Select(o,m),args) =>
if ( o.tpe =:= slowPath.tpe && m=="sum" )
Apply(Select.unique(fastPath,"sum"), args)
else
super.transformTerm(term)
case _ => super.transformTerm(term)
}
}
val r = transformer.transformTerm(x.unseal).seal.cast[A]
println(s"result: ${r.show}")
r
}
}
and Main.scala
package x
object Main {
def main(args:Array[String]):Unit =
val r = X.transform{
SlowPath.sum(1)
}
}
Output
Latest Dotty nightly build version: 0.28.0-bin-20200828-4c99388-NIGHTLY
[info] loading settings for project root from build.sbt ...
[info] set current project to test (in build file:/Users/rssh/tests/dotty/unexpanded-inline/)
[info] Executing in batch mode. For better performance use sbt's shell
[info] Compiling 1 Scala source to /Users/rssh/tests/dotty/unexpanded-inline/target/scala-0.28/classes ...
[info] Compiling 1 Scala source to /Users/rssh/tests/dotty/unexpanded-inline/target/scala-0.28/classes ...
result: x.FastPath.sum(1)
[error] -- Error: /Users/rssh/tests/dotty/unexpanded-inline/src/main/scala/x/Main.scala:8:22
[error] 8 | val r = X.transform{
[error] | ^
[error] | method sum is declared as erased, but is in fact used
[error] 9 | SlowPath.sum(1)
[error] 10 | }
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
Expectation
that it will compile and expand inlined FastPath.sum
btw, if this impossible => maybe give access to inlineCall from reflection interface ?