-
Notifications
You must be signed in to change notification settings - Fork 1.1k
macros: remove or restrict symbol.tree
to avoid CyclicReference exception
#7025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
shapeless 3's |
|
How about having Having the ability to examine the trees of symbols sounds extremely useful from a macro author perspective. Scala 2 macros often go through very ugly hacks because they don't have it (including casting their way through to expose internal compiler APIs so they can look at the surrounding trees). As another example, IIRC the quill library has been stashing trees in annotations in order to recover them later, but that's a hack and it has plenty of drawbacks. |
Thanks for sharing the use cases @LPTK @milessabin . It seems inline def debug: Unit = ${Macros.debugImpl}
object Macros {
import scala.quoted._
import scala.tasty._
def debugImpl given (qctx: QuoteContext): Expr[Unit] = {
import qctx.tasty._
def nearestEnclosingDef(owner: Symbol): Symbol =
owner match {
case IsDefDefSymbol(x) => x
case IsClassDefSymbol(x) => x
case _ => nearestEnclosingDef(owner.owner)
}
nearestEnclosingDef(rootContext.owner) match {
case IsDefDefSymbol(x) =>
val code = x.signature.toString
'{ println(${code.toExpr}) }
case _ =>
'{()}
}
}
} object Test {
def main(args: Array[String]): Unit = {
bar("world", 100, true)
}
def bar(a1: String, a2: Long, a3: Boolean) = {
debug
}
} |
Another approach could be to just not set the trese when expanding white box macros. |
Reopen: the original test is not added. |
@liufengyun the original test is incomplete |
@nicolasstucki Updated. |
Out of curiosity, what does the current test print? Or does it take the other branch and not print anything? |
Current macro system exposes
symbol.tree
to get the tree of a symbol. However, its usage in macros easily leads toCyclicReference
exception.minimized code
Macro_1.scala
:Test_2.scala
:expectation
The code above leads to
CyclicReference
exception:stack trace
To avoid such crashes with
symbol.tree
, we may either:symbol.tree
in the API (scala2 macros doesn't have such an API)The text was updated successfully, but these errors were encountered: