Skip to content

Commit

Permalink
Allow for Function treess with refined types in UnCurry.
Browse files Browse the repository at this point in the history
Also removes an unnecessary condition in UnCurry, isFunctionType(fun.tpe)
is always true.
  • Loading branch information
lrytz committed Feb 3, 2013
1 parent 59918ee commit 6697c28
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
18 changes: 13 additions & 5 deletions src/compiler/scala/tools/nsc/transform/UnCurry.scala
Expand Up @@ -231,18 +231,25 @@ abstract class UnCurry extends InfoTransform
* If `settings.XoldPatmat.value`, also synthesized AbstractPartialFunction subclasses (see synthPartialFunction).
*
*/
def transformFunction(fun: Function): Tree =
def transformFunction(fun: Function): Tree = {
fun.tpe match {
// can happen when analyzer plugins assign refined types to functions, e.g.
// (() => Int) { def apply(): Int @typeConstraint }
case RefinedType(List(funTp), decls) =>
debuglog(s"eliminate refinement from function type ${fun.tpe}")
fun.tpe = funTp
case _ =>
()
}

deEta(fun) match {
// nullary or parameterless
case fun1 if fun1 ne fun => fun1
case _ if fun.tpe.typeSymbol == PartialFunctionClass =>
// only get here when running under -Xoldpatmat
synthPartialFunction(fun)
case _ =>
val parents = (
if (isFunctionType(fun.tpe)) addSerializable(abstractFunctionForFunctionType(fun.tpe))
else addSerializable(ObjectClass.tpe, fun.tpe)
)
val parents = addSerializable(abstractFunctionForFunctionType(fun.tpe))
val anonClass = fun.symbol.owner newAnonymousFunctionClass(fun.pos, inConstructorFlag) addAnnotation serialVersionUIDAnnotation
anonClass setInfo ClassInfoType(parents, newScope, anonClass)

Expand Down Expand Up @@ -275,6 +282,7 @@ abstract class UnCurry extends InfoTransform
}

}
}

/** Transform a function node (x => body) of type PartialFunction[T, R] where
* body = expr match { case P_i if G_i => E_i }_i=1..n
Expand Down
7 changes: 4 additions & 3 deletions src/reflect/scala/reflect/internal/Definitions.scala
Expand Up @@ -678,9 +678,10 @@ trait Definitions extends api.StandardDefinitions {

def functionApply(n: Int) = getMemberMethod(FunctionClass(n), nme.apply)

def abstractFunctionForFunctionType(tp: Type) =
if (isFunctionType(tp)) abstractFunctionType(tp.typeArgs.init, tp.typeArgs.last)
else NoType
def abstractFunctionForFunctionType(tp: Type) = {
assert(isFunctionType(tp), tp)
abstractFunctionType(tp.typeArgs.init, tp.typeArgs.last)
}

def isFunctionType(tp: Type): Boolean = tp.normalize match {
case TypeRef(_, sym, args) if args.nonEmpty =>
Expand Down

0 comments on commit 6697c28

Please sign in to comment.