Regression: Macro usage compilation error (related to closure?)

53 views
Skip to first unread message

Eduardo D'Avila

unread,
Jan 24, 2017, 6:49:25 PM1/24/17
to scala-internals
Hi!

I may have found a compiler bug related to macros.

The macro definition:

import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context

object Y {
  def m(g: () => Int): Unit = macro impl

  def impl(c: Context)(g: c.Tree): c.Tree = {
    import c.universe._
    q"{ () => $g() }.apply()"
  }
}

And the usage:

object X {
  def f(): Unit = {
    val thisIsTheVal = 0
    Y.m { () => thisIsTheVal }
  }
}

On 2.11.8, the compilation works without any error. But on 2.12.0 and 2.12.1, the compilation of the code using the macro issues a "java.util.NoSuchElementException: key not found: value thisIsTheVal" exception. See the attached file for the full stack trace.

Is this a known issue?

Regards,

Eduardo
stacktrace.txt

Jason Zaugg

unread,
Jan 24, 2017, 6:58:08 PM1/24/17
to scala-internals
In general, it is not safe to splice a macro argument into the body of a synthesised closure, class, method, or val. The result can be an incoherent "owner chain". 

A common fix (albeit with its own corner cases) it to call `c.untypecheck` on the tree before splicing. 

Changes to the way that lambdas are desugared in 2.12. might have changed this particular example from benign to a crasher.

-jason

--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-interna...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Eduardo D'Avila

unread,
Jan 24, 2017, 7:19:27 PM1/24/17
to scala-internals
Thank you, Jason! 
With the proposed fix, now the code compiles successfully:

  def impl(c: Context)(g: c.Tree): c.Tree = {
    import c.universe._
    val ug = c.untypecheck(g)
    q"{ () => $ug() }.apply()"
  }

Anyways, should I report an issue?

Eduardo

Jason Zaugg

unread,
Jan 24, 2017, 7:29:38 PM1/24/17
to scala-internals
I think in this case the macro is at fault, and it only worked accidentally in 2.11, so no need for an issue.

-jason

Eduardo R. D'Avila

unread,
Jan 24, 2017, 7:33:02 PM1/24/17
to scala-internals

Hmmm... OK

Thanks!


You received this message because you are subscribed to a topic in the Google Groups "scala-internals" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-internals/W4pggkPRiFg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scala-interna...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages