diff --git a/src/compiler/scala/tools/nsc/transform/Delambdafy.scala b/src/compiler/scala/tools/nsc/transform/Delambdafy.scala index d2c511a2d1fd..1f832ba81eab 100644 --- a/src/compiler/scala/tools/nsc/transform/Delambdafy.scala +++ b/src/compiler/scala/tools/nsc/transform/Delambdafy.scala @@ -113,7 +113,9 @@ abstract class Delambdafy extends Transform with TypingTransformers with ast.Tre // after working on the entire compilation until we'll have a set of // new class definitions to add to the top level override def transformStats(stats: List[Tree], exprOwner: Symbol): List[Tree] = { - super.transformStats(stats, exprOwner) ++ lambdaClassDefs(exprOwner) + // Need to remove from the lambdaClassDefs map: there may be multiple PackageDef for the same + // package when defining a package object. We only add the lambda class to one. See SI-9097. + super.transformStats(stats, exprOwner) ++ lambdaClassDefs.remove(exprOwner).getOrElse(Nil) } private def optionSymbol(sym: Symbol): Option[Symbol] = if (sym.exists) Some(sym) else None diff --git a/src/compiler/scala/tools/nsc/transform/Flatten.scala b/src/compiler/scala/tools/nsc/transform/Flatten.scala index 6149e40fa7f7..fbb030777341 100644 --- a/src/compiler/scala/tools/nsc/transform/Flatten.scala +++ b/src/compiler/scala/tools/nsc/transform/Flatten.scala @@ -166,7 +166,7 @@ abstract class Flatten extends InfoTransform { override def transformStats(stats: List[Tree], exprOwner: Symbol): List[Tree] = { val stats1 = super.transformStats(stats, exprOwner) if (currentOwner.isPackageClass) { - val lifted = liftedDefs(currentOwner).toList + val lifted = liftedDefs.remove(currentOwner).toList.flatten stats1 ::: lifted } else stats1 diff --git a/src/compiler/scala/tools/nsc/transform/LambdaLift.scala b/src/compiler/scala/tools/nsc/transform/LambdaLift.scala index fa0c1f797bc3..a703542587e7 100644 --- a/src/compiler/scala/tools/nsc/transform/LambdaLift.scala +++ b/src/compiler/scala/tools/nsc/transform/LambdaLift.scala @@ -539,12 +539,11 @@ abstract class LambdaLift extends InfoTransform { override def transformStats(stats: List[Tree], exprOwner: Symbol): List[Tree] = { def addLifted(stat: Tree): Tree = stat match { case ClassDef(_, _, _, _) => - val lifted = liftedDefs get stat.symbol match { + val lifted = liftedDefs remove stat.symbol match { case Some(xs) => xs reverseMap addLifted case _ => log("unexpectedly no lifted defs for " + stat.symbol) ; Nil } - try deriveClassDef(stat)(impl => deriveTemplate(impl)(_ ::: lifted)) - finally liftedDefs -= stat.symbol + deriveClassDef(stat)(impl => deriveTemplate(impl)(_ ::: lifted)) case DefDef(_, _, _, _, _, Block(Nil, expr)) if !stat.symbol.isConstructor => deriveDefDef(stat)(_ => expr) diff --git a/test/files/run/t9097.scala b/test/files/run/t9097.scala new file mode 100644 index 000000000000..d2bf55fc4481 --- /dev/null +++ b/test/files/run/t9097.scala @@ -0,0 +1,34 @@ +import scala.tools.partest._ +import java.io.{Console => _, _} + +object Test extends StoreReporterDirectTest { + + override def extraSettings: String = List( + "-usejavacp", + "-Xfatal-warnings", + "-Ybackend:GenBCode", + "-Ydelambdafy:method", + "-Xprint:delambdafy", + s"-d ${testOutput.path}" + ) mkString " " + + override def code = """package o + |package a { + | class C { + | def hihi = List(1,2).map(_ * 2) + | } + |} + |package object a { + | def f = 1 + |} + |""".stripMargin.trim + + override def show(): Unit = { + val baos = new java.io.ByteArrayOutputStream() + Console.withOut(baos)(Console.withErr(baos)(compile())) + assert(!storeReporter.hasErrors, message = filteredInfos map (_.msg) mkString "; ") + val out = baos.toString("UTF-8") + // was 2 before the fix, the two PackageDefs for a would both contain the ClassDef for the closure + assert(out.lines.count(_ contains "class hihi$1") == 1, out) + } +}