From 74297159f5c4df42dcf6289f4daea79e7d4f7bb4 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Sun, 28 Oct 2012 13:44:10 +0100 Subject: 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 (): 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 (): 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 (): 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 { --- src/compiler/scala/tools/nsc/typechecker/Typers.scala | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src') 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 Function { def apply(params) = `translateMatch('sel match { cases }')` } -- cgit v1.2.3