以下简单示例(在Scala 2.11.8中运行):
import scala.tools.reflect.ToolBox
import scala.reflect.runtime.currentMirror
object Main extends App {
val toolbox = currentMirror.mkToolBox()
val tree = toolbox.parse {
"""
package foo.bar {
trait Baz
}
"""
}
println(tree)
val checkedTree = toolbox.typecheck(tree)
println(tree)
}
会在toolbox.typecheck(tree)
行产生以下异常:
Exception in thread "main" java.lang.AssertionError: assertion failed: value <local <expression-owner>>
at scala.reflect.internal.Symbols$Symbol.newPackage(Symbols.scala:310)
at scala.tools.nsc.typechecker.Namers$Namer.createPackageSymbol(Namers.scala:381)
at scala.tools.nsc.typechecker.Namers$Namer.createPackageSymbol(Namers.scala:374)
...
如何获得类型检查的AST树?