Skip to content
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

Regression: scopes created within a macro are unable to reference values created on the outside #10145

Open
scabug opened this issue Jan 12, 2017 · 1 comment

Comments

@scabug
Copy link

scabug commented Jan 12, 2017

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

@scabug
Copy link
Author

scabug commented Jan 12, 2017

Imported From: https://issues.scala-lang.org/browse/SI-10145?orig=1
Reporter: Bruno Filippone (bfil)
Affected Versions: 2.12.1

@SethTisue SethTisue added this to the Backlog milestone Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants