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

ToolBox.runExpr resets attrs #5713

Closed
scabug opened this issue Apr 27, 2012 · 2 comments
Closed

ToolBox.runExpr resets attrs #5713

scabug opened this issue Apr 27, 2012 · 2 comments
Assignees
Labels
Milestone

Comments

@scabug
Copy link

scabug commented Apr 27, 2012

The following code compiles, but yields a runtime error:

package m

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

object Level extends Enumeration {
  val Error = Value(5)
}

object Logger {
  def error(message: String): Unit = macro LoggerMacros.error
}

private object LoggerMacros {

  type LoggerContext = Context { type PrefixType = Logger.type }

  def error(c: LoggerContext)(message: c.Expr[String]): c.Expr[Unit] =
    log(c)(c.reify(Level.Error), message)

  private def log(c: LoggerContext)(level: c.Expr[Level.Value], message: c.Expr[String]): c.Expr[Unit] = 
    if (level.eval.id < 4) // TODO Remove hack!
      c.reify(())
    else {
      c.reify(println(message.eval))
    }
}

scala> import m._
import m._

scala> Logger.error("err")
<console>:11: error: exception during macro expansion: 
scala.tools.nsc.ToolBoxes$ToolBox$ToolBoxError: reflective compilation has failed: 

object Error is not a member of package Level
	at scala.tools.nsc.ToolBoxes$ToolBox.runExpr(ToolBoxes.scala:48)
	at scala.reflect.api.Exprs$Expr.eval(Exprs.scala:15)
	at m.LoggerMacros$.log(M.scala:22)
	at m.LoggerMacros$.error(M.scala:19)

              Logger.error("err")
                          ^

When you remove the package clause everything works.

@scabug
Copy link
Author

scabug commented Apr 27, 2012

Imported From: https://issues.scala-lang.org/browse/SI-5713?orig=1
Reporter: @xeno-by
Other Milestones: 2.10.0-M3

@scabug
Copy link
Author

scabug commented Apr 27, 2012

@xeno-by said:
Fixed in scala/scala@543df62

@scabug scabug closed this as completed Apr 27, 2012
@scabug scabug added the macros label Apr 7, 2017
@scabug scabug added this to the 2.10.0-M2 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants