Skip to content

Commit eb8275a

Browse files
committed
Add reflect Unapply.apply
Fixes #12850
1 parent 3dd15f7 commit eb8275a

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
14391439
end UnapplyTypeTest
14401440

14411441
object Unapply extends UnapplyModule:
1442+
def apply(fun: Term, implicits: List[Term], patterns: List[Tree]): Unapply =
1443+
withDefaultPos(tpd.UnApply(fun, implicits, patterns, dotc.core.Symbols.defn.AnyType))
14421444
def copy(original: Tree)(fun: Term, implicits: List[Term], patterns: List[Tree]): Unapply =
14431445
withDefaultPos(tpd.cpy.UnApply(original)(fun, implicits, patterns))
14441446
def unapply(x: Unapply): (Term, List[Term], List[Tree]) =

library/src/scala/quoted/Quotes.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,6 +2087,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
20872087

20882088
/** Methods of the module object `val Unapply` */
20892089
trait UnapplyModule { this: Unapply.type =>
2090+
def apply(fun: Term, implicits: List[Term], patterns: List[Tree]): Unapply
20902091
def copy(original: Tree)(fun: Term, implicits: List[Term], patterns: List[Tree]): Unapply
20912092
def unapply(x: Unapply): (Term, List[Term], List[Tree])
20922093
}

project/MiMaFilters.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import com.typesafe.tools.mima.core.ProblemFilters._
44

55
object MiMaFilters {
66
val Library: Seq[ProblemFilter] = Seq(
7+
// Will be added in 3.1.0
8+
exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#UnapplyModule.apply"),
9+
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#UnapplyModule.apply"),
10+
711
// Internals added in 3.0.2
812
exclude[MissingClassProblem]("scala.annotation.internal.ProvisionalSuperClass"),
913

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
import scala.quoted.*
3+
4+
inline def foo() = ${ expr }
5+
6+
private def expr(using Quotes): Expr[Unit] =
7+
import quotes.reflect.*
8+
9+
// Option(1) match
10+
// case Some(1) => ()
11+
// case None => ()
12+
val mtch2 = Match(
13+
Apply(TypeApply(Ref(Symbol.requiredMethod("scala.Option.apply")), List(Inferred(TypeRepr.of[Int]))), List(Literal(IntConstant(1)))),
14+
List(
15+
CaseDef(/** FIXME: needs TypedTree from #12200; remove cast */Typed(Unapply(TypeApply(Ref(Symbol.requiredMethod("scala.Some.unapply")), List(Inferred(TypeRepr.of[Int]))), Nil, List(Literal(IntConstant(1)))).asInstanceOf[Term], Inferred(TypeRepr.of[Some[Int]])), None, Literal(UnitConstant())),
16+
CaseDef(Ref(Symbol.requiredModule("scala.None")), None, Literal(UnitConstant())))
17+
)
18+
19+
mtch2.asExprOf[Unit]

tests/pos-macros/i12850/Test_2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def test = foo()

0 commit comments

Comments
 (0)