Skip to content

Commit

Permalink
Only evaluate transparent inline unapply once
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Jan 5, 2024
1 parent 2746ee8 commit 867d330
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,9 @@ trait Applications extends Compatibility {
unapplyArgType

val dummyArg = dummyTreeOfType(ownType)
val unapplyApp = typedExpr(untpd.TypedSplice(Apply(unapplyFn, dummyArg :: Nil)))
val unapplyApp = withMode(Mode.NoInline) { // transparent inline unapplies are exapnded when indexing the pattern see `indexPattern` in Typer.
typedExpr(untpd.TypedSplice(Apply(unapplyFn, dummyArg :: Nil)))
}
def unapplyImplicits(unapp: Tree): List[Tree] = {
val res = List.newBuilder[Tree]
def loop(unapp: Tree): Unit = unapp match {
Expand Down
11 changes: 11 additions & 0 deletions tests/pos-macros/i19369/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.quoted._

object Unapplier:
transparent inline def unapply(arg: Any): Option[Int] = ${unapplyImpl('arg)}

def unapplyImpl(using Quotes)(argExpr: Expr[Any]): Expr[Option[Int]] =
executionCount += 1
assert(executionCount == 1, "macro should only expand once")
'{Some(1)}

private var executionCount = 0
2 changes: 2 additions & 0 deletions tests/pos-macros/i19369/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@main def main() =
val Unapplier(result) = Some(5)

0 comments on commit 867d330

Please sign in to comment.