Skip to content

Add reflect Unapply.apply #13298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
end UnapplyTypeTest

object Unapply extends UnapplyModule:
def apply(fun: Term, implicits: List[Term], patterns: List[Tree]): Unapply =
withDefaultPos(tpd.UnApply(fun, implicits, patterns, dotc.core.Symbols.defn.NothingType))
def copy(original: Tree)(fun: Term, implicits: List[Term], patterns: List[Tree]): Unapply =
withDefaultPos(tpd.cpy.UnApply(original)(fun, implicits, patterns))
def unapply(x: Unapply): (Term, List[Term], List[Tree]) =
Expand Down
11 changes: 11 additions & 0 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2087,7 +2087,11 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>

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

Expand All @@ -2097,8 +2101,15 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
/** Extension methods of `Unapply` */
trait UnapplyMethods:
extension (self: Unapply)
/** The extractor function of the pattern.
*
* It may be a reference to the `unapply` method of the pattern or may be a
* partially applied tree containing type parameters and leading given parameters.
*/
def fun: Term
/** Training implicit parameters of the `unapply` method */
def implicits: List[Term]
/** List of nested patterns */
def patterns: List[Tree]
end extension
end UnapplyMethods
Expand Down
2 changes: 2 additions & 0 deletions project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ object MiMaFilters {
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.getJPath"),
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.name"),
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SourceFileMethods.path"),
exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#UnapplyModule.apply"),
exclude[DirectMissingMethodProblem]("scala.quoted.Quotes#reflectModule#UnapplyModule.apply"),
)
}
19 changes: 19 additions & 0 deletions tests/pos-macros/i12850/Macros_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

import scala.quoted.*

inline def foo() = ${ expr }

private def expr(using Quotes): Expr[Unit] =
import quotes.reflect.*

// Option(1) match
// case Some(1) => ()
// case None => ()
val mtch2 = Match(
Apply(TypeApply(Ref(Symbol.requiredMethod("scala.Option.apply")), List(Inferred(TypeRepr.of[Int]))), List(Literal(IntConstant(1)))),
List(
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())),
CaseDef(Ref(Symbol.requiredModule("scala.None")), None, Literal(UnitConstant())))
)

mtch2.asExprOf[Unit]
1 change: 1 addition & 0 deletions tests/pos-macros/i12850/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def test = foo()