Skip to content

Prepare inlining of quotes transforms too much #14603

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
nicolasstucki opened this issue Mar 2, 2022 · 1 comment · Fixed by #14604
Closed

Prepare inlining of quotes transforms too much #14603

nicolasstucki opened this issue Mar 2, 2022 · 1 comment · Fixed by #14604
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug
Milestone

Comments

@nicolasstucki
Copy link
Contributor

The bug in the macro implementation was fixed upstream. The fact that compiler was crashing isn't a bug per-se, given -Xcheck-macros (same if it were -Ycheck) errors out.

Currently, however this smaller minimisation of the macro implementation doesn't even compile, because "prepare inlineable" widens a prefix (type avoidance) which fails -Ycheck:

// Verify.scala
import scala.language.experimental.macros
import scala.quoted.*

class Runtime

class Macro(using qctx: Quotes) {
  import qctx.reflect._

  def apply[A: Type](x: Expr[A]): Expr[Unit] = {
    '{
      val rt: Runtime = ???
      ${Block(doExprs('{ rt }.asTerm), '{ () }.asTerm).asExprOf[Unit]}
    }
  }

  private def doExprs(rt: Term): List[Term] = Nil
}
Fatal compiler crash when compiling: tests/pos-macros/i7128:
assertion failed: Found:    quoted.Quotes#reflect.Term2s
Required: Macro.this.qctx.reflect.Term
found: type Term in trait reflectModule with type Term, flags = <deferred> <touched>, underlying = quoted.Quotes#reflect.Term,  <: quoted.Quotes#reflect.Statement, quoted.Quotes#reflect.Statement,  <: quoted.Quotes#reflect.Tree, quoted.Quotes#reflect.Tree,  <: AnyRef, AnyRef,  = Object, Object, Any with Matchable {...}
expected: type Term in trait reflectModule with type Term, flags = <deferred> <touched>, underlying = Macro.this.qctx.reflect.Term,  <: Macro.this.qctx.reflect.Statement, Macro.this.qctx.reflect.Statement,  <: Macro.this.qctx.reflect.Tree, Macro.this.qctx.reflect.Tree,  <: AnyRef, AnyRef,  = Object, Object, Any with Matchable {...}
tree = this.inline$qctx.reflect.asTerm('{rt}.apply(evidence$2))
	at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
	at dotty.tools.dotc.transform.TreeChecker$Checker.adapt(TreeChecker.scala:598)
	at dotty.tools.dotc.typer.ProtoTypes$FunProto.typedArg(ProtoTypes.scala:463)
	at dotty.tools.dotc.typer.Applications$ApplyToUntyped.typedArg(Applications.scala:866)

Nico to close and spin off an issue for that.

Originally posted by @dwijnand in #7128 (comment)

@nicolasstucki
Copy link
Contributor Author

Minimized

import scala.quoted.*

class Macro(using qctx: Quotes): // Anti pattern: put Quotes in a field
  import qctx.reflect._

  def apply: Expr[Unit] = '{
    println("in quote")
    ${ val a: Term = '{ println("in nested quote") }.asTerm; ??? }
  }

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Mar 2, 2022
Contents of splices do not need to be transformed as they will be
evaluated before the code is inlined. Therefore we only want to make
code at staging level 1 or grater inlineable. If the quote is in an
inline method, we also need to make the contents of the splices
inlineable (i.e. for any level).

Fix scala#14603
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Mar 2, 2022
Contents of splices do not need to be transformed as they will be
evaluated before the code is inlined. Therefore we only want to make
code at staging level 1 or grater inlineable. If the quote is in an
inline method, we also need to make the contents of the splices
inlineable (i.e. for any level).

Fix scala#14603
@Kordyjan Kordyjan added this to the 3.1.3 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants