Skip to content

Commit aa4f7ec

Browse files
nicolasstuckiolsdavis
authored andcommitted
Add reflect Unapply.apply
Fixes scala#12850
1 parent a40ced3 commit aa4f7ec

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-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.NothingType))
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,7 +2087,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
20872087

20882088
/** Methods of the module object `val Unapply` */
20892089
trait UnapplyModule { this: Unapply.type =>
2090+
/** Create an `Unapply` tree representing a pattern `<fun>(<patterns*>)(using <implicits*>)` */
2091+
def apply(fun: Term, implicits: List[Term], patterns: List[Tree]): Unapply
2092+
/** Copy an `Unapply` tree representing a pattern `<fun>(<patterns*>)(using <implicits*>)` */
20902093
def copy(original: Tree)(fun: Term, implicits: List[Term], patterns: List[Tree]): Unapply
2094+
/** Matches an `Unapply(fun, implicits, patterns)` tree representing a pattern `<fun>(<patterns*>)(using <implicits*>)` */
20912095
def unapply(x: Unapply): (Term, List[Term], List[Tree])
20922096
}
20932097

@@ -2097,8 +2101,15 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
20972101
/** Extension methods of `Unapply` */
20982102
trait UnapplyMethods:
20992103
extension (self: Unapply)
2104+
/** The extractor function of the pattern.
2105+
*
2106+
* It may be a reference to the `unapply` method of the pattern or may be a
2107+
* partially applied tree containing type parameters and leading given parameters.
2108+
*/
21002109
def fun: Term
2110+
/** Training implicit parameters of the `unapply` method */
21012111
def implicits: List[Term]
2112+
/** List of nested patterns */
21022113
def patterns: List[Tree]
21032114
end extension
21042115
end UnapplyMethods

project/MiMaFilters.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ object MiMaFilters {
1111
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.getJPath"),
1212
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.name"),
1313
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.path"),
14+
exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#UnapplyModule.apply"),
15+
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#UnapplyModule.apply"),
1416
)
1517
}
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)