Skip to content

"Error while emitting" compiler crash with edge-case macro usage #11628

Open
@shadaj

Description

@shadaj

As part of developing a new macro-backed feature for Slinky, shadaj/slinky#263, I found an issue where the compiler will fail to compile code that involves a macro in a very specific situation. This issue affects both Scala 2.12 and 2.13. I'm running on JDK 8, but don't believe that is related to this bug.

I've put up a minimal reproduction of this issue at https://github.com/shadaj/scala-macro-bugs.

A summary of the conditions that cause this issue (copied from the README):

  • Have a macro that includes and uses a val as part of the code it emits
  • Call the macro when supplying a by-name parameter to some method b
  • Call method b while providing varargs to method a
  • Method a is called with a parameter (default or non-default) that is not the first parameter supplied as a named parameter

Example (copied from the repo):

object Macro {
  def myMacro(): Int = macro myMacroImpl
  def myMacroImpl(c: blackbox.Context)(): c.Tree = {
    import c.universe._
    q"""{
      val ret = 0
      println(ret)
      0
    }"""
  }
}

object MacroTest {
  def a(default: Int = 0, defaultOrNonDefault: Int)(args: Int*) = ???
  def b(num: => Int): Int = ???
  a(defaultOrNonDefault = 0)(b(Macro.myMacro()))
}

The compiler fails with:

[error] Error while emitting MacroTest.scala
[error] value ret

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions