Skip to content

Commit 4ecddb4

Browse files
committed
Fix scala#19369 - duplicate macro execution in unapply methods
1 parent 2746ee8 commit 4ecddb4

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ 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 = typedExpr(untpd.TypedSplice(Apply(unapplyFn, dummyArg :: Nil)))(using ctx.addMode(Mode.NoInline))
14501450
def unapplyImplicits(unapp: Tree): List[Tree] = {
14511451
val res = List.newBuilder[Tree]
14521452
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+
import quotes.reflect._
8+
// The code below will recurse, throwing cyclic reference, if it is being inlined too early,
9+
// which is caused by the same problem as the duplicate macro execution here
10+
println(Symbol.spliceOwner.owner.tree)
11+
'{Some(1)}

tests/pos-macros/i19369/Main_2.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
val Unapplier(result) = Some(5)
4+
}
5+
}

0 commit comments

Comments
 (0)