summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdriaan Moors <adriaan.moors@typesafe.com>2013-11-25 14:33:25 -0800
committerAdriaan Moors <adriaan.moors@typesafe.com>2013-11-25 14:33:25 -0800
commit5f21394cd42dbd93e34320e09403cd66bdad3a96 (patch)
treee77e5be48543793c2e5826c5cbb3aa215ddc184a
parent247828ed0dfb7daedf60dafb7413537c788848a0 (diff)
parent5d5596bb07c0b5985fe9a6ba5433a3d463918b28 (diff)
downloadscala-5f21394cd42dbd93e34320e09403cd66bdad3a96.tar.gz
scala-5f21394cd42dbd93e34320e09403cd66bdad3a96.tar.bz2
scala-5f21394cd42dbd93e34320e09403cd66bdad3a96.zip
Merge pull request #3165 from retronym/ticket/uncurry-tidy-2
Unifying -Ydelambdafy:{inline, method}
-rw-r--r--src/compiler/scala/tools/nsc/ast/TreeGen.scala40
-rw-r--r--src/compiler/scala/tools/nsc/transform/UnCurry.scala111
-rw-r--r--src/reflect/scala/reflect/internal/Trees.scala8
-rw-r--r--src/reflect/scala/reflect/internal/tpe/TypeMaps.scala6
-rw-r--r--test/files/run/delambdafy-dependent-on-param-subst-2.scala20
-rw-r--r--test/files/run/delambdafy-dependent-on-param-subst.flags1
-rw-r--r--test/files/run/delambdafy-dependent-on-param-subst.scala20
-rw-r--r--test/files/run/value-class-partial-func-depmet.scala24
8 files changed, 134 insertions, 96 deletions
diff --git a/src/compiler/scala/tools/nsc/ast/TreeGen.scala b/src/compiler/scala/tools/nsc/ast/TreeGen.scala
index d4ac21a6b8..4ac6672727 100644
--- a/src/compiler/scala/tools/nsc/ast/TreeGen.scala
+++ b/src/compiler/scala/tools/nsc/ast/TreeGen.scala
@@ -256,4 +256,44 @@ abstract class TreeGen extends scala.reflect.internal.TreeGen with TreeDSL {
val stats1 = if (stats.isEmpty) List(Literal(Constant(()))) else stats
mkNew(Nil, noSelfType, stats1, NoPosition, NoPosition)
}
+
+ /**
+ * Create a method based on a Function
+ *
+ * Used both to under `-Ydelambdafy:method` create a lifted function and
+ * under `-Ydelamdafy:inline` to create the apply method on the anonymous
+ * class.
+ *
+ * It creates a method definition with value params cloned from the
+ * original lambda. Then it calls a supplied function to create
+ * the body and types the result. Finally
+ * everything is wrapped up in a DefDef
+ *
+ * @param owner The owner for the new method
+ * @param name name for the new method
+ * @param additionalFlags flags to be put on the method in addition to FINAL
+ */
+ def mkMethodFromFunction(localTyper: analyzer.Typer)
+ (fun: Function, owner: Symbol, name: TermName, additionalFlags: FlagSet = NoFlags) = {
+ val funParams = fun.vparams map (_.symbol)
+ val formals :+ restpe = fun.tpe.typeArgs
+
+ val methSym = owner.newMethod(name, fun.pos, FINAL | additionalFlags)
+
+ val paramSyms = map2(formals, fun.vparams) {
+ (tp, vparam) => methSym.newSyntheticValueParam(tp, vparam.name)
+ }
+
+ methSym setInfo MethodType(paramSyms, restpe.deconst)
+
+ fun.body.substituteSymbols(funParams, paramSyms)
+ fun.body changeOwner (fun.symbol -> methSym)
+
+ val methDef = DefDef(methSym, fun.body)
+
+ // Have to repack the type to avoid mismatches when existentials
+ // appear in the result - see SI-4869.
+ methDef.tpt setType localTyper.packedType(fun.body, methSym).deconst
+ methDef
+ }
}
diff --git a/src/compiler/scala/tools/nsc/transform/UnCurry.scala b/src/compiler/scala/tools/nsc/transform/UnCurry.scala
index 3d648ccbac..844774e75f 100644
--- a/src/compiler/scala/tools/nsc/transform/UnCurry.scala
+++ b/src/compiler/scala/tools/nsc/transform/UnCurry.scala
@@ -217,112 +217,33 @@ abstract class UnCurry extends InfoTransform
// nullary or parameterless
case fun1 if fun1 ne fun => fun1
case _ =>
- val parents = addSerializable(abstractFunctionForFunctionType(fun.tpe))
- val anonClass = fun.symbol.owner newAnonymousFunctionClass(fun.pos, inConstructorFlag) addAnnotation SerialVersionUIDAnnotation
- anonClass setInfo ClassInfoType(parents, newScope, anonClass)
-
- val targs = fun.tpe.typeArgs
- val (formals, restpe) = (targs.init, targs.last)
+ def typedFunPos(t: Tree) = localTyper.typedPos(fun.pos)(t)
+ val funParams = fun.vparams map (_.symbol)
+ def mkMethod(owner: Symbol, name: TermName, additionalFlags: FlagSet = NoFlags): DefDef =
+ gen.mkMethodFromFunction(localTyper)(fun, owner, name, additionalFlags)
if (inlineFunctionExpansion) {
- val applyMethodDef = {
- val methSym = anonClass.newMethod(nme.apply, fun.pos, FINAL)
- val paramSyms = map2(formals, fun.vparams) {
- (tp, param) => methSym.newSyntheticValueParam(tp, param.name)
- }
- methSym setInfoAndEnter MethodType(paramSyms, restpe)
-
- fun.vparams foreach (_.symbol.owner = methSym)
- fun.body changeOwner (fun.symbol -> methSym)
-
- val body = localTyper.typedPos(fun.pos)(fun.body)
- val methDef = DefDef(methSym, List(fun.vparams), body)
+ val parents = addSerializable(abstractFunctionForFunctionType(fun.tpe))
+ val anonClass = fun.symbol.owner newAnonymousFunctionClass(fun.pos, inConstructorFlag) addAnnotation SerialVersionUIDAnnotation
+ anonClass setInfo ClassInfoType(parents, newScope, anonClass)
- // Have to repack the type to avoid mismatches when existentials
- // appear in the result - see SI-4869.
- methDef.tpt setType localTyper.packedType(body, methSym)
- methDef
- }
+ val applyMethodDef = mkMethod(anonClass, nme.apply)
+ anonClass.info.decls enter applyMethodDef.symbol
- localTyper.typedPos(fun.pos) {
+ typedFunPos {
Block(
- List(ClassDef(anonClass, NoMods, ListOfNil, List(applyMethodDef), fun.pos)),
+ ClassDef(anonClass, NoMods, ListOfNil, List(applyMethodDef), fun.pos),
Typed(New(anonClass.tpe), TypeTree(fun.tpe)))
}
} else {
- /**
- * Abstracts away the common functionality required to create both
- * the lifted function and the apply method on the anonymous class
- * It creates a method definition with value params cloned from the
- * original lambda. Then it calls a supplied function to create
- * the body and types the result. Finally
- * everything is wrapped up in a MethodDef
- *
- * TODO it is intended that this common functionality be used
- * whether inlineFunctionExpansion is true or not. However, it
- * seems to introduce subtle ownwership changes that produce
- * binary incompatible changes and so it is completely
- * hidden behind the inlineFunctionExpansion for now.
- *
- * @param owner The owner for the new method
- * @param name name for the new method
- * @param additionalFlags flags to be put on the method in addition to FINAL
- * @bodyF function that turns the method symbol and list of value params
- * into a body for the method
- */
- def createMethod(owner: Symbol, name: TermName, additionalFlags: Long)(bodyF: (Symbol, List[ValDef]) => Tree) = {
- val methSym = owner.newMethod(name, fun.pos, FINAL | additionalFlags)
- val vparams = fun.vparams map (_.duplicate)
-
- val paramSyms = map2(formals, vparams) {
- (tp, vparam) => methSym.newSyntheticValueParam(tp, vparam.name)
- }
- foreach2(vparams, paramSyms){(valdef, sym) => valdef.symbol = sym}
- vparams foreach (_.symbol.owner = methSym)
-
- val methodType = MethodType(paramSyms, restpe.deconst)
- methSym setInfo methodType
-
- // TODO this is probably cleaner if bodyF only works with symbols rather than parameter ValDefs
- val tempBody = bodyF(methSym, vparams)
- val body = localTyper.typedPos(fun.pos)(tempBody)
- val methDef = DefDef(methSym, List(vparams), body)
-
- // Have to repack the type to avoid mismatches when existentials
- // appear in the result - see SI-4869.
- methDef.tpt setType localTyper.packedType(body, methSym).deconst
- methDef
- }
-
- val methodFlags = ARTIFACT
// method definition with the same arguments, return type, and body as the original lambda
- val liftedMethod = createMethod(fun.symbol.owner, tpnme.ANON_FUN_NAME.toTermName, methodFlags){
- case(methSym, vparams) =>
- fun.body.substituteSymbols(fun.vparams map (_.symbol), vparams map (_.symbol))
- fun.body changeOwner (fun.symbol -> methSym)
- }
-
- // callsite for the lifted method
- val args = fun.vparams map { vparam =>
- val ident = Ident(vparam.symbol)
- // if -Yeta-expand-keeps-star is turned on then T* types can get through. In order
- // to forward them we need to forward x: T* ascribed as "x:_*"
- if (settings.etaExpandKeepsStar && definitions.isRepeatedParamType(vparam.tpt.tpe))
- gen.wildcardStar(ident)
- else
- ident
- }
-
- val funTyper = localTyper.typedPos(fun.pos) _
-
- val liftedMethodCall = funTyper(Apply(liftedMethod.symbol, args:_*))
+ val liftedMethod = mkMethod(fun.symbol.owner, nme.ANON_FUN_NAME, additionalFlags = ARTIFACT)
// new function whose body is just a call to the lifted method
- val newFun = treeCopy.Function(fun, fun.vparams, liftedMethodCall)
- funTyper(Block(
- List(funTyper(liftedMethod)),
- super.transform(newFun)
- ))
+ val newFun = deriveFunction(fun)(_ => typedFunPos(
+ gen.mkForwarder(gen.mkAttributedRef(liftedMethod.symbol), funParams :: Nil)
+ ))
+ typedFunPos(Block(liftedMethod, super.transform(newFun)))
}
}
}
diff --git a/src/reflect/scala/reflect/internal/Trees.scala b/src/reflect/scala/reflect/internal/Trees.scala
index af0af8afe8..d191fbd38f 100644
--- a/src/reflect/scala/reflect/internal/Trees.scala
+++ b/src/reflect/scala/reflect/internal/Trees.scala
@@ -590,7 +590,7 @@ trait Trees extends api.Trees {
def TypeTree(tp: Type): TypeTree = TypeTree() setType tp
private def TypeTreeMemberType(sym: Symbol): TypeTree = {
// Needed for pos/t4970*.scala. See SI-7853
- val resType = (sym.owner.thisType memberType sym).finalResultType
+ val resType = (if (sym.isLocal) sym.tpe else (sym.owner.thisType memberType sym)).finalResultType
atPos(sym.pos.focus)(TypeTree(resType))
}
@@ -1804,6 +1804,12 @@ trait Trees extends api.Trees {
case t =>
sys.error("Not a LabelDef: " + t + "/" + t.getClass)
}
+ def deriveFunction(func: Tree)(applyToRhs: Tree => Tree): Function = func match {
+ case Function(params0, rhs0) =>
+ treeCopy.Function(func, params0, applyToRhs(rhs0))
+ case t =>
+ sys.error("Not a Function: " + t + "/" + t.getClass)
+ }
// -------------- Classtags --------------------------------------------------------
diff --git a/src/reflect/scala/reflect/internal/tpe/TypeMaps.scala b/src/reflect/scala/reflect/internal/tpe/TypeMaps.scala
index 9a54ad8217..f5aa048e6a 100644
--- a/src/reflect/scala/reflect/internal/tpe/TypeMaps.scala
+++ b/src/reflect/scala/reflect/internal/tpe/TypeMaps.scala
@@ -717,6 +717,12 @@ private[internal] trait TypeMaps {
else appliedType(tcon.typeConstructor, args)
case SingleType(NoPrefix, sym) =>
substFor(sym)
+ case ClassInfoType(parents, decls, sym) =>
+ val parents1 = parents mapConserve this
+ // We don't touch decls here; they will be touched when an enclosing TreeSubstitutor
+ // transforms the tree that defines them.
+ if (parents1 eq parents) tp
+ else ClassInfoType(parents1, decls, sym)
case _ =>
tp
}
diff --git a/test/files/run/delambdafy-dependent-on-param-subst-2.scala b/test/files/run/delambdafy-dependent-on-param-subst-2.scala
new file mode 100644
index 0000000000..7b6fc597e8
--- /dev/null
+++ b/test/files/run/delambdafy-dependent-on-param-subst-2.scala
@@ -0,0 +1,20 @@
+trait M[-X] {
+ def m(x: X): Boolean
+}
+
+class C
+class A { class C }
+
+object Test {
+ def main(args: Array[String]) {
+ val a = new A
+
+ // class O extends M[a.C] { def m(x: a.C) = true }
+ // (new O: M[Null]).m(null) // Okay
+
+ ((a: A) => {
+ class N extends M[a.C] { def m(x: a.C) = true }
+ new N: M[Null]
+ }).apply(a).m(null) // NPE, missing bridge
+ }
+}
diff --git a/test/files/run/delambdafy-dependent-on-param-subst.flags b/test/files/run/delambdafy-dependent-on-param-subst.flags
new file mode 100644
index 0000000000..2b27e19830
--- /dev/null
+++ b/test/files/run/delambdafy-dependent-on-param-subst.flags
@@ -0,0 +1 @@
+-Ydelambdafy:method \ No newline at end of file
diff --git a/test/files/run/delambdafy-dependent-on-param-subst.scala b/test/files/run/delambdafy-dependent-on-param-subst.scala
new file mode 100644
index 0000000000..7b6fc597e8
--- /dev/null
+++ b/test/files/run/delambdafy-dependent-on-param-subst.scala
@@ -0,0 +1,20 @@
+trait M[-X] {
+ def m(x: X): Boolean
+}
+
+class C
+class A { class C }
+
+object Test {
+ def main(args: Array[String]) {
+ val a = new A
+
+ // class O extends M[a.C] { def m(x: a.C) = true }
+ // (new O: M[Null]).m(null) // Okay
+
+ ((a: A) => {
+ class N extends M[a.C] { def m(x: a.C) = true }
+ new N: M[Null]
+ }).apply(a).m(null) // NPE, missing bridge
+ }
+}
diff --git a/test/files/run/value-class-partial-func-depmet.scala b/test/files/run/value-class-partial-func-depmet.scala
new file mode 100644
index 0000000000..12ff64ed36
--- /dev/null
+++ b/test/files/run/value-class-partial-func-depmet.scala
@@ -0,0 +1,24 @@
+class C
+class A { class C }
+
+object Test {
+ def main(args: Array[String]) {
+ val a = new A
+
+ new VC("").foo(a)
+ }
+}
+
+class VC(val a: Any) extends AnyVal {
+ def foo(a: A) = {
+ val pf: PartialFunction[a.C, Any] = { case x => x }
+ (pf: PartialFunction[Null, Any]).isDefinedAt(null)
+ }
+}
+
+// 2.11.0-M6
+// test/files/run/value-class-partial-func-depmet.scala:14: error: overriding method applyOrElse in trait PartialFunction of type [A1 <: a.C, B1 >: Any](x: A1, default: A1 => B1)B1;
+// method applyOrElse has incompatible type
+// val pf: PartialFunction[a.C, Any] = { case x => x }
+// ^
+// one error found