Skip to content

Commit f60d9a7

Browse files
committed
Only evaluate transparent inline unapply once
Fixes scala#19369
1 parent 2746ee8 commit f60d9a7

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,9 @@ trait Applications extends Compatibility {
14461446
unapplyArgType
14471447

14481448
val dummyArg = dummyTreeOfType(ownType)
1449-
val unapplyApp = typedExpr(untpd.TypedSplice(Apply(unapplyFn, dummyArg :: Nil)))
1449+
val unapplyApp = withMode(Mode.NoInline) { // transparent inline unapplies are exapnded when indexing the pattern see `indexPattern` in Typer.
1450+
typedExpr(untpd.TypedSplice(Apply(unapplyFn, dummyArg :: Nil)))
1451+
}
14501452
def unapplyImplicits(unapp: Tree): List[Tree] = {
14511453
val res = List.newBuilder[Tree]
14521454
def loop(unapp: Tree): Unit = unapp match {

tests/pos-macros/i19369/Macro_1.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import scala.quoted._
2+
3+
object Unapplier:
4+
transparent inline def unapply(arg: Any): Option[Int] = ${unapplyImpl('arg)}
5+
6+
def unapplyImpl(using Quotes)(argExpr: Expr[Any]): Expr[Option[Int]] =
7+
executionCount += 1
8+
assert(executionCount == 1, "macro should only expand once")
9+
'{Some(1)}
10+
11+
private var executionCount = 0

tests/pos-macros/i19369/Test_2.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@main def main() =
2+
val Unapplier(result) = Some(5)

0 commit comments

Comments
 (0)