Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

SI-7789 make quasiquotes deconstruct UnApply trees

  • Loading branch information...
commit e6eed418eefe3122b15c8e64b8697c5770a74960 1 parent 1188f95
@densh densh authored
View
8 src/reflect/scala/reflect/internal/BuildUtils.scala
@@ -163,9 +163,11 @@ trait BuildUtils { self: SymbolTable =>
def apply(tree: Tree, argss: List[List[Tree]]): Tree =
argss.foldLeft(tree) { (f, args) => Apply(f, args.map(treeInfo.assignmentToMaybeNamedArg)) }
- def unapply(tree: Tree): Some[(Tree, List[List[Tree]])] = {
- val treeInfo.Applied(fun, targs, argss) = tree
- Some((SyntacticTypeApplied(fun, targs), argss))
+ def unapply(tree: Tree): Some[(Tree, List[List[Tree]])] = tree match {
+ case UnApply(treeInfo.Unapplied(Select(fun, nme.unapply)), pats) =>
+ Some((fun, pats :: Nil))
+ case treeInfo.Applied(fun, targs, argss) =>
+ Some((SyntacticTypeApplied(fun, targs), argss))
}
}
View
25 test/files/scalacheck/quasiquotes/TypecheckedProps.scala
@@ -50,4 +50,29 @@ object TypecheckedProps extends QuasiquoteProperties("typechecked") {
assert(enums1 ≈ enums)
assert(body1 ≈ body)
}
+
+ property("extract UnApply (1)") = test {
+ val q"object $_ { $_; $_; $m }" = typecheck(q"""
+ object Test {
+ class Cell(val x: Int)
+ object Cell { def unapply(c: Cell) = Some(c.x) }
+ new Cell(0) match { case Cell(v) => v }
+ }
+ """)
+ val q"$_ match { case $f(..$args) => $_ }" = m
+ assert(f ≈ pq"Test.this.Cell")
+ assert(args ≈ List(pq"v"))
+ }
+
+ property("extract UnApply (2)") = test {
+ val q"object $_ { $_; $_; $m }" = typecheck(q"""
+ object Test {
+ case class Cell(val x: Int)
+ new Cell(0) match { case Cell(v) => v }
+ }
+ """)
+ val q"$_ match { case ${f: TypeTree}(..$args) => $_ }" = m
+ assert(f.original ≈ pq"Test.this.Cell")
+ assert(args ≈ List(pq"v"))
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.