Skip to content

Macro does not always elide its receiver #5110

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

Closed
allanrenucci opened this issue Sep 14, 2018 · 4 comments
Closed

Macro does not always elide its receiver #5110

allanrenucci opened this issue Sep 14, 2018 · 4 comments

Comments

@allanrenucci
Copy link
Contributor

Let's consider the following macro based string interpolator

import scala.quoted._
import scala.tasty.Tasty

object Macro {
  class StringContextOps(sc: StringContext) {
    rewrite def ff(args: => Any*): String = ~Macro.impl('(sc), '(args))
  }
  implicit rewrite def XmlQuote(sc: => StringContext): StringContextOps = new StringContextOps(sc)
  def impl(sc: Expr[StringContext], args: Expr[Seq[Any]])(implicit tasty: Tasty): Expr[String] = '("")
}

And the following use case:

class Test {
  import Macro._
  def test = ff"Hello World"
}

which expands to:

class Test {
  def test: String = {
    // start dead code
    val $1$: Macro.Macro$StringContextOps =
      new Macro.Macro$StringContextOps(
        _root_.scala.StringContext.apply(
          Predef.wrapRefArray(["Hello World" : String])
        )
      )
    // end dead code

    // expansion
    ""
  }
}

My expectation (and how it works in Scala 2) is that the receiver of the macro gets elided

@sjrd
Copy link
Member

sjrd commented Sep 15, 2018

The receiver is not by-name, so it cannot be elided.

@nicolasstucki
Copy link
Contributor

In general we must not elide the receiver or else we would change the semantics function application.

@allanrenucci
Copy link
Contributor Author

Even if it is by-name, is it not elided:

class Test {
  def test(): String = {
    val $1$: Macro.Macro$StringContextOps = 
      new Macro.Macro$StringContextOps(
        {
          closure(<empty>.this.$anonfun$1:Function0)
        }
      )
    ""
  }

  private final <static> def $anonfun$1(): StringContext = 
    root_.scala.StringContext.apply(
      Predef.wrapRefArray(["Hello World" : String])
    )
}

But when I write:

class Foo {
  rewrite def foo: String = ~Macro.impl
}

object Macro {
  def impl: Expr[String] = '("")
}
class Test {
  def test = (new Foo).foo
}

The full call to the macro foo is elided provided that the constructor of Foo is pure

@nicolasstucki
Copy link
Contributor

nicolasstucki commented Sep 15, 2018

Previously we did rely on the optimizer to remove the remaining unused val with no side effects on the RHS. I will have a look at those details. Maybe we should have a minimal DCE optimization for inlined methods.

@nicolasstucki nicolasstucki self-assigned this Sep 18, 2018
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Mar 20, 2019
With the new syntax for extension method we can create a by name prefix which is elided if not used.
liufengyun added a commit that referenced this issue Mar 20, 2019
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

3 participants