From 1e3012d5542b41b7303e6b8eda0931814a0b28f9 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Mon, 17 Sep 2012 23:38:49 +0200 Subject: SI-6287 fixes synthetic symbol clashes in toolbox Apparently synthetic classes like $anonfun$1 have two properties: 1) Their names are generated using a counter unique to a compilation unit 2) After flatten they levitate to the nearest enclosing package As a result if we use an empty package to wrap toolbox codegen, then this package will soon be overflown by $anonfun$1 symbols, because: 1) New codegen session = new compilation unit = new counter which starts at 0 2) New codegen session = new anon funs that end up as children of empty package Creating a freshly named package for each codegen session fixed the problem. Now anonfuns from different sessions end up with different parents. --- src/compiler/scala/tools/reflect/ToolBoxFactory.scala | 6 +++--- test/files/run/t6287.check | 3 +++ test/files/run/t6287.scala | 11 +++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 test/files/run/t6287.check create mode 100644 test/files/run/t6287.scala diff --git a/src/compiler/scala/tools/reflect/ToolBoxFactory.scala b/src/compiler/scala/tools/reflect/ToolBoxFactory.scala index d941519958..091224c88a 100644 --- a/src/compiler/scala/tools/reflect/ToolBoxFactory.scala +++ b/src/compiler/scala/tools/reflect/ToolBoxFactory.scala @@ -175,7 +175,7 @@ abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf => val thunks = freeTerms map (fte => () => fte.value) // need to be lazy in order not to distort evaluation order verify(expr) - def wrap(expr0: Tree): Tree = { + def wrap(expr0: Tree): ModuleDef = { val (expr, freeTerms) = extractFreeTerms(expr0, wrapFreeTermRefs = true) val (obj, mclazz) = rootMirror.EmptyPackageClass.newModuleAndClassSymbol( @@ -213,11 +213,11 @@ abstract class ToolBoxFactory[U <: JavaUniverse](val u: U) { factorySelf => var cleanedUp = resetLocalAttrs(moduledef) trace("cleaned up: ")(showAttributed(cleanedUp, true, true, settings.Yshowsymkinds.value)) - cleanedUp + cleanedUp.asInstanceOf[ModuleDef] } val mdef = wrap(expr) - val pdef = PackageDef(Ident(nme.EMPTY_PACKAGE_NAME), List(mdef)) + val pdef = PackageDef(Ident(mdef.name), List(mdef)) val unit = new CompilationUnit(NoSourceFile) unit.body = pdef diff --git a/test/files/run/t6287.check b/test/files/run/t6287.check new file mode 100644 index 0000000000..2a783704a2 --- /dev/null +++ b/test/files/run/t6287.check @@ -0,0 +1,3 @@ +Vector(2, 3, 4) +Vector(2, 3, 4) +Vector(2, 3, 4) diff --git a/test/files/run/t6287.scala b/test/files/run/t6287.scala new file mode 100644 index 0000000000..0c75d1081b --- /dev/null +++ b/test/files/run/t6287.scala @@ -0,0 +1,11 @@ +import scala.reflect.runtime.universe._ +import scala.reflect.runtime.{currentMirror => cm} +import scala.tools.reflect._ + +object Test extends App { + val tb = cm.mkToolBox() + val t1 = tb.parse("1 to 3 map (_+1)") + println(tb.eval(t1)) + println(tb.eval(t1)) + println(tb.eval(t1)) +} \ No newline at end of file -- cgit v1.2.3