spire icon indicating copy to clipboard operation
spire copied to clipboard

Macro inlining fails for trivial functions

Open bashimao opened this issue 9 years ago • 1 comments

There is a bug with the macro inlining code. The following gives: 3

object Demo extends App {

  val res: Int = Macros.test(1, 2)
  println(res)

}


object Macros {

  def test(a: Int, b: Int): Int = macro test_impl

  def test_impl(c: Context)(a: c.Expr[Int], b: c.Expr[Int]): c.Expr[Int] = {
    import c.universe._

    val tree: Tree = q"$a + $b"
    new InlineUtil[c.type](c).inlineAndReset[Int](tree)
  }

}

which is correct. But if the macro is changed as follows, the result is 2

object Macros {

  def test(a: Int, b: Int): Int = macro test_impl

  def test_impl(c: Context)(a: c.Expr[Int], b: c.Expr[Int]): c.Expr[Int] = {
    import c.universe._

    val fn = c.universe.reify((i: Int, j: Int) => i + j)

    val tree: Tree = q"$fn($a, $b)"
    new InlineUtil[c.type](c).inlineAndReset[Int](tree)
  }

}

The worst part of it is that there is no warning. I checked the code, but my macro debugging capabilities are limited. It looks as if the error originates from values being overwritten in InlineSymbol.transform(...). Apparently, several instances of tree are supplied to transform(tree: Tree) where the tree.symbol == symbol match is ambiguous.

bashimao avatar Feb 25 '16 14:02 bashimao

I just tested it with a bunch of more complex things. The functions still work. But inlining could be much better. Would it be possible to apply an optimizer?

bashimao avatar Feb 25 '16 18:02 bashimao