Regression: scopes created within a macro are unable to reference values created on the outside #10145
Open
Description
It's a little hard to find the best wording for this, but it looks like trying to access external val
s from within a closure passed into a macro materializer breaks on Scala 2.12.
Using the following macro:
trait Scope {}
object Macros {
def materializeImpl[T: c.WeakTypeTag](c: Context)(generator: c.Expr[() => T]): c.Expr[Unit] = {
import c.universe._
c.Expr[Unit](q"""
new Scope {
$generator()
}
""")
}
}
def generate[T](generator: () => T): Unit = macro Macros.materializeImpl[T]
The following will compile on Scala 2.11 but not 2.12:
val someValue = 1
generate { () => someValue }
The compiler seem to have problems dealing with someValue
If the macro doesn't wrap the generator with
new Scope {...}
it compiles just fine. If an external method is used from within the closure, it also compiles just fine.
I've created a Github repo that can be cloned and run, to quickly reproduce this issue:
https://github.com/bfil/scala-compiler-bug
The original issue originated when trying to upgrade the following project to Scala 2.12:
https://github.com/bfil/scala-automapper