Permalink
Browse files

SI-8411 match desugared partial functions

  • Loading branch information...
1 parent 79f7e05 commit 5e23a6ac32394130dd72458c7efe38dbd1ddb116 Denys Shabalin committed Mar 17, 2014
@@ -765,7 +765,7 @@ trait Internals { self: Universe =>
val SyntacticPartialFunction: SyntacticPartialFunctionExtractor
trait SyntacticPartialFunctionExtractor {
def apply(cases: List[Tree]): Match
- def unapply(tree: Match): Option[List[CaseDef]]
+ def unapply(tree: Tree): Option[List[CaseDef]]
}
val SyntacticMatch: SyntacticMatchExtractor
@@ -6,7 +6,7 @@ import Flags._
import util._
trait ReificationSupport { self: SymbolTable =>
- import definitions.{TupleClass, FunctionClass, ScalaPackage, UnitClass}
+ import definitions._
import internal._
class ReificationSupportImpl extends ReificationSupportApi {
@@ -852,9 +852,25 @@ trait ReificationSupport { self: SymbolTable =>
object SyntacticPartialFunction extends SyntacticPartialFunctionExtractor {
def apply(cases: List[Tree]): Match = Match(EmptyTree, mkCases(cases))
- def unapply(tree: Match): Option[List[CaseDef]] = tree match {
+ def unapply(tree: Tree): Option[List[CaseDef]] = tree match {
case Match(EmptyTree, cases) => Some(cases)
- case _ => None
+ case Typed(
+ Block(
+ List(ClassDef(clsMods, tpnme.ANON_FUN_NAME, Nil, Template(
+ List(abspf: TypeTree, ser: TypeTree), noSelfType, List(
+ DefDef(_, nme.CONSTRUCTOR, _, _, _, _),
+ DefDef(_, nme.applyOrElse, _, _, _,
+ Match(_, cases :+
+ CaseDef(Bind(nme.DEFAULT_CASE, Ident(nme.WILDCARD)), _, _))),
+ DefDef(_, nme.isDefinedAt, _, _, _, _))))),
+ Apply(Select(New(Ident(tpnme.ANON_FUN_NAME)), termNames.CONSTRUCTOR), List())),
+ pf: TypeTree)
+ if pf.tpe != null && pf.tpe.typeSymbol.eq(PartialFunctionClass) &&
+ abspf.tpe != null && abspf.tpe.typeSymbol.eq(AbstractPartialFunctionClass) &&
+ ser.tpe != null && ser.tpe.typeSymbol.eq(SerializableClass) &&
+ clsMods.hasFlag(FINAL) && clsMods.hasFlag(SYNTHETIC) =>
+ Some(cases)
+ case _ => None
}
}
@@ -0,0 +1,10 @@
+import scala.language.experimental.macros
+import scala.reflect.macros.blackbox.Context
+
+object Macros {
+ def defaultZeroCase(pf: PartialFunction[Int, Int]): PartialFunction[Int, Int] = macro impl
+ def impl(c: Context)(pf: c.Tree) = { import c.universe._
+ val q"{ case ..$cases }" = pf
+ q"{ case ..$cases case _ => 0 }"
+ }
+}
@@ -0,0 +1,4 @@
+object Test extends App {
+ val pf = Macros.defaultZeroCase { case 1 => 2 }
+ assert(pf(2) == 0)
+}
@@ -152,4 +152,9 @@ object TypecheckedProps extends QuasiquoteProperties("typechecked") {
assert(name == defName)
assert(rhs ≈ defRhs)
}
+
+ property("partial function") = test {
+ val q"{ case ..$cases }: $ascr" = typecheck(q"{ case 1 => () }: PartialFunction[Int, Unit]")
+ assert(cases ≈ q"{ case 1 => () }".cases)
+ }
}

0 comments on commit 5e23a6a

Please sign in to comment.