Permalink
Browse files

Provide a way for unapply macro to obtain a list of subpattens

This commit introduces internal attachment that allows unapply macros to
be aware of their sub patterns and tweak their expansion depending
on that info.

At the moment this is not possible due to the way pattern macros are
expanded:

	case MacroPat(inner1, inner2) => ...

During type checking this will expand as

	MacroPat.unapply(<unapply-dummy>)

Meaning that macro can’t see inner1 and inner2 in it’s macroApplication.
To circumvent this we attach that info as an attachment to the dummy.
  • Loading branch information...
1 parent f3c260b commit 4be6ea147a7d8f300c1e6db2a216b50fe8cf5dc7 @densh densh committed Dec 2, 2013
@@ -391,8 +391,10 @@ trait PatternTypers {
else freshUnapplyArgType()
)
)
+ val unapplyArgTree = Ident(unapplyArg) updateAttachment SubpatternsAttachment(args)
+
// clearing the type is necessary so that ref will be stabilized; see bug 881
- val fun1 = typedPos(fun.pos)(Apply(Select(fun.clearType(), unapplyMethod), Ident(unapplyArg) :: Nil))
+ val fun1 = typedPos(fun.pos)(Apply(Select(fun.clearType(), unapplyMethod), unapplyArgTree :: Nil))
def makeTypedUnApply() = {
// the union of the expected type and the inferred type of the argument to unapply
@@ -35,4 +35,7 @@ trait StdAttachments {
/** Identifies trees are either result or intermidiate value of for loop desugaring.
*/
case object ForAttachment extends PlainAttachment
+
+ /** Untyped list of subpatterns attached to selector dummy. */
+ case class SubpatternsAttachment(patterns: List[Tree])
}
@@ -57,6 +57,7 @@ trait JavaUniverseForce { self: runtime.JavaUniverse =>
this.CompoundTypeTreeOriginalAttachment
this.BackquotedIdentifierAttachment
this.ForAttachment
+ this.SubpatternsAttachment
this.noPrint
this.typeDebug
// inaccessible: this.maxFree

0 comments on commit 4be6ea1

Please sign in to comment.