mercredi 9 décembre 2020

Creation trees with reify error: Macro expansion contains free term variable c

I'm learning how to create trees with reify as specified in the documentation and here is my toy macro attempt:

I.

def hasErrors: Boolean = macro impl

def impl(c: blackbox.Context): c.Expr[Boolean] = {
  import c.universe._
  c.Expr(reify(c.hasErrors).tree) //error
}

The macro invocation fails with the following error:

Main.scala:21:17: Macro expansion contains free term variable c defined by impl in Main.scala:11:12. 
Have you forgotten to use splice when splicing this variable into a reifee? 
If you have troubles tracking free term variables, consider using -Xlog-free-terms

II.

I tried to add splice, but got the same error:

def hasErrors: Boolean = macro impl

def impl(c: blackbox.Context): c.Expr[Boolean] = {
  import c.universe._
  reify(reify(c.hasErrors).splice) //error
}

III.

Writing the tree manually works fine:

def hasErrors: Boolean = macro impl

def impl(c: blackbox.Context): c.Expr[Boolean] = {
  import c.universe._
  c.Expr(Literal(Constant(c.hasErrors))) //compiles fine
}

What is the problem with reify here and how to fix it?





Aucun commentaire:

Enregistrer un commentaire