summaryrefslogtreecommitdiff
path: root/test/files/run/macro-expand-unapply-b/Impls_Macros_1.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2013-01-11 21:11:13 -0800
committerPaul Phillips <paulp@improving.org>2013-01-11 21:12:50 -0800
commit82dc21078e7aa6215ed32b79dece3b4c626ebea6 (patch)
tree833763d89631bf389dc6124e6c3d1b64f409f7dd /test/files/run/macro-expand-unapply-b/Impls_Macros_1.scala
parent5f51e3f754d15415e21f79c2fc81995f89019edf (diff)
parent143cd7a307706a8884c9352e64addf6e9be0a181 (diff)
downloadscala-82dc21078e7aa6215ed32b79dece3b4c626ebea6.tar.gz
scala-82dc21078e7aa6215ed32b79dece3b4c626ebea6.tar.bz2
scala-82dc21078e7aa6215ed32b79dece3b4c626ebea6.zip
Merge commit 'refs/pull/1844/head' into merge/pr-1844
* commit 'refs/pull/1844/head': macroExpandAll is now triggered by typed SI-5923 adapt macros when they are deferred generalizes macroExpand typedPrimaryConstrBody now returns supercall more precise errors for macros parentTypes => typedParentTypes changes isTermMacro checks to something more universal fixes printing of AppliedTypeTree adds Trees.replace(Tree, Tree) makes macro override error more consistent refactors handling of macros in repl SI-5903 extractor macros do work adds c.macroRole Conflicts: src/compiler/scala/tools/nsc/typechecker/Macros.scala src/compiler/scala/tools/nsc/typechecker/Typers.scala
Diffstat (limited to 'test/files/run/macro-expand-unapply-b/Impls_Macros_1.scala')
-rw-r--r--test/files/run/macro-expand-unapply-b/Impls_Macros_1.scala37
1 files changed, 37 insertions, 0 deletions
diff --git a/test/files/run/macro-expand-unapply-b/Impls_Macros_1.scala b/test/files/run/macro-expand-unapply-b/Impls_Macros_1.scala
new file mode 100644
index 0000000000..d0300bdf7e
--- /dev/null
+++ b/test/files/run/macro-expand-unapply-b/Impls_Macros_1.scala
@@ -0,0 +1,37 @@
+import language.experimental.macros
+import scala.reflect.macros.Context
+
+object Macros {
+ implicit class ContextExtensions(c: StringContext) {
+ object q {
+ def unapply(x: Any): Option[Any] = macro impl
+ }
+ }
+
+ def impl(c: Context)(x: c.Expr[Any]): c.Expr[Option[Any]] = {
+ import c.universe._
+ import Flag._
+
+ // parts here will be string literals - static parts of the string interpolation
+ // e.g. for q"$x, $y" parts will be Literal(Constant("")), Literal(Constant(", ")) and Literal(Constant(""))
+ val Apply(Select(Select(Apply(_, List(Apply(_, parts))), _), _), _) = c.macroApplication
+ val nresults = parts.length - 1
+
+ def results() =
+ ((1 to (nresults - 1)).toList map (i => Literal(Constant(i)))) :+ // (n - 1) results of type Int
+ Apply(Ident(TermName("List")), List(Literal(Constant(nresults)))) // and also one result of a different type
+ def extractorBody() =
+ if (nresults == 0) Literal(Constant(true))
+ else if (nresults == 1) Apply(Ident(TermName("Some")), results())
+ else Apply(Ident(TermName("Some")), List(Apply(Ident(TermName("Tuple" + nresults)), results())))
+
+ val name = TermName(java.util.UUID.randomUUID().toString.replace("-", ""))
+ val mdef = ModuleDef(NoMods, name, Template(List(Select(Ident(TermName("scala")), TypeName("AnyRef"))), emptyValDef, List(
+ DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(),
+ Block(List(pendingSuperCall), Literal(Constant(())))),
+ DefDef(Modifiers(), TermName("unapply"), List(), List(List(ValDef(Modifiers(PARAM), TermName("x"), Ident(TypeName("Any")), EmptyTree))), TypeTree(),
+ extractorBody()))))
+ c.introduceTopLevel(nme.EMPTY_PACKAGE_NAME.toString, mdef)
+ c.Expr[Option[Any]](Apply(Select(Ident(name), TermName("unapply")), List(x.tree)))
+ }
+} \ No newline at end of file