2
Scala 2.13.2

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?

Some Name
  • 8,555
  • 5
  • 27
  • 77
  • https://stackoverflow.com/questions/56977467/macro-expansion-contains-free-variable https://stackoverflow.com/questions/11029675/can-this-free-term-variable-error-produced-at-macro-expansion-be-avoided – Dmytro Mitin Dec 13 '20 at 05:53
  • You can also write a quasiquote `c.Expr(q"${c.hasErrors}")`. – Dmytro Mitin Dec 13 '20 at 05:54

0 Answers0