Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[backport] transformers no longer ignore UnApply.fun
- Loading branch information
Showing
5 changed files
with
114 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(SomeTree,SomeTree) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import scala.reflect.macros.Context | ||
import scala.language.experimental.macros | ||
|
||
trait Tree | ||
case object SomeTree extends Tree | ||
|
||
object NewQuasiquotes { | ||
implicit class QuasiquoteInterpolation(c: StringContext) { | ||
object nq { | ||
def unapply(t: Tree): Any = macro QuasiquoteMacros.unapplyImpl | ||
} | ||
} | ||
} | ||
|
||
object QuasiquoteMacros { | ||
def unapplyImpl(c: Context)(t: c.Expr[Tree]) = { | ||
import c.universe._ | ||
import Flag._ | ||
// q""" | ||
// new { | ||
// def unapply(t: Tree) = t match { | ||
// case SomeTree => Some((SomeTree, SomeTree)) | ||
// case _ => None | ||
// } | ||
// }.unapply($t) | ||
// """ | ||
c.Expr[Any](Apply( | ||
Select( | ||
Block(List( | ||
ClassDef(Modifiers(FINAL), newTypeName("$anon"), List(), | ||
Template(List(Select(Ident(newTermName("scala")), newTypeName("AnyRef"))), emptyValDef, List( | ||
DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), | ||
Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))), | ||
DefDef(Modifiers(), newTermName("unapply"), List(), List(List(ValDef(Modifiers(PARAM), newTermName("t"), Ident(newTypeName("Tree")), EmptyTree))), TypeTree(), | ||
Match( | ||
Ident(newTermName("t")), List( | ||
CaseDef(Ident(newTermName("SomeTree")), EmptyTree, Apply(Ident(newTermName("Some")), List(Apply(Select(Ident(newTermName("scala")), newTermName("Tuple2")), List(Ident(newTermName("SomeTree")), Ident(newTermName("SomeTree"))))))), | ||
CaseDef(Ident(nme.WILDCARD), EmptyTree, Ident(newTermName("None")))))))))), | ||
Apply(Select(New(Ident(newTypeName("$anon"))), nme.CONSTRUCTOR), List())), | ||
newTermName("unapply")), | ||
List(t.tree))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
object Test extends App { | ||
import NewQuasiquotes._ | ||
SomeTree match { | ||
case nq"$x + $y" => println((x, y)) | ||
} | ||
} |