3

似乎 的输入Context.eval只能引用来自不同编译单元的值:

// project 1
object Z {

  val foo = "WOOF"

  def impl(c: Context)(x: c.Expr[String]) = {
    val x1 = c.Expr[String](c.untypecheck(x.tree.duplicate))
    println(s"compile-time value is: ${c.eval(x1)}")
    x
  }
  def test(x: String) = macro impl
}

// project 2
object Y {
  val foo = "GOOF"
  val boo = Z.test(Z.foo)
}


println(Y.boo)

打印出来"WOOF",但如果我将 boo 替换为val boo = Z.test(Y.foo),我会收到以下编译错误:

Error:(32, 29) exception during macro expansion:
java.lang.ClassNotFoundException: Y$
at scala.reflect.internal.util.AbstractFileClassLoader.findClass(AbstractFileClassLoader.scala:72)
...

有没有办法解决这个问题?我知道用 quill.io 定义的查询可以引用同一范围内的方法,但我找不到他们用来允许它的技巧。

4

1 回答 1

4

Context#eval无法评估运行时值。它写在它的 scaladoc 中:https ://github.com/scala/scala/blob/2.13.x/src/reflect/scala/reflect/macros/Evals.scala#L61-L67

让我们修改你的宏

def impl(c: blackbox.Context)(x: c.Expr[String]): c.Expr[String] = {
  import c.universe._
  println(s"input: ${showRaw(x.tree)}") // added
  val x1 = c.Expr[String](c.untypecheck(x.tree.duplicate))
  println(s"compile-time value is: ${c.eval(x1)}")
  x
}

然后我们会有

object App {
  /*class*/ object Y {
    val foo = "GOOF"

    val boo = Z.test(Z.foo)//Warning:scalac: input: Select(Select(Ident(Macros), Macros.Z), TermName("foo"))
                           //Warning:scalac: compile-time value is: WOOF
//  val boo1 = Z.test(Y.foo)//Warning:scalac: input: Select(Select(This(TypeName("App")), App.Y), TermName("foo"))
                            //Error: exception during macro expansion:  
                            //  java.lang.ClassNotFoundException: App$Y$
//  val boo2 = Z.test((new Y).foo)//Warning:scalac: input: Select(Apply(Select(New(Select(This(TypeName("App")), App.Y)), termNames.CONSTRUCTOR), List()), TermName("foo"))
                                  //Error: exception during macro expansion: 
                                  //  java.lang.ClassNotFoundException: App$Y
//  val boo3 = Z.test(foo) //Warning:scalac: input: Select(This(TypeName("Y")), TermName("foo"))
                           //Error: exception during macro expansion:
                           //  scala.tools.reflect.ToolBoxError: reflective compilation has failed:
                           //    Internal error: unable to find the outer accessor symbol of object __wrapper$1$fd3cb1297ce8421e809ee5e821c2f708
                     // or
                           //Error: exception during macro expansion:  
                           //  java.lang.ClassNotFoundException: App$Y$
    val boo4 = Z.test("abc")//Warning:scalac: input: Literal(Constant("abc"))
                            //Warning:scalac: compile-time value is: abc
    val boo5 = Z.test("abc" + "DEF")//Warning:scalac: input: Literal(Constant("abcDEF"))
                                    //Warning:scalac: compile-time value is: abcDEF
  }
}

This意味着它代表一个运行时值。只是ClassNotFoundException有时发生得比ToolBoxError. 您使用宏的子项目project 1不依赖于子项目project 2,因此在编译宏期间Y找不到。

Z.foofoo(又名Y.foo)之间的区别foo实际上是this.foo(编译器在这里不关心Y是类还是对象)并且可以在子类中被覆盖。

鹅毛笔不使用eval。如果可以,它将树解析为自己的AST,如果不能则离开Dynamic(即,如果树对应于运行时值)。然后它以不同的方式处理这两种情况:在宏扩展期间使用QueryMeta或在编译时+运行时使用Decoder

https://github.com/getquill/quill/blob/master/quill-core/src/main/scala/io/getquill/context/QueryMacro.scala#L34-L38

所以解决方法是在运行时使用运行时值。

def impl(c: blackbox.Context)(x: c.Expr[String]): c.Expr[String] = {
  import c.universe._
  println(s"input: ${showRaw(x.tree)}")
  try {
    val x1 = c.Expr[String](c.untypecheck(x.tree.duplicate))
    val x2 = c.eval(x1)
    println(s"compile-time value is: $x2")
    c.Expr[String](q"$x2")
  } catch {
    case ex: Throwable =>
      println(ex.getMessage)
      x
  }
}

这类似于https://github.com/getquill/quill/blob/master/quill-core/src/main/scala/io/getquill/context/ContextMacro.scala#L66-L68

于 2019-07-08T18:03:44.840 回答