summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2012-10-28 13:44:10 +0100
committerJason Zaugg <jzaugg@gmail.com>2012-10-28 23:17:48 +0100
commit74297159f5c4df42dcf6289f4daea79e7d4f7bb4 (patch)
tree3ab72667fcf8689ba01fb7272601965dcdc25153
parent2c554249fd8e99286134b217027b6e3cb2c92d77 (diff)
downloadscala-74297159f5c4df42dcf6289f4daea79e7d4f7bb4.tar.gz
scala-74297159f5c4df42dcf6289f4daea79e7d4f7bb4.tar.bz2
scala-74297159f5c4df42dcf6289f4daea79e7d4f7bb4.zip
SI-6575 Plug inference leak of AbstractPartialFun
Usually this isn't needed, as partial functions can only be defined with an expected type. But if that expected type is and inherited method return type, the actual type of the partial function literal is used, and the implementation detail of `AbstractPartialFunction[A, B] with Serializable` leaks out. After this change, the inferred types match those from Scala 2.9.2. ticket/6575 ~/code/scala scalac29 -Xprint:typer test/files/pos/t6575a.scala | grep def > 29.txt ticket/6575 ~/code/scala squalac -Xprint:typer test/files/pos/t6575a.scala | grep def > 210.txt ticket/6575 ~/code/scala diff -u 29.txt 210.txt --- 29.txt 2012-10-28 13:51:07.000000000 +0100 +++ 210.txt 2012-10-28 13:51:20.000000000 +0100 @@ -1,7 +1,16 @@ def foo: PartialFunction[Int,Int] def /*Y*/$init$(): Unit = { - absoverride def foo: PartialFunction[Int,Int] = ((x0$1: Int) => x0$1 match { + absoverride def foo: PartialFunction[Int,Int] = { + def <init>(): anonymous class $anonfun = { + final override def applyOrElse[A1 >: Nothing <: Int, B1 >: Int <: Any](x$1: A1, default: A1 => B1): B1 = (x$1: A1 @unchecked) match { + final def isDefinedAt(x$1: Int): Boolean = (x$1: Int @unchecked) match { def /*Z*/$init$(): Unit = { - absoverride def foo: PartialFunction[Int,Int] = ((x0$2: Int) => x0$2 match { + absoverride def foo: PartialFunction[Int,Int] = { + def <init>(): anonymous class $anonfun = { + final override def applyOrElse[A1 >: Nothing <: Int, B1 >: Int <: Any](x$1: A1, default: A1 => B1): B1 = (x$1: A1 @unchecked) match { + final def isDefinedAt(x$1: Int): Boolean = (x$1: Int @unchecked) match { def /*Comb*/$init$(): Unit = { - absoverride def foo: PartialFunction[Int,Int] = ((x0$3: Int) => x0$3 match { + absoverride def foo: PartialFunction[Int,Int] = { + def <init>(): anonymous class $anonfun = { + final override def applyOrElse[A1 >: Nothing <: Int, B1 >: Int <: Any](x$1: A1, default: A1 => B1): B1 = (x$1: A1 @unchecked) match { + final def isDefinedAt(x$1: Int): Boolean = (x$1: Int @unchecked) match {
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/Typers.scala10
-rw-r--r--test/files/pos/t6575a.scala15
-rw-r--r--test/files/pos/t6575b.scala17
3 files changed, 41 insertions, 1 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala
index f82786da35..8e06588ed8 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala
@@ -2590,7 +2590,15 @@ trait Typers extends Modes with Adaptations with Tags {
def translated =
if (members.head eq EmptyTree) setError(tree)
- else typed(atPos(tree.pos)(Block(List(ClassDef(anonClass, NoMods, ListOfNil, ListOfNil, members, tree.pos.focus)), atPos(tree.pos.focus)(New(anonClass.tpe)))), mode, pt)
+ else {
+ val typedBlock = typed(atPos(tree.pos)(
+ Block(ClassDef(anonClass, NoMods, ListOfNil, ListOfNil, members, tree.pos.focus), atPos(tree.pos.focus)(New(anonClass.tpe)))
+ ), mode, pt)
+ // Don't leak implementation details into the type, see SI-6575
+ if (isPartial && !typedBlock.isErrorTyped)
+ typedBlock modifyType (_ baseType PartialFunctionClass)
+ else typedBlock
+ }
}
// Function(params, Match(sel, cases)) ==> new <Partial>Function { def apply<OrElse>(params) = `translateMatch('sel match { cases }')` }
diff --git a/test/files/pos/t6575a.scala b/test/files/pos/t6575a.scala
new file mode 100644
index 0000000000..f128714dab
--- /dev/null
+++ b/test/files/pos/t6575a.scala
@@ -0,0 +1,15 @@
+trait X { def foo: PartialFunction[Int, Int] }
+
+trait Y extends X {
+ // Inferred type was AbstractPartialFunction[Int, Int] with Serializable
+ abstract override def foo = { case i => super.foo(i) * 2 }
+}
+trait Z extends X {
+ // ditto
+ abstract override def foo = { case i => super.foo(i) + 3 }
+}
+
+trait Comb extends Y with Z {
+ // ... which led to a type error here.
+ abstract override def foo: PartialFunction[Int, Int] = { case i => super.foo(i) - 2 }
+}
diff --git a/test/files/pos/t6575b.scala b/test/files/pos/t6575b.scala
new file mode 100644
index 0000000000..d3e58b2a16
--- /dev/null
+++ b/test/files/pos/t6575b.scala
@@ -0,0 +1,17 @@
+// inferred types were okay here as Function nodes aren't
+// translated into anoymous subclasses of AbstractFunctionN
+// until after the typer.
+//
+// So this test is just confirmation.
+trait X { def foo: Function1[Int, Int] }
+
+trait Y extends X {
+ abstract override def foo = { case i => super.foo(i) * 2 }
+}
+trait Z extends X {
+ abstract override def foo = { case i => super.foo(i) + 3 }
+}
+
+trait Comb extends Y with Z {
+ abstract override def foo: Function1[Int, Int] = { case i => super.foo(i) - 2 }
+}